d_melnyk
asked on
Problem with cross threading in vb.net with third party control
Hi Folks.
I am having trouble with an error indicating cross threading. I have checked numerous examples but cannot seem to get the invoke / delegate stuff right. Not sure if I can in my case?
using VB.net - Visual Studio 2008
Here is the situation I have
Code in one module does following:
Imports Zipcontrol '// third party control that raises events
Private with events H_Zip As New Zipcontrol
Private MyDisp as New MyFormDisplay '// on form is a listview control
Public Sub Process
'// code sets up for ziping a file and does
H_Zip.Zip
End sub
Private Sub H_Zip_Progress(ByVal sender As Object, ByVal e As ZipControl.ProgressEventAr gs) Handles H_Zip.Progress
'// It is here that I attempt to call a public sub in the form code to update the list view control
'// this results in the corss threading exception
MyDisp.filenameprop = "abcd" '// seems to be no problem to set property in form
MyDisp.UpdateList
end sub
In the code for the form I have tried the following:
Private Delegate sub UpdateMyListview_Delegate( )
Public sub UpdateList()
IF invokerequired then
Dim x as new UpdateMyListView_Delegate( addressof Updatelist)
x.invoke()
Else
'// do normal update of listview
end if
end sub
There are various other combinations I have tried but it either throws cross threading exeception of indicates there is no instance of an object (usually the zip control) when it returns to the other code.
Any assistance with understanding this process would be greatly appreciated. As I foresee running into this many times with the project I am doing.
Best regards, Dave Melnyk
I am having trouble with an error indicating cross threading. I have checked numerous examples but cannot seem to get the invoke / delegate stuff right. Not sure if I can in my case?
using VB.net - Visual Studio 2008
Here is the situation I have
Code in one module does following:
Imports Zipcontrol '// third party control that raises events
Private with events H_Zip As New Zipcontrol
Private MyDisp as New MyFormDisplay '// on form is a listview control
Public Sub Process
'// code sets up for ziping a file and does
H_Zip.Zip
End sub
Private Sub H_Zip_Progress(ByVal sender As Object, ByVal e As ZipControl.ProgressEventAr
'// It is here that I attempt to call a public sub in the form code to update the list view control
'// this results in the corss threading exception
MyDisp.filenameprop = "abcd" '// seems to be no problem to set property in form
MyDisp.UpdateList
end sub
In the code for the form I have tried the following:
Private Delegate sub UpdateMyListview_Delegate(
Public sub UpdateList()
IF invokerequired then
Dim x as new UpdateMyListView_Delegate(
x.invoke()
Else
'// do normal update of listview
end if
end sub
There are various other combinations I have tried but it either throws cross threading exeception of indicates there is no instance of an object (usually the zip control) when it returns to the other code.
Any assistance with understanding this process would be greatly appreciated. As I foresee running into this many times with the project I am doing.
Best regards, Dave Melnyk
ASKER
urban smurf:
Thanks for the reply .. your suggestion seems to indicate invoking a new copy of the event handler for the zip control, but I am already in that code and it is OK , I need to reference a control on a form that was created by the module code which also created the zip control. I'll give it a try, but this seems circular to me.
Thanks for the reply .. your suggestion seems to indicate invoking a new copy of the event handler for the zip control, but I am already in that code and it is OK , I need to reference a control on a form that was created by the module code which also created the zip control. I'll give it a try, but this seems circular to me.
ASKER
Just follow up to my last post, the "invokerquired" is not declared as I am in module code, so that suggestion doesn't exactly work in this case.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
urban smurf:
Just a quick update - your last post reminded me of some code in another form I was using doing the same thing. Set up what you suggested and still got an error! HOWEVER, in one of the other posts I was checking someone referred to a similar problem and that the problem wasn't really with the "invoke" call, but with the "other code" Changed my other code to something simple for the listview update (I was updating on a key) - used an index instead - and VOILA! it worked. (i.e. your suggestion.) then took out the setting for checking illegal cross threads and used the regular invoke call for a delegate:
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf Update_File_Stats))
Else
'//do the update stuff properly with index call
end if
and it now works properly! So it seems that the error that was being generated was not really a cross threading error in the sense of an improper invoke / delegate call structure. Many thanksfor your suggestion which tipped me to find the solution. Points are yours...
Best regards, Dave Melnyk
Just a quick update - your last post reminded me of some code in another form I was using doing the same thing. Set up what you suggested and still got an error! HOWEVER, in one of the other posts I was checking someone referred to a similar problem and that the problem wasn't really with the "invoke" call, but with the "other code" Changed my other code to something simple for the listview update (I was updating on a key) - used an index instead - and VOILA! it worked. (i.e. your suggestion.) then took out the setting for checking illegal cross threads and used the regular invoke call for a delegate:
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf Update_File_Stats))
Else
'//do the update stuff properly with index call
end if
and it now works properly! So it seems that the error that was being generated was not really a cross threading error in the sense of an improper invoke / delegate call structure. Many thanksfor your suggestion which tipped me to find the solution. Points are yours...
Best regards, Dave Melnyk
ASKER
See my final post for full explanation
Private Sub H_Zip_Progress(ByVal sender As Object, ByVal e As ZipControl.ProgressEventAr
If InvokeRequired Then
Invoke(New EventHandler(AddressOf H_Zip_Progress), sender, e)
Exit Sub
End If
'// update the list view control
MyDisp.filenameprop = "abcd" '// seems to be no problem to set property in form
MyDisp.UpdateList
end sub