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




Reply With Quote