我想创建一个线程,然后将参数传递给它。但我不知道如何。
Thread siteDownloader = new Thread(new ParameterizedThreadStart(GetHTML));
这是我想作为新线程启动的功能。
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";
}
}
答案 0 :(得分:21)
我个人总是使用捕获的变量,即
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();
这意味着在构建期间始终检查它是否正确,与传递对象参数不同。
答案 1 :(得分:12)
引用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);
}
}
以便您可以看到创建一个以object
作为参数的方法,并将此参数传递给Start
类的Thread
方法
答案 2 :(得分:3)
拉法尔的解决方案可行,但我不确定你是如何从中得到你的回报价值的。正如Martinho指出的那样,ParameterizedThreadStart需要在方法上返回void。请尝试使用BackgroundWorker。
这样称呼它。
var bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync(url);
这是DoWork,基本上是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";
}
}
最后,在bw_RunWorkerCompleted
中执行类似的操作static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string result;
result = (string)e.Result;
//do something with result
}
答案 3 :(得分:1)
你看过ParametrizedThreadStart
的文档了吗?您需要一个采用对象参数并且不返回任何内容的方法。因此,您需要相应地更改方法签名:
static void GetHTML(object data)
{
string siteUrl = (string) data; // cast argument to string
// blah blah
}
然后你需要找出一种使用返回值的方法。