Click here to Skip to main content
14,977,979 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,

i am trying to call some methods via threading. I am using it in asp.net.
methods are like:

C#
private void MethodName(string strParam1, out string strParam2,out string strParam3)

C#
private int MethodName(string strParam1,  string strParam2)


i have tried my best but not found any solution. can any give me some suggestion.

thanking you in anticipation.

warm regards,
Arshad Alam
Posted
Comments
[no name] 13-Jul-12 11:19am
   
"i have tried my best but not found any solution"... a solution for what? You have not described any sort of a problem yet.
arshad alam 14-Jul-12 2:21am
   
@Wes Aday, i am writing the following code in asp.net :
Thread thread1= new Thread(new ThreadStart(method1));
Thread thread2 = new Thread(new ThreadStart(method2));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();

i am getting compile time error

The easiest way to deal with threading is to use the BackgroundWorker. Look at the following:
http://www.dotnetperls.com/backgroundworker[^]
or
Basic Backgroundworker[^]
   
Kind of depends where you need your result. You could make use of a delegate, as the following example shows.
C#
class Program
{
   static void Main(string[] args)
   {
      Func<int,> theFunc = new Func<int,>(GetThreadId);
      theFunc.BeginInvoke(42, TheCallback, theFunc);
      Console.WriteLine("Working . . .");
      Console.WriteLine("Starting thread was " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
      Console.ReadKey();
   }

   static void TheCallback(IAsyncResult result)
   {
      Func<int,> theFunc = (Func<int,>)result.AsyncState;
      Console.WriteLine("The thread on which the function executed was " + theFunc.EndInvoke(result));
      Console.WriteLine("The thread on which this was printed was " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
      Console.WriteLine("Finished . . .");
   }

   static string GetThreadId(int justAnInt)
   {
      return System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + ", just an int " + justAnInt.ToString();
   }
}

Output:
Working . . .
Starting thread was 10
The thread on which the function executed was 11, just an int 42
The thread on which this was printed was 11
Finsihed . . .
As you see this method returns the result on a seperate thread as well. You could use an ISynchronizeInvoke[^] or something similiar to return to the calling thread though.

An alternative, as someone mentioned, could be a BackgroundWorker[^].
You could go hardcore with the quite heavyweight Thread Object[^].
Or go for the more lightweight ThreadPool[^].
Another option would be Tasks[^].
Sacha Barber actually wrote a nice series of articles on Tasks: Task Parallel Library: 1 of n[^].

Hope this helps.
   

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