c#创建带参数的线程
1、无参数线程的创建
Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmessage() { Console.WriteLine("hello world"); }
Thread thread = new Thread(new ParameterizedThreadStart(showmessage)); string o = "hello"; thread.Start((object)o); private static void showmessage(object message) { string temp = (string)message; Console.WriteLine(message); }
public class ThreadTest { private string str1; private string str2; public ThreadTest(string a, string b) { str1 = a; str2 = b; } public void ThreadProc() { Console.WriteLine(str1 + str2); } } public class Example { public static void Main() { ThreadTest tt = new ThreadTest("hello ", "world"); Thread thread = new Thread(new ThreadStart(tt.ThreadProc)); thread.Start(); } }
4.将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。
具体代码如下(本示例来自MSDN)
using System; using System.Threading; //ThreadWithState 类里包含了将要执行的任务以及执行任务的方法 public class ThreadWithState { //要用到的属性,也就是我们要传递的参数 private string boilerplate; private int value; //包含参数的构造函数 public ThreadWithState(string text, int number) { boilerplate = text; value = number; } //要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用 public void ThreadProc() { //这里就是要执行的任务,本处只显示一下传入的参数 Console.WriteLine(boilerplate, value); } } //用来调用上面方法的类,是本例执行的入口 public class Example { public static void Main() { //实例化ThreadWithState类,为线程提供参数 ThreadWithState tws = new ThreadWithState( "This report displays the number {0}.", 42); // 创建执行任务的线程,并执行 Thread t = new Thread(new ThreadStart(tws.ThreadProc)); t.Start(); Console.WriteLine("Main thread does some work, then waits."); t.Join(); Console.WriteLine( "Independent task has completed; main thread ends."); } }
方法一:
在VS2003中,也不能直接访问,参看
一般来说,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软提供了Invoke的方法,其作用就是让子线程告诉窗体线程来完成相应的控件操作。
现在用一个用线程控制的进程条来说明,大致的步骤如下:
1.创建Invoke函数,大致如下:
///
/// Delegate function be invoked by main thread
///
private void InvokeFun()
{
if(prgBar.Value< 100)
prgBar.Value = prgBar.Value + 1;
}
2.子线程入口函数:
///
/// Thread function interface
///
private void ThreadFun()
{
// Create invoke method by specific function
MethodInvoker mi = new MethodInvoker(this.InvokeFun);
for(int i=0; i<100; i++)
{
this.BeginInvoke(mi);
Thread.Sleep(100);
}
}
3.创建子线程:
Thread thdProcess = new Thread(new ThreadStart(ThreadFun));
thdProcess.Start();
备注:
using System.Threading;
private System.Windows.Forms.ProgressBar prgBar;
方法二:
加入该句:Control.CheckForIllegalCrossThreadCalls = False 取消线线程安全保护模式!
方法三:带参数
使用类、类的方法或类的属性都可以向线程传递参数:
public class UrlDownloader
{
string url;
public UrlDownloader (string url)
{
this.url = url;
}
public void Download()
{
WebClient wc = new WebClient();
Console.WriteLine("Downloading " + url);
byte[] buffer = wc.DownloadData (url);
string download = Encoding.ASCII.GetString(buffer);
Console.WriteLine(download);
Console.WriteLine("Download successful.");
//这里你可以将download进行保存等处理......
}
}
[... 在另一个类中使用它们...]
UrlDownloader downloader = new UrlDownloader (yourUrl);
new Thread (new ThreadStart (downloader.Download)).Start();
注意参数是如何传递的。
方法四:带参数
ThreadStart starter = delegate { Download(yourUrl); };
new Thread(starter).Start();
//使用线程池
WaitCallback callback = delegate (object state) { Download ((string)state); };
ThreadPool.QueueUserWorkItem (callback, yourUrl);
方法五:带参数
Thread t = new Thread (new ParameterizedThreadStart(DownloadUrl));
t.Start (myUrl);
static void DownloadUrl(object url)
{
// ....
}