Solved

Resizing Command Prompt Windows that are opened programmatically

Posted on 2009-05-14
5
329 Views
Last Modified: 2012-05-07
I have a program that reads a list of codes and sequentially launches corresponding .bat files using the System.Diagnostics.Process.Start method and then does a procID.waitforexit() before launching the next .bat file in sequence.

When the .Start method opens the command prompt window and launches the .bat file, the window partially covers the main program's window.  Moving the newly opened window does not cause the main program to repaint the window thereby leaving blank spaces denying the user from seeing the main program's status messages.  I'm pretty certain that the waitforexit() function is put the main program into an idle state to the point where it will not process any screen painting commands.

Is there anyway to:
a) control the position of the new process's window, or,
b) loop around the waitforexit() giving it a 500 millisecond pause time and call a DoEvents() inside the loop so that the program can repaint its status messages?

Ideally I'd like to do both, but I will settle for only (b).

Thanks in advance!

Colby
The Not-So-Big Cheese
0
Comment
Question by:ksi-tech
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:Zoooink
ID: 24386206
do you need to see the information on the batch files? Because you could initiate the minimize property for each batch file launched?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24387740
You can poll using code like this:

        Dim p As New Process
        p.StartInfo.FileName = "c:\someFile.bat"
        p.Start()
        While Not p.HasExited
            System.Threading.Thread.Sleep(50) ' reduce cpu usage
            Application.DoEvents() ' keep app responsive
        End While
        ' ...continue with code..

0
 

Author Comment

by:ksi-tech
ID: 24387748
Zoooink, I had thought about doing this, but, my client's policy is to be able to see everything that is happening while it is happening and so this was dismissed early in the project.

Thank you for your comment!

Colby
0
 

Author Closing Comment

by:ksi-tech
ID: 31581515
IdleMind,
Thanks for your code snippet.  I wasn't sure about whether to check the status of the WaitForExit which returns a true/false or if there was another method or property that could be checked.

Colby
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24387891
Right...the loop itself can be written other ways:

        p.Start()
        Do
            Application.DoEvents()
            p.WaitForExit(50)  ' <-- basically equivalent to my Thread.Sleep() call
        Loop While Not p.HasExited
        ' ...continue with code..

Other options include:
(1) Turn on EnableRaisingEvents() and subscribe to the Exited() event:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim p As New Process
        p.StartInfo.FileName = "c:\someFile.bat"
        p.EnableRaisingEvents = True
        AddHandler p.Exited, AddressOf P_Exited
        p.Start()
    End Sub

    Private Sub P_Exited(ByVal sender As Object, ByVal e As System.EventArgs)
        ' ...continue with code here...
    End Sub

(2) Execute the Process.Start() in another thread so you can use WaitForExit() without locking up the main GUI.  Encapsulate that in a seperate class so you can raise an event from it.  This requires you to use Delegates/Invoke though...or you could encapsulate a BackgroundWorker component.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now