Caioshin

Hi to all,
I'm trying to understand well the paradigm of Event and Delegates, but I've some problem that I can't explain to myself.
I've tried to write a little piece of code: in one class (the publisher) I declare a delegate and an public event, so someone can subscribe to it.
In another class (the subscriber) I create an instance of the publisher class, and add its method to be executed when the event is raised.
If I do this using a Windows Forms, when the event is raised up, I receive the message that I'm using Cross-Threading operation that is not a good think (I know how to disable the message, but is not what I want).

Re: Windows Forms General Doubt about Event and Delegate in multithread context

Mark Dawson

Hi Caioshin,

the problem you are having is that when the event is raised, you eventually end up accessing the text box from a different thread than the one that created it, this is not allowed. This is since the System.Timers.Timer uses a thread from the ThreadPool to execute the elapsed method, which is different from the main UI thread. What you can do is to use a System.Windows.Forms.Timer which will use the main UI thread to executed the Tick event so there will be no cross thread issue, or inside the EventHandler method you can do this:

private void EventHandler

{

if(textBox1.InvokeRequired)

{

//call this method on the correct thread.

textBox1.Invoke(new MethodInvoker(EventHandler));

}

else

{

textBox1.Text = " hi there";

}

}

HTH

Mark.






Re: Windows Forms General Doubt about Event and Delegate in multithread context

Caioshin

Hi Mark Dawson, and thank you for your reply.

I know that the problem to textbox is caused by an access from a different thread than its creator, but I can't solve this using the System.Windows.Forms.Timer, because in other situations in example I needed to modify a form from a different thread, that isn't a timer.

Your example works greatly, thank you very much.

Another curiosity...instead of create my delegate and an event related to this delegate:


public delegate void MyDelegate();
public event MyDelegate somethingHappens;

can't I use directly an EventHandler event in this way (I've tried but didn't work because EventHandler doesn't take 0 arguments)


public event EventHandler somethingHappens;


Thank you





Re: Windows Forms General Doubt about Event and Delegate in multithread context

Mark Dawson

If you want to have zero parameters in your delegate, you can use the System.Windows.Forms.MethodInvoker delegate type, which has zero parameters. The EventHandler delegate as you say requires parameters of the signature EventHandler(Object sender, EventArgs e), so if you want to use this delegate you will have to update your method signature to match.

On a side note, before raising an event you should always check that the event has some subscribed listeners by checking for null, otherwise you might get a NullReferenceException when you try to raise the event i.e.

if(MyEvent != null)

{

MyEvent();

}






Re: Windows Forms General Doubt about Event and Delegate in multithread context

Caioshin

I've tried to update the method signature so it can match with the EventHandler signature, but seems to not work; however the previous solution is right for me, this is only a curiosity.

Thank you for all!