Stan
Stan

Reputation: 26511

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

Upvotes: 11

Views: 50118

Answers (4)

Marc Gravell
Marc Gravell

Reputation: 1064324

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.

Upvotes: 22

cadrell0
cadrell0

Reputation: 17337

Rafal's solution will work, but I'm not sure how you get your return value from that. As Martinho points out, ParameterizedThreadStart requires a void return on the method. Try using a BackgroundWorker instead.

Call it like this.

var bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync(url);

Here is DoWork, basically a modified version of GetHTML.

static void bw_DoWork(object sender, DoWorkEventArgs e)
{
    WebClient webClient = new WebClient();
    string siteURL = (string)e.Argument;

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

    try
    {
        e.Result = webClient.DownloadString(siteURL);
    }
    catch
    {
        e.Result = "404";
    }
}

Finally, do something like this in bw_RunWorkerCompleted

static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    string result;
    result = (string)e.Result;
    //do something with result
}

Upvotes: 3

Rafal Spacjer
Rafal Spacjer

Reputation: 4918

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

Upvotes: 12

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234674

Did you look at the docs for ParametrizedThreadStart? You need a method that takes an object parameter and returns nothing. So, you'll need to change your method signature accordingly:

static void GetHTML(object data)
{
    string siteUrl = (string) data; // cast argument to string
    // blah blah
}

And then you need to figure out a way of using the return value.

Upvotes: 1

Related Questions