Control.Invoke and multithreading

Z

Zanna

Hi all!

I got a little problem with multithreading.

I have a form with tree labels that create an instance of a class with a
new thread in it.
This new thread do some computation and then it raises an event of its
class that is trapped by the main form.

In this form I do this:


Private Delegate Sub SafeInvoker()

Public Sub Safe()
Me.Label1.Text = "ok"
Me.Label2.Text = "ok"
Me.Label3.Text = "ok"
End Sub


Private Sub MyThreadEvent(ByVal sender As Object, ByVal e As
EventArgs) Handles MyObj.MyThreadEvent
Dim del As New SafeInvoker(AddressOf Safe)
Me.Invoke(del)
End Sub


When I do Me.Invoke(del) I get ArgumentException.

Why?
How can I do it work?

Thanks
 
D

Daniel Moth

Control.Invoke accepts only the EventHandler delegate type in CF 1.0

Cheers
Daniel
 
Z

Zanna

Daniel Moth ha scritto:
Control.Invoke accepts only the EventHandler delegate type in CF 1.0

Oh!
Well, I changed my declarations to

Private Delegate Sub SafeInvoker(ByVal sender As Object, ByVal e As
EventArgs)

Public Sub Safe(ByVal sender As Object, ByVal e As EventArgs)

But I still have an ArgumentException on the Me.Invoke(del)

What is going wrong again?

Thanks again :)
 
D

Daniel Moth

Hi

You are still passing your own delegate to Control.Invoke which will not
work (just changing the signature is not enough)

1. Comment out your delegate declaration and any reference to it
2. Replace your Me.Invoke(del) with Me.Invoke(New EventHandler(AddressOf
Safe))

Cheers
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top