C#, Event Handlers and Threading - c#

I'm writing a little chat app, and I have this event handler:
void o_Typing(object sender, EventArgs e)
{
MessageBox.Show("Fired!");
this.Text = "Fired!";
}
o_Typing is a method in a class derived from TabPage. Basically, I want each conversation to have it's own tab.
The event handlers are fired by my Chat object, which is running in another thread. I have 1 thread for UI, and another thread for each Chat conversation (to keep polling the server for new data)
When the event is fired, the MessageBox pops up, but the Tab caption doesn't change. After the event has fired once, it never fires again, leading me to believe that the event is being called in the worker thread, although it is defined in the UI thread.
How can I get my events to be called from the worker thread, and use Invoke() to get them to execute on the UI thread?

There are two options:
1) Make the event handlers thread-safe: use Control.Invoke/BeginInvoke in any event handler which needs to talk to the UI thread.
2) Make the worker thread marshal back to the UI thread before raising the event - in other words, use Control.Invoke as part of the process of raising the event, so that the event handlers will all be called in the UI thread. Depending on how your app is structured, you may not want your event-raising component to know about the UI explicitly - but when it's being constructed you can pass in an ISynchronizeInvoke (which Control implements) and your component can use that to raise its events on the right thread. Of course, that only works (simply, anyway) if every event handler is happy to run on the same thread - but that will often be the case. You'd write something like:
protected void OnFoo(EventArgs args)
{
if (sync != null && sync.InvokeRequired)
{
sync.Invoke((Action) delegate { OnFoo(args) }, null);
return;
}
EventHandler handler = Foo; // Where Foo is the event name
if (handler != null)
{
handler (this, args);
}
}

If you fire your event in code which is executed by your worker thread, then all methods subscribed to the event will be executed under that worker thread.
For GUI-elements you need to look at the Invoke-methods.
Best Regards

Related

Datareceived handler vs TimerTick Event(Threaded and Non-Threaded Event)

I just want to know why TimerTick event handler is handled by the UI Thread and the DataReceived Event is handled by a worker thread. I thought they both are an event and handled by the UI thread. I tried Thread.Sleep(5000) when Data received event handler method is handled but the UI thread and form controls didn't freeze, yet when I tried the same inside timertick event handler, it blocks the UI thread and form control objects.
Is there a way to make timertick event work just like datareceived event (in a separate thread)?

Is there any way to get an event handler to run directly on UI thread without intermediate threadpool thread?

