Multithreading and Timers (yuck!)

samsmithnz

Senior Contributor
Joined
Jul 22, 2003
Messages
1,038
Location
Boston
I have a form that is doing some fairly hefty processing, so I display a progress bar window above it before I start the task, which has a timer on it that updates a progress bar.

Initially the processing was so intensitive, that the progress window never had time to update, even with application.doevents peppered through out the code.

So I decided to put the loading of Progress form into it's own thread. This all works fine until it's time for the progress window to close... I keep getting WindowsFormsParkingWindow errors when I'm closing the progress window, and they only happen when I enable the timer on the progress window.

I've googled the heck out of WindowsFormsParkingWindow and multithreaded progress windows, but it seems that no one has done anything like what I'm doing before - which I find hard to believe.

The code is a bit too big and sensitive to post here, but I can put in snippets.

Code:
    Private frmProgressi As frmProgress

    Private Sub ShowProgress()

        Try
            frmProgressi = New frmProgress

            'Show the progress window to give the user some feedback while they wait
            frmProgressi.ShowForm("Preparing Documents...")
            blnShowProgress = True

            Do While Not objActiveThread Is Nothing AndAlso blnShowProgress = True AndAlso objActiveThread.IsAlive
                Application.DoEvents()
            Loop

            'close the progress window
            frmProgressi.CloseForm()
            ''Clean up the progress just in case there are some traces of it left.
            frmProgressi.Dispose()

        Catch ex As Threading.ThreadInterruptedException
            'thread interrupted, get out of here!
            'Exit Sub
        End Try
    End Sub

    private Sub RunExpensiveFunction
                    objActiveThread = New Threading.Thread(AddressOf ShowProgress)
                    objActiveThread.Name = "Progress"
                    objActiveThread.Priority = Threading.ThreadPriority.BelowNormal ' Otherwise the progress bar eats up too much CPU time.
                    objActiveThread.Start()

                    ExpensiveFunctionIsRunHere()

                    'Clean up the progress window
                    blnShowProgress = False

    end sub

I've been working on this all day today and all weekend, so I'm a little frustrated. I have a couple workarounds I can try, but it seems to me that this SHOULD work... any ideas???
 
I don't know if this will help, I don't know that much about threading but have you tried:
Visual Basic:
    private Sub RunExpensiveFunction
                    objActiveThread = New Threading.Thread(AddressOf ShowProgress)
                    objActiveThread.Name = "Progress"
                    objActiveThread.Priority = Threading.ThreadPriority.BelowNormal ' Otherwise the progress bar eats up too much CPU time.
                    objActiveThread.Start()

                    ExpensiveFunctionIsRunHere()


                    'Clean up the progress window
                    blnShowProgress = False
                    objActiveThread.Join() '<--Wait for the other thread to finish
    end sub
 
Unfortunetly that doesn't help. I think I need to raise some sort of event or something to stop the timer before I stop the thread...
 
Well I worked this out... I reversed the threading so that the progress popup window was part of the main thread, and my new processing thread was for the expensive function...

Now to do the same with web services... Asynchronous web services should be fun... right? ;)
 
Back
Top