1

i have this piece of code:

 public void GenerateImageAsync(Area area)
    {
        ThreadPool.QueueUserWorkItem(threading  =>
        {
            ready.Reset();
            GenerateImage(area);
            ready.Set();
        });
    }

can you tell me how can i change it, or use alternative of Threadpool?

Giga
  • 98
  • 1
  • 2
  • 11

1 Answers1

1

You can either use a Task, which will create a thread in the threadpool like this:

public void GenerateImageAsync(Area area)
    {
        Task.Run(() => {
            ready.Reset();
            GenerateImage(area);
            ready.Set();
        });
    }

or a actual Thread like this:

public void GenerateImageAsync(Area area)
    {
        new Thread(() => {
            ready.Reset();
            GenerateImage(area);
            ready.Set();
        }).Start();
    }
clean_coding
  • 1,156
  • 1
  • 9
  • 14
  • How is any of them a replacement to ThreadPool? there is difference between thread and threadpool .http://stackoverflow.com/questions/230003/thread-vs-threadpool – lazy Jan 13 '16 at 19:07
  • @lazy What do you mean by *The thread is still not a managed thread?* – Sriram Sakthivel Jan 13 '16 at 19:08
  • 1
    Well a new Thread is an alternative to Thread Pool, I added the Task because I was unsure if OP meant the ThreadPool class, or actual thread pools. – clean_coding Jan 13 '16 at 19:09
  • I was not sure about the question but the alternate is ambiguous too. There is difference between thread and threadpool. Sorry for using the word managed. You can read more about the difference in the link in my comment – lazy Jan 13 '16 at 19:10