15.01.2013 Views

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eal-time work, you must also elevate the process priority using the Process class in<br />

System.Diagnostics (we didn’t tell you how to do this):<br />

using (Process p = Process.GetCurrentProcess())<br />

p.PriorityClass = ProcessPriorityClass.High;<br />

ProcessPriorityClass.High is actually one notch short of the highest priority:<br />

Realtime. Setting a process priority to Realtime instructs the OS that you never want<br />

the process to yield CPU time to another process. If your program enters an accidental<br />

infinite loop, you might find even the operating system locked out, with<br />

nothing short of the power button left to rescue you! For this reason, High is usually<br />

the best choice for real-time applications.<br />

If your real-time application has a user interface, elevating the<br />

process priority gives screen updates excessive CPU time, slowing<br />

down the entire computer (particularly if the UI is complex).<br />

Lowering the main thread’s priority in conjunction with raising<br />

the process’s priority ensures that the real-time thread doesn’t<br />

get preempted by screen redraws, but doesn’t solve the problem<br />

of starving other applications of CPU time, because the operating<br />

system will still allocate disproportionate resources to the<br />

process as a whole. An ideal solution is to have the real-time<br />

worker and user interface run as separate applications with different<br />

process priorities, communicating via Remoting or<br />

memory-mapped files. Memory-mapped files are ideally suited<br />

to this task; we explain how they work in Chapters 14 and 25.<br />

Even with an elevated process priority, there’s a limit to the suitability of the managed<br />

environment in handling hard real-time requirements. In Chapter 12, we described<br />

the issues of garbage collection and the workarounds. Further, the operating<br />

system may present additional challenges—even for unmanaged applications—that<br />

are best solved with dedicated hardware or a specialized real-time platform.<br />

Exception Handling<br />

Any try/catch/finally blocks in scope when a thread is created are of no relevance<br />

to the thread when it starts executing. Consider the following program:<br />

public static void Main()<br />

{<br />

try<br />

{<br />

new Thread (Go).Start();<br />

}<br />

catch (Exception ex)<br />

{<br />

// We'll never get here!<br />

Console.WriteLine ("Exception!");<br />

}<br />

}<br />

static void Go() { throw null; } // Throws a NullReferenceException<br />

798 | Chapter 21: Threading

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!