Ways in which coms thread affinity will affect you

Info icon This preview shows pages 646–648. Sign up to view the full content.

View Full Document Right Arrow Icon
ways in which COM’s thread affinity will affect you are that certain objects will have different performance characteristics depending on which thread you call them on, and there may be additional complica- tions if your COM objects use callbacks. So thread affinity just means that the thread you’re calling on makes a difference. It doesn’t always mean that using the wrong thread is guar- anteed to fail—it depends on what you’re using. If you never write multithreaded code, you never have to worry about thread affinity— if you do everything on one thread, it will always be the right one. But as soon as multiple threads get involved—either explicitly or implicitly —you may need to add code to get things back on the right thread. ASP.NET has a similar problem. It makes contextual information about the current request available to the thread handling the request, so if you use multiple threads to handle a single request, those other threads will not have access to that contextual information. Strictly speaking, this isn’t a thread affinity issue—ASP.NET can use dif- ferent threads at different stages of handling a single request—but it presents the same challenge to the developer: if you start trying to use ASP.NET objects from some ran- dom thread, you will have problems. The .NET Framework defines a solution that’s common to WPF, Windows Forms, and ASP.NET. The SynchronizationContext class can help you out if you find yourself on the wrong thread when using any of these frameworks. Example 16-7 shows how you can use this in an event handler for a GUI application—the click handler for a button, perhaps. Always remember that even if you have not created any threads explicitly, that doesn’t mean you’re necessarily writing single-threaded code. Some .NET Framework classes will bring extra threads into play implicitly. For example, the CLR’s garbage collector runs finalizers on a distinct thread. 622 | Chapter 16: Threads and Asynchronous Code
Image of page 646

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example 16-7. Handling thread affinity with SynchronizationContext SynchronizationContext originalContext = SynchronizationContext.Current; ThreadPool.QueueUserWorkItem(delegate { string text = File.ReadAllText(@"c:\temp\log.txt"); originalContext.Post(delegate { myTextBox.Text = text; }, null); }); The code reads all the text in from a file, and that’s something that might take awhile. Event handlers in WPF and Windows Forms are called on the thread that the event source belongs to—a UI thread. (Or the UI thread if, like most desktop applications, you have only one UI thread.) You should never do slow work on a UI thread—thread affinity means that if your code is busy using that thread, none of the UI elements belonging to that thread will be able to do anything until you’re finished. The user interface will be unresponsive for as long as you keep the thread busy. So Exam- ple 16-7 uses the thread pool to do the work, keeping the UI thread free.
Image of page 647
Image of page 648
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern