Results 1 to 6 of 6

Thread: [RESOLVED] Delegate Function

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    11

    [RESOLVED] Delegate Function

    Hello, I've been searching through google and here and cannot find a solution my problem. I hope everyone can help me with this problem.

    I have a program that has a main form. This main form has other components that has their own background worker thread too but is separate from the main form. Everything is fine, however the main thread listens to an event when the event is fired by one of the component it start a background process (BackgroundWorker) to show a form dialog for user input. The main thread must continue to work and show the progress from other components it has and show the form dialog to get the user input. The code below works fine but the GetParentFromUIThread() keeps returning nothing if I use BeginInvoke() but if I use Invoke() I get a Cross-thread operation not valid error, thanks in advance for any help.

    Code:
        Private Delegate Function DelGetParent() As Form
    
        Private Function GetParentFromUIThread() As Form
            Dim refParentObject As Form = Nothing
    
            If Me.InvokeRequired Then
                refParentObject = DirectCast(Me.Invoke(New DelGetParent(AddressOf GetParentFromUIThread)), Form)
                'Me.BeginInvoke(New DelGetParent(AddressOf GetParentFromUIThread))
            Else
                refParentObject = Me
            End If
            Return refParentObject
        End Function
    
        Private Sub _NewMaterial_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _NewMaterial.DoWork
            Dim rMaterial As New frmMaterials(_Job)
            rMaterial.EnableCancelButton(False)
            rMaterial.Owner = GetParentFromUIThread()
            rMaterial.ShowDialog()
            rMaterial.Dispose()
        End Sub
    Last edited by MLVB6; Oct 7th, 2011 at 07:46 AM. Reason: RESOLVED

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