Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new thread, passing parameters

I want to create a thread and then pass parameters to it. But I don't know how.

Thread siteDownloader = new Thread(new ParameterizedThreadStart(GetHTML));

This is function that I want to launch as new thread.

static string GetHTML(string siteURL)
{
    WebClient webClient = new WebClient();

    try
    {
        string sitePrefix = siteURL.Substring(0, 7);
        if (sitePrefix != "http://")
        {
            siteURL = "http://" + siteURL;
        }
    }
    catch
    {
        siteURL = "http://" + siteURL;
    }

    try
    {
        return webClient.DownloadString(siteURL);
    }
    catch
    {
        return "404";
    }
}
like image 998
Stan Avatar asked Mar 28 '11 16:03

Stan


People also ask

Can we pass parameters to thread?

The first way we can send a parameter to a thread is simply providing it to our Runnable or Callable in their constructor. Note that the reason this works is that we've handed our class its state before launching the thread.

How do you pass a parameter to a thread function in C++?

The arguments to the thread function are moved or copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g., with std::ref or std::cref). Any return value from the function is ignored.

How do you create a new thread in Java?

Starting a Thread With a Runnable Runnable runnable = new MyRunnable(); // or an anonymous class, or lambda... Thread thread = new Thread(runnable); thread. start(); When the thread is started it will call the run() method of the MyRunnable instance instead of executing it's own run() method.


2 Answers

Personally I always use captured variables, I.e.

int a = ...
string b = ...
ThreadStart work = delegate {
    var result = DoSomethingInteresting(a, b);
    // push result somewhere; might involve a UI
    // thread switch
};
new Thread(work).Start();

This means it is always checked for correctness during build, unlike passing an object parameter.

like image 146
Marc Gravell Avatar answered Oct 18 '22 16:10

Marc Gravell


Citing the msdn:

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);

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

    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);
    }
}

so as you can see you create a method with an object as a parameter and pass this parameter to the Start method of the Thread class

like image 43
Rafal Spacjer Avatar answered Oct 18 '22 14:10

Rafal Spacjer