2

In a WPF app I have a pool of threads (each thread is a call to a REST webservice returning base64 image) that I'd like to "control" much precisely.

These threads are async and I'd like to be able to suspend/resume each of them.

  • Use something like a CancellationToken is not an option I guess because each thread is doing just a call and waiting for the answer.

  • I just saw that in the class Thread => Suspend & Restart are obsolete/deprecated.

So if someone has any suggestion?

Thanks by advance.

Benoît
  • 87
  • 8
  • `thread is doing just a call and waiting for the answer.` doesnt that mean it doesn't use processing time while waiting? so why suspend it? – x4rf41 May 07 '16 at 07:38
  • Actually I run first several threads (10, 20 it depends) in charge to get images asynchronously. These demands are not urgent. Sometimes I run another task very urgent: unfortunatly this one is slow. I cannot put priority among tasks in saying: this one is not urgent, this other one is urgent. I cannot manage the dispatching among tasks... It is the reason I am interested in suspend/Restart non-urgent tasks... – Benoît May 08 '16 at 19:45

1 Answers1

1

So the type of control you are looking for with your threads isn't exactly feasable because of the nature of threads. Thread execution is queued up by the OS and it's scheduler. This means that you have no control as to when the thread will be actively running. However, fibers are similar to threads with the difference that the application is responsible for scheduling and switching threads. Fibers are idealized as co-routines in mono and the win32 library has some support for them. The .net framework doesn't provide much support for it though.

Some people here in the community have been asking about fibers but their searches have been mostly fruitless. SO post

This answer may lead you in a direction that may help you on your journeys. It mentions creating you own scheduler for the parallel task library.

I didn't explore the concept much as i haven't had the need for it yet, but the yield return program design seems to be an alternative.

Community
  • 1
  • 1
xtreampb
  • 528
  • 6
  • 19