Solved

Resizing Command Prompt Windows that are opened programmatically

Posted on 2009-05-14
5
326 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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

19 Experts available now in Live!

Get 1:1 Help Now