简体   繁体   English

ThreadPool.QueueUserWorkItem在c#2.0中的单独线程中运行方法时出错

[英]ThreadPool.QueueUserWorkItem Error when run method in separate thread in c# 2.0

i got a code to invoke any method through thread. 我有一个代码可以通过线程调用任何方法。

System.Threading.ThreadPool.QueueUserWorkItem(Export());

here i tried to run Export() method through thread and got compilation error. 在这里,我尝试通过线程运行Export()方法,并得到编译错误。 what is wrong in the code. 代码有什么问题。 i am using c# 2.0 version. 我正在使用C#2.0版本。 please help. 请帮忙。

QueueUserWorkItem takes a delegate, but you're calling the Export method and then trying to pass the result of Export to the QueueUserWorkItem method. QueueUserWorkItem需要一个委托,但是您正在调用Export方法,然后尝试将Export结果传递给QueueUserWorkItem方法。 In other words, it's as if you're running: 换句话说,就好像您正在跑步:

var result = Export();
ThreadPool.QueueUserWorkItem(result);

That's clearly not going to be running Export in another thread... 显然,这不会在另一个线程中运行Export ...

Assuming the signature of the Export method is correct, you just need to change it from a method invocation to a method group conversion: 假设Export方法的签名正确,则只需将其从方法调用更改为方法组转换:

ThreadPool.QueueUserWorkItem(Export);

EDIT: If you need to give arguments to the Export method, the simplest way of doing this is to use a lambda expression (assuming you're using C# 3). 编辑:如果需要给Export方法提供参数,最简单的方法是使用lambda表达式(假设您使用的是C#3)。 For example: 例如:

ThreadPool.QueueUserWorkItem(state => Export(filename));

Does method Export return a delegate of type WaitCallback ? 方法Export是否返回类型为WaitCallback的委托? If not then you're supplying the wrong parameter to QueueUserWorkItem . 如果不是,那么您正在为QueueUserWorkItem提供错误的参数。 To be clear, you need to supply a delegate that conforms to the following signature: 为了清楚起见,您需要提供符合以下签名的委托:

public delegate void WaitCallback(
    Object state
)

So, method Export should have the following signature: 因此,方法Export应该具有以下签名:

public void Export(object state)

and be supplied to QueueUserWorkItem as follows: 并按以下方式提供给QueueUserWorkItem

ThreadPool.QueueUserWorkItem(Export)

or, if Export has a different signature then you could use an intermediate delegate to call it: 或者,如果Export的签名不同,则可以使用中间委托来调用它:

ThreadPool.QueueUserWorkItem(state=>Export())

or if Export needs parameters, you could: 或者,如果Export需要参数,则可以:

ThreadPool.QueueUserWorkItem(state=>Export(some,parameters))

You probably have to write ThreadPool.QueueUserWorkItem(new WaitCallback(Export)) and modifying export to have a single parameter of type object, ie: 您可能必须编写ThreadPool.QueueUserWorkItem(new WaitCallback(Export))并修改export以具有object类型的单个参数,即:

Export(object state)
{
......

}

the state is a parameter you can pass-trought to the callback function when you call QueueUserWorkItem() 状态是一个参数,您可以在调用QueueUserWorkItem()时将其传递给回调函数

Providing Export matches the delegate that is to be supplied into QueueUserWorkItem do: 提供Export匹配要提供给QueueUserWorkItem的委托,请执行以下操作:

ThreadPool.QueueUserWorkItem(Export);

By writing Export() with parentheses you are calling the method instead of passing to QueueUserWorkItem to be called on a separate thread. 通过用括号编写Export() ,您将调用该方法,而不是传递给QueueUserWorkItem在单独的线程上被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM