Event thread updating guestmetrics

Access to Windows Forms controls is not inherently thread safe.



If you use multithreading to improve the performance of your Windows Forms applications, you must make sure that you make calls to your controls in a thread-safe way.Click Thread = New Thread( _ New Thread Start(Address Of Me. Start() End Sub ' This method is executed on the worker thread and makes ' an unsafe call on the Text Box control. Text = "This text was set unsafely." End Sub // This event handler creates a thread that calls a // Windows Forms control in an unsafe way.private: void set Text Unsafe Btn_Click(Object^ sender, Event Args^ e) // This method is executed on the worker thread and makes // an unsafe call on the Text Box control. NET Framework helps you detect when you are accessing your controls in a manner that is not thread safe.// This event handler creates a thread that calls a // Windows Forms control in a thread-safe way.private void set Text Safe Btn_Click( object sender, Event Args e) // This method is executed on the worker thread and makes // a thread-safe call on the Text Box control.private: void Thread Proc Safe() // This method demonstrates a pattern for making thread-safe // calls on a Windows Forms control. Two seconds later, when the unsafe call is attempted, the Visual Studio debugger indicates that an exception occurred.
// // If the calling thread is different from the thread that // created the Text Box control, this method creates a // String Arg Returning Void Delegate and calls itself asynchronously using the // Invoke method. The debugger stops at the line in the background thread that attempted to write directly to the text box.
// // If the calling thread is the same as the thread that created // the Text Box control, the Text property is set directly. You will have to restart the application to test the other two buttons.
private void Set Text(string text) ' This method demonstrates a pattern for making thread-safe ' calls on a Windows Forms control. When you click the Safe Call button, "Written by the main thread" appears in the text box.
// This event handler creates a thread that calls a // Windows Forms control in an unsafe way.
private void set Text Unsafe Btn_Click( object sender, Event Args e) // This method is executed on the worker thread and makes // an unsafe call on the Text Box control.
' ' If the calling thread is different from the thread that ' created the Text Box control, this method creates a ' String Arg Returning Void Delegate and calls itself asynchronously using the ' Invoke method. Two seconds later, the text box is set to "Written by the background thread (Invoke)", which indicates that the Invoke method was called.