[英]Threading error in C#
Ok so i am using this line of code in project 好的,所以我在项目中使用这一行代码
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ProcessReport));
but it is throwing the following error 但它引发以下错误
No overload for the "ProcessReport" matches delegate “System.Threading.WaitCallback
“ ProcessReport”的重载不匹配委托“ System.Threading.WaitCallback”
I replace the above mentioned line with the following lines: 我将上述行替换为以下行:
Thread t = new Thread(new ThreadStart(ProcessReport));
t.Start();
This removed the error but is this the right way to do it. 这消除了错误,但这是正确的方法。 I cannot check the output right there as i am an intern in a company and this is part of the whole big project.
我不能在那里查看输出,因为我是一家公司的实习生,这是整个大型项目的一部分。 Please help.
请帮忙。
I cannot post the whole ProcessReport as i am not allowed to but it starts with this : 我不能发布整个ProcessReport,因为我不允许这样做,但是它以以下内容开头:
public void ProcessReport()
公共无效ProcessReport()
My assumption is that since this compiles: 我的假设是,既然这样编译:
new ThreadStart(ProcessReport)
Your ProcessReport
method looks something like this: 您的
ProcessReport
方法看起来像这样:
void ProcessReport()
{
}
QueueUserWorkItem
takes a WaitCallback
delegate, which requires passing a single object as the parameter. QueueUserWorkItem
带有一个WaitCallback
委托,该委托需要传递一个对象作为参数。 So change your method's signature to look like this: 因此,将方法的签名更改为如下所示:
void ProcessReport(object state)
{
}
And you should be OK. 而且你应该没事。 The
state
parameter you can ignore if you don't need to use it, but it's value is whatever you pass in as a second parameter of QueueUserWorkItem
. 如果不需要使用
state
参数,则可以忽略它,但是它的值是您作为QueueUserWorkItem
的第二个参数传入的值。 Since you are using the overload that doesn't pass an object into QueueUserWorkItem
, it will always be null. 由于使用的重载不会将对象传递给
QueueUserWorkItem
,因此它将始终为null。
One of the possible problems that could occur with 可能发生的问题之一
Thread t = new Thread(new ThreadStart(ProcessReport));
线程t =新线程(new ThreadStart(ProcessReport));
t.Start();
t.Start();
is that if you start so many threads in that way without checking for any conditions, your program is likely to crash or even the system in some cases. 就是如果您以这种方式启动了这么多线程而没有检查任何条件,则在某些情况下您的程序很可能崩溃甚至系统崩溃。 However if it's an app that an event has to occur before this thread is started, your code should be fine.
但是,如果是一个应用程序,必须在启动该线程之前发生事件,所以您的代码应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.