ExpectedC#MethodName我只是想传一些值,但总是报错。有人可以纠正我在这里缺少的东西吗?我在这里遇到错误Threadt_PerthOut=newThread(newThreadStart(ReadCentralOutQueue("test"));我想将此字符串值传递给ReadCentralOutQueue。classProgram{publicvoidMain(string[]args){Threadt_PerthOut=newThread(newThreadStart(ReadCentralOutQueue("test"));t_PerthOut.Start();}publicvoidReadCentralOutQueue(stringstrQueueName){System.Messaging.MessageQueuemq;System.Messaging.Messagemes;stringm;while(true){尝试{}else{Console.WriteLine("Waitingfor"+strQueueName+"Queue...");}}}catch{m="ExceptionOccured.";Console.WriteLine(m);}finally{//Console.ReadLine();}}}}此代码:Threadt_PerthOut=newThread(newThreadStart(ReadCentralOutQueue("test"));尝试调用ReadCentralOutQueue,然后根据结果创建一个委托。这不会起作用,因为它是一个void方法。通常你会使用方法组来创建委托,或者使用像lambda表达式这样的匿名函数。在这种情况下,lambda表达式将是最简单的:Threadt_PerthOut=newThread(()=>ReadCentralOutQueue("test"));您不能只使用newThread(ReadCentralOutQueue),因为ReadCentralOutQueue具有与ThreadStart或ParameterizedThreadStartMismatch相同的签名。了解出现此错误的原因以及解决方法很重要。编辑:为了证明它确实有效,这里有一个简短但完整的程序:usingSystem;使用系统线程;类程序{publicstaticvoidMain(string[]args){Threadthread=newThread(()=>ReadCentralOutQueue("test"));线程。开始();线程.加入();}publicstaticvoidReadCentralOutQueue(stringqueueName){Console.WriteLine("Iwouldreadqueue{0}here",queueName);}}参数不允许作为ThreadStart委托的一部分。还有其他几种将参数传递给新线程的解决方案,如下所述:http://www.yoda.arachsys.com/csharp/threads/parameters.shtml但在您的情况下,最简单的方法可能是匿名方法:ThreadStartstarter=委托{获取(myUrl);};新线程(启动器).Start();你必须这样做:varthread=newThread(ReadCentralOutQueue);thread.Start("测试");ParameterizedThreadStart还需要一个以对象为参数的委托,所以需要将签名改为:以上为C#学习教程:期待C#方法名分享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多指教...}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
