Hi freind,
I am facing problem in threading
eg:
int sum(int a, int b)
{
//complex code/
//time conusming code
return int;
}
Now I want to run this method through tread.. but here i have return type so confused how to call sum method in thread ?
Hi freind,
I am facing problem in threading
eg:
int sum(int a, int b)
{
//complex code/
//time conusming code
return int;
}
Now I want to run this method through tread.. but here i have return type so confused how to call sum method in thread ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mike GoldPosted Mar 21, 2007, 6:43 PM
Thinking again, an even cleaner way of doing the callback is through an asynchronous callback rather than an event handler. This way the sum thread isn't stuck waiting for the client calling it (potential deadlock)
Here is how to call the thread:
class Program
{
static void Main(string[] args)
{
MySumClass c = new MySumClass(new AsyncCallback(callback));
c.ProcessSum(10, 15);
Console.ReadLine();
}
static void callback(IAsyncResult result)
{
Console.WriteLine("The answer is {0}", result.AsyncState);
}
}
Here is the sum thread class solution that uses an asynchronous callback:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace
SumWithAsynchronousThread{
class MySumClass
{ private Thread _oThread = null;
private int _a;
private int _b; private int _sum; public ManualResetEvent ev = new ManualResetEvent(false);
private AsyncCallback _sumEventHandler = null;
public MySumClass(AsyncCallback sumHandler )
{ _a = 5;
_b = 6; _oThread = new Thread(new ThreadStart(Sum));
_oThread.Start(); _sumEventHandler = sumHandler;
} public void ProcessSum(int a, int b)
{
_a = a;
_b = b;
ev.Set(); // allow blocked thread to proceed
}
private void Sum(){
while (true)
{
ev.WaitOne();
_sum = _a + _b;
SumResult result = new SumResult(_sum);
_sumEventHandler.BeginInvoke(result, null, null);
ev.Reset();
}
}
}
}
public
class SumResult : IAsyncResult{
private int _sum = 0;
public SumResult(int sum)
{
_sum = sum;
}
#region
IAsyncResult Memberspublic object AsyncState
{
get { return _sum; }
} public WaitHandle AsyncWaitHandle
{
get { throw new System.Exception("The method or operation is not implemented."); }
} public bool CompletedSynchronously
{
get { throw new System.Exception("The method or operation is not implemented."); }
} public bool IsCompleted
{
get { throw new System.Exception("The method or operation is not implemented."); }
}
#endregion
}
Mike GoldPosted Mar 21, 2007, 5:22 PM
The question is why you would ever use a thread to do a sum, but lets assume the calculation is a bit more intense and you don't want to wait for it until it returns. This means you want to use the thread as an asynchronous method with a callback when the answer comes back.
The best way to deal with threads is to create a class that handles it. This means that the class will take the parameters (a and b), process them in a thread and then return the sum in a callback you pass in from another thread. Below is one strategy for doing this. It creates a thread that just continually loops processing sums with a wait trigger. The thread will sit and wait until it is signaled to process the next sum. When the sum is computed, it will call the eventhandler callback with the computed sum:
To use the class, just call the following lines of code:
MySumClass c = new MySumClass(new EventHandler(callback));
c.ProcessSum(10, 15);
The callback looks like this:
static void callback(object answer, EventArgs e)
{
Console.WriteLine("The answer is {0}", answer);
}
Here is the class that creates the looping thread and calls your callback after computing the sum:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace
TestSumThread{ class MySumClass
{
private Thread _oThread = null; private int _a; private int _b; private int _sum; public ManualResetEvent ev = new ManualResetEvent(false); private EventHandler _sumEventHandler = null; public MySumClass(EventHandler sumHandler ){
_a = 5; _b = 6; _oThread = new Thread(new ThreadStart(Sum)); _oThread.Start(); _sumEventHandler = sumHandler;}
public void ProcessSum(int a, int b){
_a = a; _b = b; ev.Set(); // allow blocked thread to proceed}
private void Sum(){
while (true){
ev.WaitOne(); _sum = _a + _b; _sumEventHandler(_sum, null); ev.Reset(); // block thread after computing sum and calling callback}
}
}
}