Hello everyone, i 'built' a simple windows forms application, with 2 buttons and 1 picturebox. One button is used for invoking an open file dialog, which prompts for a BMP from the harddisk, which is inserted on the picturebox.
The other button should do 'stuff' to the image, in a separate thread. I've done a similar app before, but now it's different: I have to pass arguments to the method to-be-contained by the thread.

//Method which should be contained by the thread.
void ConstructColors(int i, int j, Color col)
{
    //code
    //PS: Works recursively, just as a note.
}
//Event which should invoke ConstructColors:
private void AnalyzeButton_Click(object sender, EventArgs e)
{
       Thread mythread = new Thread (...).
}

I looked online for help, but couldn't find anything, i need to know how to pass arguments to the thread.
Also, i would like to be able to determine the main thread to wait for the second, until it's finished its work.
Thanks!

Recommended Answers

All 6 Replies

bump?

>i need to know how to pass arguments to the thread.

System.Threading.ParameterizedThreadStart delegate.

Yes, i already tried that, but i need an example !
I already read that MSDN link before asking here, that particular example requires you to create a new instance of your class. I want to invoke the method without having to create a new instance :(

Have a look,

...
 private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.ParameterizedThreadStart pthread = new System.Threading.ParameterizedThreadStart(Print);
            System.Threading.Thread th1 = new System.Threading.Thread(pthread);
            th1.Start(this);

            th1.Join();
            MessageBox.Show("Finished");
        }
       
        public  void Print(object data)
        {
            Form1 r = data as Form1;
            r.ConstructColors(10, 10, Color.Red);
             
        }
        void ConstructColors(int i, int j, Color col)
        {

        }
..

the MSDN page has an example. You can send the parameterised thread delegate an object containing your values. Sicne object is the base class of all classes you can send it any type of variable you like.
If you need to pass it more than one variable then encapsulate them into a type that can hold them (eg struct/collection/class).

Adapted from MSDN example:

using System;
    using System.Threading;

    public class Work
    {
        public static void Main()
        {
            // To start a thread using a shared thread procedure, use
            // the class name and method name when you create the 
            // ParameterizedThreadStart delegate. C# infers the 
            // appropriate delegate creation syntax:
            //    new ParameterizedThreadStart(Work.DoWork)
            //
            Thread newThread = new Thread(Work.DoWork);

            // Use the overload of the Start method that has a
            // parameter of type Object. You can create an object that
            // contains several pieces of data, or you can pass any 
            // reference type or value type. The following code passes
            // the integer value 42.
            //
            newThread.Start(42);

            // To start a thread using an instance method for the thread 
            // procedure, use the instance variable and method name when 
            // you create the ParameterizedThreadStart delegate. C# infers 
            // the appropriate delegate creation syntax:
            //    new ParameterizedThreadStart(w.DoMoreWork)
            //
            Work w = new Work();
            newThread = new Thread(w.DoMoreWork);

            //Construct object for thread
            //
            ThreadValues values = new ThreadValues();
            values.i = 1;
            values.j = 1;
            values.s = "input";


            // Pass an object containing data for the thread.
            //
            newThread.Start(values);
        }

        public static void DoWork(object data)
        {
            Console.WriteLine("Static thread procedure. Data='{0}'",
                data);
        }

        public void DoMoreWork(object data)
        {
            Console.WriteLine("Instance thread procedure. Data='{0}'",
                data);
        }
    }

    public struct ThreadValues
    {
        public int i {get;set;}
        public int j { get; set; }
        public string s { get; set; }
    }

Hey dude i also had that requirement to pass a value to other thread
What i did was i prepared a separate class for the destination thread and then called the destination thread with parameters in the source thread's class.
Thats the only way how u can pass parameters to threads
Example:
I first wrote a container class

public class ThreadContainer
{
.....
//Here i have my InsertData function
}

i called that thread as follows in another class and pass a parameter 'decoded'

public class Form1
{
.......

ThreadContainer objContainer = new ThreadContainer(decoded);
                        Thread objThread = new Thread(new ThreadStart(objContainer.InsertData));
                        objThread.Start();

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.