[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
Re: [RESOLVED] Delegate Function
it is best practice to NOT invoke the GC, it can cause some undeserible or unexpected behaviors. you should let the GC do its job in the background. Furthermore there is no guarentee the GC will be invoked there and then.
Eventually, the GC class may well be removed from .NET and left out of reach for developers.
The other, and more recommended way in this scenario, thing to do is to hide your other form when you call Show() on this new form. Then when done... raise an event back to the other form so it displays.
Re: [RESOLVED] Delegate Function
Yes understand what you are saying and tried not to use but there are cases where GC may need to be call. In my case it needs to be call to release some memory as the program will be running on an older computer with low memory.
As for the Hide() and Show() methods, it does not work for my situation as Show() will allow the user to click the main form and lose focus on the new form.
Re: [RESOLVED] Delegate Function
think you mis understood what I was saying:
when you call Show(), just before it - hide the form that you are calling Show() FROM, to prevent the user from clicking on the form or maybe not enable it (me.Enabled = false)
as for the GC again, irrespective of memory on the system, let the GC handle it. let the .NET Framework runtime handle the memory usage. it will be fine.