views:

1253

answers:

2

Is there any alternative method to System.Windows.Forms.Application.DoEvents() to flush the event queue which doesn't use the System.Windows.Forms namespace? If not, is it ok to use the above method/namespace within a web service.

+5  A: 

Application.DoEvents is used to make form elements redraw during processes that are preventing the form painting events to be raised, so I can't be used in a web service. In a web service you have no user interface, no windows and no message loop so DoEvents won't do anything.

Why do you think you need to use Application.DoEvents in a web service?

You are most likely looking for multithreading. You should be using threads if you need to do several things in parallel.

EDIT see ctackes post for more info

Nathan W
That's a bit oversimplified. It deals with a lot more than just painting events.
ctacke
Yeah I know it is but it was coffee time so I made it quick. :)
Nathan W
+4  A: 

As Nathan points out, DoEvents really just cycles the windows message pump, causing the top message on the queue to get translated and dispatched. In a Web Service it's unlikely that it's going to do anything (unless you're using Windows Messages for IPC or something wacky).

If you have a problem with a thread stealing too much scheduler quantum, you can call Thread.Sleep(0) which forces the calling thread to yield until the next scheduler tick, which may give your other, starved threads more opportunity to run, especially if they are at the same or lower priority.

Still, if you have starvation that this helps, it's fairly likely that you need to rework your code to prevent that in the first place rather than trying to just fix it via scheduler yielding.

ctacke
+1 I also like it!
Lucas McCoy