我有这样的功能:
public void view(string msg)
{
messagebox.show(msg);
}
.
Run Code Online (Sandbox Code Playgroud)
.
我想从一个线程向它传递参数..我正在使用.Net 1.1.我怎样才能做到这一点?
对于.NET 1.1,没有直接的方法,但是使用具有方法和状态的对象,在此示例中(从MSDN示例修改),ThreadState类被实例化并传递所需的状态,然后调用其方法并且使用传递状态.
public class ThreadState {
private string msg;
// The constructor obtains the state information.
public ThreadState(string msg) {
this.msg = msg;
}
public void view() {
//Do something with msg
}
}
public class Example {
public static void Main() {
ThreadState ts = new ThreadState("Hello World!");
// Create a thread to execute the task, and then
// start the thread.
Thread t = new Thread(new ThreadStart(ts.view));
t.Start();
t.Join();
}
}
Run Code Online (Sandbox Code Playgroud)
对于.NET> 1.1(原始问题没有状态版本.)
您在Start方法中传递参数.您将收到一个需要回放到正确类型的对象.
Thread t = new Thread(view);
t.Start("Hello");
public void view(object msg)
{
string m = (string)msg;
//Use msg
}
Run Code Online (Sandbox Code Playgroud)
要么是这样,要么使用ParameterizedThreadStart委托.
| 归档时间: |
|
| 查看次数: |
3607 次 |
| 最近记录: |