如何创建具有多个参数的线程?

我有这个功能


static void appUpdater(string appName, string proccessName)

{

    /**/

}

我需要为每个应用程序更新创建一个线程,但 ThreadStart 和 ParameterizedThreadStart 与委托不匹配


Thread wms = new Thread(new ParameterizedThreadStart(appUpdater));

wms.Start("WMS","StuMenu");


呼唤远方
浏览 192回答 2
2回答

慕妹3146593

你不能。Thread.Start()只接受一个类型的参数object。但是您可以例如传递自定义对象或字典目的:class ThreadParameters {&nbsp; &nbsp; public string p1 {get;set;}&nbsp; &nbsp; public string p2 {get;set;}}static void appUpdater(object p) {&nbsp; &nbsp; ThreadParameters tp = p as ThreadParameters;}Thread wms = new Thread(new ParameterizedThreadStart(appUpdater));wms.Start(new ThreadParameters{p1="p1", p2= "p2"});字典:static void appUpdater(object p) {&nbsp; &nbsp; Dictionary<string, object> tp = p as Dictionary<string, object>;}Thread wms = new Thread(new ParameterizedThreadStart(appUpdater));wms.Start(new Dictionary<string, object>{{"p1", "p1"}, {"p2", "p2}});但根据您的需求,aTask可能是更好的方法。
打开App,查看更多内容
随时随地看视频慕课网APP