Must disable context switch in VB.NET 2005

I have a function that, when called, cannot be interrupted by ANYTHING.  After much frustration, I figured out that the reason it was having problems is that other time slices were interrupting it (I found I kept loosing 16ms here and there, but at different points and eventually isolated it to this).

How do I make it so that, from when the function starts to when the function ends, NOTHING interrupts it.  In case it matters, this function calls other functions and communicates with a database.  

I need a solution ASAP, so any help would be greatly appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jens FiedererTest Developer/ValidatorCommented:
If you set your processpriority to RealTime, you will not be preempted by any other threads.

You will also with high probability lock up your computer, since these other functions and database communications are in all likelihood DEPENDENT on other tasks running!

"CAUTION   The threads of a process with RealTime priority preempt the threads of all other processes, including operating system processes performing important tasks. Thus, a RealTime priority process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive."
Jens FiedererTest Developer/ValidatorCommented:
Generally, when you need to do Realtime programming, you try to restrict the realtime part to as short as possible a section of code that does NOT require action from the outside.
stev0931Author Commented:
I figured this might be the case, so I just spent some time to have the information needed from external interfaces cached before the function runs.  It is now to the point to where only local functions are called - no DBMS communication or anything.  

So, is there a way to have JUST this one function execute in real time?  (The idea being that the functions it calls will also run in real time, but if these other functions are called from somewhere else, they just run normal)
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Jens FiedererTest Developer/ValidatorCommented:
At the beginning of the function, IF YOU HAVE PERMISSION, you should be able to do a

dim ps as process = Process.GetCurrentProcess
ps.PriorityClass = ProcessPriorityClass.RealTime

and unset it at the end of the function with

ps.PriorityClass = ProcessPriorityClass.Normal

stev0931Author Commented:
Thanks for all your help - this looks promising, but the interruptions that would occur to the function occur inside the same VERY large program.  The program runs as a single process and as many threads.  Is there a thread version of the code above?  If so, I should be able to get it work.
Jens FiedererTest Developer/ValidatorCommented:
Thread priority is subordinate to process priority - behavior is o/s dependent, but generally you cannot count on a thread to be realtime if its process is not..."Threads with different priorities that are running in the process run relative to the priority class of the process."

You might want to run this in a subprocess with its own priority.
stev0931Author Commented:
The thread only has to be real time as far as the program is concerned.  (the only thing that must be ensured is that no other code in the program executes while this function with its own thread executes)

It can be assumed that the OS will always be Windows XP Pro or Windows Server 2003
Jens FiedererTest Developer/ValidatorCommented:
That is totally different!  Then you don't want to monkey with Process priority at all.

have you tried just

Thread.CurrentThread.Priority = ThreadPriority.Highest

Of course, none of the other threads would be allowed to do that.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stev0931Author Commented:
Yes, but it doesn't do the trick.  Any other thoughts?
stev0931Author Commented:
Actually, it does work - I just realized I had it running in the wrong thread. . .  Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.