A
A
anonymous2017-11-10 16:19:45
.NET
anonymous, 2017-11-10 16:19:45

How to correctly call ThreadPool and put a method in it?

There is a void type method that accepts a sheet

private void add(List<Data> Data)
        {
            var loaddata = new LoadData();
            loaddata.add(Data);
        }

I do like this through Task
void SomeMethod() {
      
            new Task(() =>
            {
                add(DataList);
            }).Start();
}

How can I do the same only through the ThreadPool?
Something somehow doesn't work

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Peter, 2017-11-10
@petermzg

All asynchronous operations where a Task is involved are performed in the ThreadPool.
ThreadPool - There can only be one per application.
"How can I do the same only through the ThreadPool?" - You are already using it.

M
marssx, 2017-11-10
@marssx

I haven't worked with it for a long time, but something like this should work
ThreadPool.QueueUserWorkItem(new WaitCallback(add), DataList);

L
LiptonOlolo, 2017-11-10
@LiptonOlolo

Use async-await constructs. Direct access to Thread & ThreadPoll is a bit deprecated with the release of .Net 4.5.

async void Add(List<Data> Data)
{
  await Task.Run(() => 
  {
    var loaddata = new LoadData();
    loaddata.add(Data);
  });
}

You can call it and it will work asynchronously. Or you can write Task instead of void (if nothing needs to be returned) and you can wait for the method to execute.
async Task Add(List<Data> Data)
{
  await Task.Run(() => 
  {
    var loaddata = new LoadData();
    loaddata.add(Data);
  });
}

And call:
await Add(new List<Data>());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question