string类型的返回值~
2 回答
莫回无
TA贡献1865条经验 获得超7个赞
public class A{ public string Test() => null; public void Main() { //方式1:TAP 异步模式 Task<string> task = Task.Run(new Func<string>(Test)); //做些别的事情...然后获取结果 task.Wait(); string result1 = task.Result; //方式2:APM 异步模式 Func<string> func = new Func<string>(Test); IAsyncResult asyncResult = func.BeginInvoke(null, null); //做些别的事情...然后获取结果 string result2 = func.EndInvoke(asyncResult); //方式3:线程 using (ManualResetEventSlim finishEvent = new ManualResetEventSlim(true)) { string tempResult = null; var thread = new Thread(() => { tempResult = Test(); finishEvent.Set(); }); thread.Start(); //做些别的事情...然后获取结果 finishEvent.Wait(); string result3 = tempResult; } }} |
SMILET
TA贡献1796条经验 获得超4个赞
class Params // 线程函数参数类型 { public int inparam = 0; // 表示一个传入的值 public int outparam = 0; // 表示一个返回的值 }
var pm = new Params { inparam = 1 }; var thread = new Thread(param => { ((Params)param).outparam = 10; Thread.Sleep(2000); }); thread.Start(pm); thread.Join(); MessageBox.Show("传出参数的值是 " + pm.outparam.ToString());
- 2 回答
- 0 关注
- 344 浏览
添加回答
举报
0/150
提交
取消
