Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,
I am subscribed to an event
C#
public void Call_Comes(object sender, EventArgs e)
{
     //Code here
}


I don't want to do the tasks to be carried out inside this event, as it will take long time...
What I want is to collect the information and initiate the task, and exit the event...
How can I achieve this ?? I have used threading inside event.. But it seems that event is waiting for the thread to finish....
Can anybody help me ??
Posted
Updated 21-Oct-13 0:31am
v2

1 solution

There are many ways to decouple the event handler onto a different thread;

One of these might work for you;

C#
// Option 1
public void Call_Comes(object sender, EventArgs e) {
    Task.Factory.StartNew(() => HandleEvent(sender, e));
}

// Option 2
public void Call_Comes(object sender, EventArgs e) {
    ThreadPool.QueueUserWorkItem(x => HandleEvent(sender, e));
}

// Option 3
public void Call_Comes(object sender, EventArgs e) {
    var worker = new BackgroundWorker();
    worker.DoWork += HandleEvent;
    worker.RunWorkerAsync();
}

// Option 4
public void Call_Comes(object sender, EventArgs e)
{
    var thread = new Thread(() => HandleEvent(sender, e));
    thread.Start();
}

private void HandleEvent(object sender, EventArgs e) {
    // Your stuff
}


Note that if you do run it on a different thread you still need to handle any exceptions. You don't wan't uncaught exceptions on non-main threads.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Yesudasan Moses 21-Oct-13 6:58am    
Nothing works...
I have tried this on a fresh win project,,, there, everything works well...
But this particular library's events waits for any threads to finish inside this....
I don't know if they are using any locks for this...
Fredrik Bornander 21-Oct-13 7:14am    
I think it's unlikely that the library is waiting for any threads created in the event handler, that's a fairly complex and weird piece of functionality to add.

If you try something like this; what is printed to your output window:
<pre>
public void Call_Comes(object sender, EventArgs e) {
System.Diagnostics.Debug.WriteLine("Event fired from thread " + Thread.CurrentThread.ManagedThreadId);
Task.Factory.StartNew(() => HandleEvent(sender, e));
System.Diagnostics.Debug.WriteLine("Event processessing done");
}

private void HandleEvent(object sender, EventArgs e) {
System.Diagnostics.Debug.WriteLine("Handling on thread " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
System.Diagnostics.Debug.WriteLine("Done handling on thread " + Thread.CurrentThread.ManagedThreadId);
}
</pre>
Yesudasan Moses 21-Oct-13 7:59am    
Hi I tried ur code,,, This is the Output

Main Event fired from thread 18
Event processessing done
Handling on thread 18
Done handling on thread 18
Fredrik Bornander 21-Oct-13 8:08am    
So the event completes, the magically the payload that is supposed to run on a background thread runs on the same thread?

Are you installing a custom thread scheduler to the Task factory?
Yesudasan Moses 21-Oct-13 8:11am    
No,,, I am not doing anything,,,, Referencing a library (Asterisk.NET) and subscribing to its events... That event shows me this problem,,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900