Use Case ThreadPool.QueueUserWorkItem

I am trying to use this method as follows:

public void Method()
{
        ThreadPool.QueueUserWorkItem(() =>
        {
            while(!paused)
            {
                ThreadPool.QueueUserWorkItem(() => {...);
            }
        });
    }
}

The problem arises because it causes a compilation error on the first call.

error CS1593: delegate System.Threading.WaitCallback' does not take 0 'arguments

Any idea how to do this without arguments ?, any alternative?

+5
source share
3 answers

You can simply specify a parameter for the lambda expression and ignore it:

ThreadPool.QueueUserWorkItem(ignored =>
{
    while(!paused)
    {
        ThreadPool.QueueUserWorkItem(alsoIgnored => {...});
    }
});

Or use an anonymous method:

ThreadPool.QueueUserWorkItem(delegate
{
    while(!paused)
    {
        ThreadPool.QueueUserWorkItem(delegate {...});
    }
});

If you do not need parameters for anonymous methods, you do not need to specify them.

+12
source

ThreadPool.QueueUserWorkItem System.Threading.WaitCallback. , . , :

ThreadPool.QueueUserWorkItem(_ =>
{
    //...
});
+2

The delegate you pass in must accept one argument. If you want to ignore it, you can simply replace the brackets with any variable name.

0
source

All Articles