Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Article

Send multiple parameters to a thread in C#

Rate me:
Please Sign up or sign in to vote.
2.42/5 (23 votes)
12 Feb 2008CPOL3 min read 182.8K   1.2K   26   24
This article explains how to create a C# thread that takes a function method with multiple input parameters. Also explains how do we get a return value in this scinario.

Introduction

Have you wondered how you can send a parameter to a C# thread? Have you thought of using anonymous method calls and outer-variable semantics when you put a function in C# thread? If these questions interests you then you are at a right place to read the article further.

Background

I had a simple scinario of creating a C# thread then send a function that takes multiple input parameters and output a single value. I have been searching over net to find a help on that but could not find many. However, I have figured out a way to do that and thought of sharing the idea with other.

Using the code

Here is a sample code to create C# thread then send-in a delegate to the thread that casts a normal C# function and takes two input parameters. We also return an output from the same function when the thread execution is complete.

This sample has 3 simple C# projects.

PROJECT1: First project is a C# class library and has a one class inside. This class has a single function which takes two int input. The logic for this function is to multiply 'A' with 'N' times where 'A' = First input parameter and 'N' = second input parameter. Then it returns the calculated value to the caller. Following is a code snippet for that.

public class Thread1 
{ 
    public double Multiply(int lOpr1, int lOpr2) 
    { 
        double retVal = 1; 
        for (int i = 1; i <= lOpr2; i++) 
        { 
            retVal *= lOpr1; 
        } 
        return retVal; 
    } 
}         

PROJECT2: This project is a C# class library and also has a single class with a single function that takes two int inputs. The logic for this function is to add 'A' with 'N' times where 'A' and 'N' are input parameters to this function. This function also returns the calculated value to the caller. Following is a code snippet for that.

public class Thread2 
    { 
    public double Add(int lOpr1, int lOpr2) 
    { 
        double retVal = 0; 
        for (int i = 1; i <= lOpr2; i++) 
        { 
            retVal = retVal + lOpr1; 
        } 
        return retVal; 
    } 
} 

PROJECT3: This is a windows forms project with three text box and a button. On the button click event we will write a code to create two threads to call the above two class' that are to be executed within its own thread. (huh.. catching up with multithread). Following are the steps to do:

1. Declare two local variable and get the user input values in it.
2. Declare local variables to receive output value
3. Create an instance of a class from Project1
4. Create first thread thereby cast the above instance as a anonymous method to a type ‘delegate’. We assign the local variables created in step 1 as inputs to the anonymous method. This technique is called outter-variable semantics. We also assign a local variable to receive the return value - all in one step.
5. Start the first thread
6. Repeate step 3 to 5 to create second thread for the class from Project2
7. This step is to check whether both threads are done with the execution. If so write the output values to a text box.

Block of code for the button click event:

using System;
using System.Threading;
namespace MainThread
{
   public partial class AppOne
   {
   private void btnMultiThread_Click(object sender, EventArgs e)
    {
    //1
    int lOpr1 = Int32.Parse(txtInput1.Text);
    int lOpr2 = Int32.Parse(txtInput2.Text);
    //2
    double getmul = 0;
    double getadd = 0;
    //3
    Thread1 ClsMultiply = new Thread1();
    //4
    Thread MulThread = new Thread(delegate()
        {
        getmul = ClsMultiply.Multiply(lOpr1, lOpr2);
        });
    //5
    MulThread.Start();
    //6
    Thread2 ClsAdd = new Thread2();
    Thread AddThread = new Thread(delegate()
        { 
        getadd = ClsAdd.Add(lOpr1, lOpr2);
        });
    AddThread.Start(); 
    //7
    while (MulThread.IsAlive || AddThread.IsAlive)
         Thread.Sleep(1);
 
    txtOutput.Text = "Addition of " + txtInput2.Text + " Times of " + txtInput1.Text + " = " + getadd 
        + " ||| Multiplication of " + txtInput2.Text + " times of " + txtInput1.Text + " = " + getmul;  
    } 
   } 
} 

Remember to notice that, we can send any number of input parameters to a called method using this technique. Hope, you find this informative and simple to understand.

History

..

License

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


Written By
India India
Pasupathi Narayanan has been professionally programming for 18 years in IT using varied technologies primarily using Microsoft technologies and Mainframes.

Pasupathi Narayanan started using C# in 2002 and is a certified MCP, MCAD and PMP.

