I'm trying to use that method this way:
public void Method()
{
ThreadPool.QueueUserWorkItem(() =>
{
while(!paused)
{
ThreadPool.QueueUserWorkItem(() => {...);
}
});
}
}
The problem comes cause it throws me a compilation error in the first call.
error CS1593: Delegate
System.Threading.WaitCallback' does not take
0' arguments
Any idea of how to do it without arguments? , any alternative?
QueueUserWorkItem(WaitCallback, Object) Queues a method for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.
After using threading namespace we need to call threadpool class, using threadpool object we need to call method i.e. "QueueUserWorkItem" - which Queues function for an execution and a function executes when a thread becomes available from thread pool. ThreadPool.
The maximum allowed number of processing threads in a pool is 1023. The pool allocates a maximum of 1000 threads in an I/O operation. To get maximum number of threads, you can use the GetMaxThreads method of the ThreadPool static class. The first parameter passed to this method returns the number of processing threads.
WaitCallback represents a callback method that you want to execute on a ThreadPool thread. Create the delegate by passing your callback method to the WaitCallback constructor. Your method must have the signature shown here. Queue the method for execution by passing the WaitCallback delegate to ThreadPool.
You can just provide a parameter for the lambda expression, and ignore it:
ThreadPool.QueueUserWorkItem(ignored =>
{
while(!paused)
{
ThreadPool.QueueUserWorkItem(alsoIgnored => {...});
}
});
Or use an anonymous method instead:
ThreadPool.QueueUserWorkItem(delegate
{
while(!paused)
{
ThreadPool.QueueUserWorkItem(delegate {...});
}
});
If you don't care about parameters for anonymous methods, you don't have to state them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With