Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi I am new to c# win form application

I want to call more then one method in background worker for threding in c# winform application

I exatly require result same as threding in c++ or java

Can any one explain me overall process for that
Posted

1 solution

you can work around ThreadPool[^]

for example

first of all you need to create static method/function which is actually called in threading, like this:-

C#
// This thread procedure performs the task.
    public static void ThreadProc(Object stateInfo) {
        Console.WriteLine("Hello from the thread pool.");
    }

and call it like this...
C#
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

for passing value to your method you can do something like this:-
C#
public class ThreadPara{
   public int a{get;set;}
   public int b{get;set;}
}

public static void ThreadSum(Object stateInfo) {
    ThreadPara data = stateInfo as ThreadPara;
    int sum = data.a + data.b;
    Console.WriteLine("Sum is :" + sum);
}
public static void ThreadMul(Object stateInfo) {
    ThreadPara data = stateInfo as ThreadPara;
    int mul = data.a * data.b;
    Console.WriteLine("Mul is :" + mul);
}

and call it in button click like this..
C#
protected void btn1_Click(object sender, EventArgs e)
{
    ThreadPara data = new ThreadPara();
    data.a = 10;
    data.b = 20;
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadSum),data);
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMul),data);
}
 
Share this answer
 
v3

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