Results 1 to 12 of 12

Thread: [RESOLVED] Wait for process to finish BUT keep VB app running as normal?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Resolved [RESOLVED] Wait for process to finish BUT keep VB app running as normal?

    I am creating a batch processing interface for a program that doesn't currently support batch processing.

    The user clicks on a form button to trigger the batch processing. When they click on the button, obvioulsy this calls the subroutine mybutton_click()

    Within the subroutine, my code to start the external app is:-

    Code:
    Dim myProcess As Process = System.Diagnostics.Process.Start(myProgram, myProgramParams)
    because 'myProcess' contains the name of the running process, I can create a loop within the subroutine to check if the process is running (using Process.GetProcessByName), and if it is, wait for it to complete before starting the next batch entry.

    However, this method means that my program has to continuosly loop within the subroutine until the external process is no longer running and no more batch entries exist before End Sub is reached.

    What I really need to do is this:-

    1) Process the first entry in the batch but wait until the process completes before starting the next batch entry - I don't want any more than one instance of the external program running at any one time.

    2) While any batch entry is running, I want control to return to my program so that the user can do other things - but obviously, when the external process is complete, I need my program to start the next batch entry if there is one...

    How could I go about this? (I hope the above makes sense)

    Many thanks...
    Last edited by christopherpm; Jun 14th, 2008 at 02:57 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Wait for process to finish BUT keep VB app running as normal?

    Thread moved from CodeBank forum (which is for you to post your code examples, not questions)

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wait for process to finish BUT keep VB app running as normal?

    Welcome to the forums.
    The first thing you need to do is to set the EnableRaisingEvents property to True.
    VB.NET Code:
    1. myProcess.EnableRaisingEvents = True
    Then add a handler for the Exited event:
    VB.NET Code:
    1. AddHandler myProcess.Exited, AddressOf ProcessExited

    And of course, you need an eventhandler subroutine:
    VB.NET Code:
    1. Private Sub ProcessExited(ByVal sender As Object, ByVal e As System.EventArgs)
    2.  
    3. End Sub

    Just add your logic in the ProcessExited method. Voila
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Wait for process to finish BUT keep VB app running as normal?

    You may also want to consider using a separate thread.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Re: Wait for process to finish BUT keep VB app running as normal?

    Quote Originally Posted by MaximilianMayrhofer
    You may also want to consider using a separate thread.
    This is the method that I am attempting without success. This is what I have so far.

    Within the myButton_Click() routine, I have the following lines:-

    Code:
    Dim thrMyThread As New System.Threading.Thread(AddressOf StartBatch)
    thrMyThread.Start()
    This obviously calls the StartBatch() routine. This includes the following line, which throws up an exception ONLY when being called using the Thread start method.

    Code:
    strParams = " /file=" & ListView1.Items(0).SubItems(1).Text
    The exception message reads:-

    Code:
    System.InvalidOperationException was unhandled
      Message="Cross-thread operation not valid: Control 'ListView1' accessed from a thread other than the thread it was created on."
      Source="System.Windows.Forms"
      StackTrace:
           at System.Windows.Forms.Control.get_Handle()
           at System.Windows.Forms.ListView.ListViewNativeItemCollection.DisplayIndexToID(Int32 displayIndex)
           at System.Windows.Forms.ListView.ListViewNativeItemCollection.get_Item(Int32 displayIndex)
           at System.Windows.Forms.ListView.ListViewItemCollection.get_Item(Int32 index)
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.runTryCode(Object userData)
           at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    HOWEVER if I call the StartBatch() routine without trying to use threading, then that line works as I would expect... This is my first time with VB 2008 so I'm afraid that I don't know where I'm going wrong... (I've only used VB6 until now)
    Last edited by christopherpm; Jun 15th, 2008 at 01:34 PM.

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Wait for process to finish BUT keep VB app running as normal?

    The exception message is telling you exactly what you are doing wrong. You are trying to access the ListView, which is on your UI thread, from your Process thread. I'm not entirely sure how to solve it, but you could try pushing the Item value into a global string variable and then reading it, like so:

    Code:
    Public Class YourForm
        Private LVItem As String
    
        Private Delegate Sub ItemToStringInvoker(ByVal ItemIndex As Integer, ByVal SubItemIndex As Integer)
    
        Private Sub ItemToString(ByVal ItemIndex As Integer, ByVal SubItemIndex As Integer)
            If Me.ListView1.InvokeRequired Then
                Me.ListView1.Invoke(New ItemToStringInvoker(AddressOf GetListViewItem), ItemIndex, SubItemIndex)
            Else
                LVItem = ListView1.Items(ItemIndex).SubItems(SubItemIndex).Text
            End If
        End Sub
    End Class
    Then change this line:

    Code:
    strParams = " /file=" & ListView1.Items(0).SubItems(1).Text
    to:

    Code:
    ItemToString(0, 1)
    strParams = " /file=" & LVItem

    Of course, you could always put a BackgroundWorker in your components at design time, then do something like this:
    Change your StartBatch() Routine to include a Parameter, strItem:

    Code:
    Private Sub StartBatch(ByVal strItem As String)
        strparams = " /file=" & strItem
    End Sub
    In your button click event:

    Code:
    BackgroundWorker1.RunWorkerASync(ListView1.Items(0).SubItems(1).Text)
    In your BackgroundWorkers Do_Work event:

    Code:
    Start_Batch(DirectCast(e.Argument, String))
    very clean.
    Last edited by MaximilianMayrhofer; Jun 15th, 2008 at 02:33 PM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Re: Wait for process to finish BUT keep VB app running as normal?

    Thanks - I'll give that a go shortly. But can I ask - with regards to the second part of my problem listed in my initial post above, what would be the best way to get my app to keep checking to see if the external program is running so that I can invoke the next entry in the batch only once the current batch entry has completed? One thing I want to be careful of is slowing the program down too much...

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wait for process to finish BUT keep VB app running as normal?

    Theres no need for a separate thread if you're handling the process' Exited event.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Wait for process to finish BUT keep VB app running as normal?

    How is the exited event raised, if i may ask? Is the process started in another thread automatically when you do Process.Start()? If so, where is the application actually checking to see if the process has exited?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Wait for process to finish BUT keep VB app running as normal?

    Quote Originally Posted by MaximilianMayrhofer
    How is the exited event raised, if i may ask? Is the process started in another thread automatically when you do Process.Start()? If so, where is the application actually checking to see if the process has exited?
    The process isn't started in another thread. It's a whole separate process. Threads run within processes, not the other way around.

    The Process class is a managed wrapper for a Windows process. When the process exits the Process object knows because it gets notified by the OS via the process handle. It then passes that notification on to you as a managed event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Wait for process to finish BUT keep VB app running as normal?

    Thanks jmc, that helped alot. I think I know exactly where i'll be able to apply this knowledge.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Resolved Re: Wait for process to finish BUT keep VB app running as normal?

    Thanks MaximilianMayrhofer - my code is now working just as I want it to...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width