I have an event that is fired async via BeginInvoke - so the event handler gets its own threadpool thread. But all the code in the event handler wants to be dispatched to the UI thread - so the entire event handler code is in a Dispatcher.BeginInvoke block. This means:
Initiating thread fires event with BeginInvoke.
A new threadpool thread is created for the event handler code to run on.
The event handler immediately just posts its code async to the UI thread using
the dispatcher (Dispatcher.BeginInvoke(...all handler code...).
The temp threadpool thread returns quasi immediately - its created only to do a UI thread post.
Is there any way to restructure this to avoid the creation of this very short lived intermediate threadpool thread (I.e so the handler code just runs directly on the UI thread)?
I dont want the event to fire sycnronously (Invoke) - its used in various contexts - and it shouldn't be blocking.
Can you just call Dispatcher.BeginInvoke directly instead of using a threadpool thread?
If you don't control the code that fires the event you don't really have a choice; there is no way around the use of the thread pool thread for that short duration.
If you do control the code that fires the event you have the option of ensuring that the event handlers all run in the UI thread. While this is certainly possible, you should think long and hard about whether it should be the case. For certain event handlers it just makes sense for them to run in the UI thread (as an example, most of the events of Form run in the UI thread) and some it doesn't. If your event is on a UI control it probably makes sense for it to run in the UI thread. If it's an event of some worker class that you just happen to be using from a UI thread at the moment, it's probably a bad idea to fire the event in the UI thread (as you may, in the future, be using that worker context in a non-WPF context).
If you do want to fire the event in the UI thread it's simple enough. If you are already in the UI thread when you intend to fire the event just invoke it synchronously:
var eventCopy = MyEvent;
if(eventCopy != null) eventCopy();
If you're not in the UI thread when you wish to fire the event then marshal to the UI thread before calling the above code:
Dispatcher.BeginInvoke(()=>{ //Or just `Invoke`, if that's appropriate in context
var eventCopy = MyEvent;
if(eventCopy != null) eventCopy();
});
Based on your edit it seems you want to conditionally fire the event in the UI thread or a thread pool thread based on some specific context, rather than always firing in the UI thread.
While this is possible, you'll need to decide if it's worth it.
As an example you could look at System.Timers.Timer which has a SynchronizingObject property that allows you to determine how the events are rasied (null for the thread pool, or an object capable of marshalling to a particular context in the event of a particular UI model).
You could follow that general pattern.
There are a number of specific methods. You could capture the value in SynchronizationContext.Current at the time your worker thread was first created, and use that (possibly with a boolean value to disable capturing the source sync context if you might need to disable it, or forcably enable it).
Another option is to just have a property that accepts a SynchronizationContext, or some other mechanism of marshalling code to a given context (you could invent your own, use delegates, etc.).

Handling Background Events on the Main Thread

I create a background thread to do some work and, at the very end, I call a method ThreadDone(threadWorkResult) which raises an event. Currently, the event handler runs on the same background thread but I would like it to run on the main UI thread (Forms application). I searched online and found something about using attributes here but would like to know how to do this programatically. Is there any way it can be done outside the body of the handler itself?
I looked into BackgroundWorker but I have to create several threads at once so all of the respective OnWorkerCompleted event handlers become quite messy; more importantly, not all of them require the completed event. Worst case scenario I will use several BackgroundWorkers but is it possible for me to simply call a method (void aMethod()) from a background thread and force it to run on the primary UI thread?
There is a method called BeginInvoke on Windows Form controls which will execute code in the GUI thread.
I would recommend you to use BackgroundWorker thread for the background work and you can then easily handle the UI in the OnWorkerCompleted event.
Look at my answer here for more information.
Edit
You can use a delegate to hand over some tasks to MainUI thread.
public delegate void MyDelegate(object paramObject);
In the background thread call it as below.
private void aMethod(object myParam)
{
if (InvokeRequired)
{
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new MyDelegate(aMethod), new object());
return;
}
// Must be on the UI thread if we've got this far.
// Do UI updates here.
}
See here and here for references.

Do firing events in C# block the current thread execution?

If I'm firing the event:
var handler = OnMyEvent;
if (handler != null)
{
handler(some_info);
}
then will the execution thread wait until all suscriber methods return to continue the execution after line:
handler(some_info);
?
Or events are fired "in another thread", meaning that it automatically goes to the next line after handler(some_info)?
Events are fired on the same thread and it will block until they are completed. Of course the event handling code itself can spawn another thread and return immediately but this is completely different matter.
Also note that events like button clicks in a desktop applications like Windows Forms apps are put on a message queue and will fire one at a time. i.e. if you press a button and then press another button the second button event will not fire until the first is completed. Also the form will not repaint and will be "not responding" because painting the form is also an event.
Events are fired in the thread that raised them.

Is the event fired, when previous handler didn't finish?

Does SqlDependency (for example) fires the OnChange event, when the invocation of previous event handler didn't finish? (Assume that OnDependencyChange method is very time consuming)
What exactly happens?
SqlDependency dependency=new SqlDependency(command);
// Subscribe to the SqlDependency event.
dependency.OnChange += new
OnChangeEventHandler(OnDependencyChange);
I am not that familiar with the SqlDependency class, but per the MSDN documenation:
The OnChange event may be generated on
a different thread from the thread
that initiated command execution.
That seems to open up the possibility that two event handlers can run simultaneously. There is no documentation that states that all event handlers invoked from raising the event must complete before the event can be raised again. The safe thing to do is to assume multiple simultaneous invocations of the event can occur. Because the event is raised on an undefined thread you will have to guard against the problems of concurrency anyway.
In normal single threaded program not it will not. Try following:
Add windows forms app. Add a button to a form. Use following method to handle button's click event:
private void Buttonclick(object sender, EventArgs e)
{
Thread.Sleep(25);//SUSPEND CURRENT THREAD
}
you will not be able to click the button until the call to Thread.Sleep(25) finishes its work (suspend thread for 25 secs). You can test this in Console app also.

Categories

Resources