Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Referencing a Form on a Seperate Thread...?
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
Referencing a Form on a Seperate Thread...?
Miscellaneous
Thread ID:
01149321
Message ID:
01149321
Views:
66
I converted my code from VS2003 to VS2005 and came across a threading issue. In 2003, it worked without a problem, but now 2005 complains about Cross-Threading.

I created a "Progress Bar Form" which when displayed, runs the ShowDialog() method on a seperate thread. I do this in order to allow the "main" calling thread to continue processing and let the ShowDialog's thread blocking to do so on the secondary thread. This allows me to maintain the ShowDialog() features of locking the UI but not blocking the main thread. Problem is that calling ShowDialog() without a parent form parameter can sometimes cause the form to display in strange locations on your screen. So I added the option to define the parent form to which the "Progress Form" would center against. Problem is that when the "Progress Form" is displayed (on a secondary thread) and it is passed a reference to the parent for it to center on which is on a different thread (the main thread), VS2005 crashes complaining about Cross-Threading.

Now, although I am relatively new to Multi-Threading I am aware of the concept, and I understand it. I have gotten my fair share of migrains debugging Threading problems before in VS2003. I am aware of the concept of a BackgroundWorker and of using the InvokeRequired property to create thread safe methods. I just don't see how these concepts would apply to the situation I am in. I was hoping that someone here would be able to give me some idea's or pointers on how I can fix this particular Cross-Threading issue...? Below is a small peice of the code that is causing my problem.
    Public Sub Display(Optional ByVal objParentForm As Object = Nothing)
        '************************************************************************
        ' Procedure/Function: Display()
        ' Description:
        '       Display the Thermometer form on it's own thread.
        '************************************************************************

        '***************************************
        ' If Parent Form Supplied, Store Reference
        '***************************************
        If Not IsNothing(objParentForm) Then
            Me._objParentForm = objParentForm
        End If

        '***************************************
        ' Display Thermometer Form (Threaded)
        '***************************************
        Dim objNewThread As New Threading.Thread(AddressOf DisplayThreaded)
        objNewThread.Start()
        Threading.Thread.Sleep(1000)
    End Sub

    Private Sub DisplayThreaded()
        '************************************************************************
        ' Procedure/Function: DisplayThreaded()
        ' Description:
        '       Display the Thermometer form.
        '************************************************************************

        If Not IsNothing(Me._objParentForm) Then
            Me._objThermometerForm.ShowDialog(Me._objParentForm) '<-- This Causes a Cross-Threading Error
        Else
            Me._objThermometerForm.ShowDialog()
        End If
    End Sub
________________________
Ben Santiago, MCP & A+
Programmer Analyst (SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS)
Eastern Suffolk BOCES - Student Data Services


Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-Rich Cook
Next
Reply
Map
View

Click here to load this message in the networking platform