Comments and Discussions

 
GeneralVery Much Thanks To You Pin
CHARTVETTI23-Jun-13 22:09
CHARTVETTI23-Jun-13 22:09 
QuestionInteresting. Pin
Hua Yujun13-Dec-12 4:05
Hua Yujun13-Dec-12 4:05 
Questioncan u explain how to use progress bar here Pin
explorerC28-Jul-11 18:14
explorerC28-Jul-11 18:14 
GeneralMy vote of 4 Pin
Riquemion24-Aug-10 20:35
Riquemion24-Aug-10 20:35 
Generalstart a thread at a precision time Pin
sckipp17-Feb-10 21:21
sckipp17-Feb-10 21:21 
QuestionHow to identify a particular thread? Pin
mafaz3218-Aug-09 2:58
professionalmafaz3218-Aug-09 2:58 
GeneralPerformance issue in wait loop Pin
AndyHo12-Feb-08 4:11
professionalAndyHo12-Feb-08 4:11 
GeneralRe: Performance issue in wait loop Pin
Pasupathi Narayanan12-Feb-08 17:23
Pasupathi Narayanan12-Feb-08 17:23 
GeneralDidn't search very hard Pin
Gates VP11-Feb-08 19:24
Gates VP11-Feb-08 19:24 
GeneralRe: Didn't search very hard Pin
Pasupathi Narayanan12-Feb-08 16:46
Pasupathi Narayanan12-Feb-08 16:46 
GeneralRe: Didn't search very hard PinPopular
MaxGuernsey12-Feb-08 17:23
MaxGuernsey12-Feb-08 17:23 
Pasupathi Narayanan wrote:
I disagree to use a Join() as it would only allow threads to execute one after another.


You said this earlier. It is still not true.

<br />
Thread thread1 = GetThread1();<br />
Thread thread2 = GetThread2();<br />
<br />
// at this point, neither thread is running<br />
<br />
thread1.Start();<br />
<br />
// now thread1 is running and thread2 is not<br />
<br />
thread2.Start();<br />
<br />
// at this point, both threads are running<br />
<br />
thread1.Join();<br />
<br />
// now thread1 is done running and thread2 may or may not be running<br />
<br />
thread2.Join();<br />
<br />
// now both thread1 and thread2 are done<br />


Does that not address your concern with Join?

Max Guernsey, III
Managing Member, Hexagon Software LLC
http://www.hexsw.com
http://www.dataconstructor.com

GeneralRe: Didn't search very hard Pin
Gates VP13-Feb-08 4:28
Gates VP13-Feb-08 4:28 
GeneralRe: Didn't search very hard Pin
Pasupathi Narayanan13-Feb-08 19:35
Pasupathi Narayanan13-Feb-08 19:35 
GeneralRe: Didn't search very hard Pin
Gates VP13-Feb-08 21:00
Gates VP13-Feb-08 21:00 
GeneralYou might want to rethink this Pin
MaxGuernsey11-Feb-08 12:51
MaxGuernsey11-Feb-08 12:51 
GeneralRe: You might want to rethink this Pin
Pasupathi Narayanan11-Feb-08 17:41
Pasupathi Narayanan11-Feb-08 17:41 
GeneralRe: You might want to rethink this Pin
MaxGuernsey11-Feb-08 18:48
MaxGuernsey11-Feb-08 18:48 
GeneralRe: You might want to rethink this Pin
Pasupathi Narayanan12-Feb-08 17:15
Pasupathi Narayanan12-Feb-08 17:15 
GeneralRe: You might want to rethink this Pin
MaxGuernsey12-Feb-08 17:24
MaxGuernsey12-Feb-08 17:24 
GeneralYuck! PinPopular
mav.northwind7-Feb-08 2:42
mav.northwind7-Feb-08 2:42 
AnswerRe: Yuck! Pin
Pasupathi Narayanan7-Feb-08 16:48
Pasupathi Narayanan7-Feb-08 16:48 
GeneralRe: Yuck! Pin
mav.northwind7-Feb-08 21:21
mav.northwind7-Feb-08 21:21 
GeneralRe: Yuck! Pin
Pasupathi Narayanan11-Feb-08 16:26
Pasupathi Narayanan11-Feb-08 16:26 
GeneralRe: Yuck! Pin
i.Posei12-Feb-08 19:30
i.Posei12-Feb-08 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.