How do I make GUI events yield to a given thread? By GUI events I mean things like opening a sub-form, selecting different tabs, or minimizing/maximizing my form or changing focus.
When my working thread gets data, can I throttle resources over to it, and deny those resources from other processes or threads in my application?
History:
In my question Here I explain a scenario where I am concerned with my Invoke to my main form causing hang-ups on my critical threads. It was then revealed to me that I have the option to “BeginInvoke”.
Now I find that while BeginInvoke gets me half-way there, I’m still unable to avoid hang-ups in my critical threads. When I say “critical threads” I am refering to a thread that takes approximately 60ms to cycle, but must finish cycling before 130ms has elapsed. It sits there looping, polling a data list. When the data shows up, it generally takes between 40 and 60ms to finish processing.
I had (and technically, I plan to reinstate) a second thread throwing BeginInvokes at my form to update the user on the status of the run. I currently, for debugging purposes, am not doing that.
I did some profiling, and I noticed that I almost always keep my process time down far below 100ms, which I need to be, because my application needs to output results no later than when the next package of data appears (every 100ms, but for this example I slowed that to 130ms). When I interact with my form, by say changing focus, or minimizing/maximizing it, I can see my process time spike above 1s. I assume this is because silly things like minimizing or maximizing, or other re-draw events, are plundering all my precious clock time.
Platform:
I am using Visual C# 2008 with WinForms. The target machine runs Windows XP. I have attempted closing Windows Explorer, and running my application on Normal, High, and Realtime, priority, but I still see a nearly identical impact when manipulating my form.
Edit
I have tried to use Thread.CurrentThread.Priority to throttle my priority at critical times during my worker loop. This somehow isn’t able to take those resources away from my GUI and operations that involve redrawing the GUI still rule over my poor worker thread.
Did you try and set the Thread Priority of your worker class to RealTime using the Thread.Priority property?
Based on the description of Scheduling Priorities this should give a higher priority to your worker thread. However, this will cause your GUI to stutter along, so you probably only want to set the priority high during critical time periods.