Communication with Shared Data in .NET framework

Encoder QR Code 2d barcode in .NET framework Communication with Shared Data
Communication with Shared Data
QR Code Generation In .NET
Using Barcode drawer for .NET Control to generate, create QR Code JIS X 0510 image in .NET applications.
Remember that the ShowProgress method is the code that actually performs the transition between the worker thread and the UI thread, so it's the one at the heart of the communication between the two threads The obvious way to communicate the current state of the UI is to give the worker thread direct access to the state member variable: void ShowProgress(, out bool cancel) { if( state == CalcStateCanceled ) { state = CalcStatePending; cancel = true; return; } } I hope that something inside you cringed when you saw this code If you're going to do multithreaded programming, you're going to have to watch out for any time that two threads have simultaneous access to the same data (in this case, the state member variable) Shared access to data between threads makes it very easy to get into race conditions, in which one thread is racing to read data that is only partially up-to-date before another thread has finished updating it For concurrent access to shared data to work, you must monitor usage of your shared data to make sure that each thread waits patiently while the other thread works on the data To monitor access to shared data, NET provides the Monitor class You use Monitor on a shared object to act as the lock on the data, which C# wraps with the handy lock block: object stateLock = new object();
Denso QR Bar Code Decoder In .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
void ShowProgress(, out bool cancel) { lock( stateLock ) { // Monitor the lock if( state == CalcStateCancel ) { state = CalcStatePending; cancel = true; return; } } } The data has now been properly protected against race conditions, but the way we've done it invites another problem known as a deadlock When two threads are deadlocked, each of them waits for the other to complete its work before continuing, thereby ensuring that neither will actually progress If all this talk of race conditions and deadlocks has caused you concern, that's good Multithreaded programming with shared data is hard So far we've been able to avoid these issues because we have passed copies of data around so that no two threads need to share access to any one piece of data If you don't have shared data, there's no need for synchronization If you find that you need access to shared data maybe because the overhead of copying the data is too great a space or time burden then you'll need to read up on multithreading and shared data synchronization, topics that are beyond the scope of this book Luckily, the vast majority of multithreading scenarios, especially as related to UI multithreading, seem to work best with the simple message-passing scheme used so far Most of the time, you don't want the UI to have access to data being worked on in the background, such as the document being printed or the collection of objects being enumerated For these cases, passing data to be owned by the receiving thread is just the ticket
Make Bar Code In Visual Studio .NET
Using Barcode creation for VS .NET Control to generate, create bar code image in VS .NET applications.
Communicating via Method Parameters
Barcode Decoder In VS .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications.
Because ShowProgress has already been updated with an out parameter, the sample lets it check the state variable when it's executing on the UI thread: void ShowProgress(, out bool cancel) { // Make sure we're on the UI thread if( thisInvokeRequired == false ) { // Check for Cancel cancel = (state == CalcStateCanceled); // Check for completion if( cancel || (digitsSoFar == totalDigits) ) { state = CalcStatePending; calcButtonText = "Calc"; calcButtonEnabled = true; } } // Transfer control to UI thread else { } } The UI thread is the only one to access the state member variable, so no synchronization is needed Now it's just a matter of passing control to the UI thread in such a way as to harvest the cancel output parameter of the ShowProgressDelegate Unfortunately, our use of
Print QR Code In Visual C#
Using Barcode encoder for .NET framework Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications.
ControlBeginInvoke makes this complicated, because it doesn't wait for results from the UI thread Waiting for the ShowProgress method to complete and the cancel flag to be set requires a call to the blocking ControlInvoke, but even this is a bit tricky: void ShowProgress(, out bool cancel) { if( thisInvokeRequired == false ) { } // Transfer control to UI thread else { ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress); // Avoid boxing and losing our return value object inoutCancel = false; // Show progress synchronously (so we can check for cancel) Invoke(showProgress, new object[] {, inoutCancel}); cancel = (bool)inoutCancel; } } It would have been nice to simply pass a Boolean variable directly to ControlInvoke to harvest the cancel parameter, but there is a problem The problem is that a Boolean is a value type, whereas Invoke takes an array of objects, which are reference types A value type is a simple type, such as a Boolean, that is meant to be managed on the stack A reference type , on the other hand, comes out of the heap Although passing a value type where a reference type is expected is certainly legal, it causes a copy of the value type (this copying is called boxing )[3] So even though ShowProgress would be able to change the cancel flag, the change would occur on a temporary variable created by the run time on-the-fly in the heap, and we have no access to that variable
QR Code Creation In VS .NET
Using Barcode printer for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
For the full treatment of value types, reference types, boxing, and unboxing, see Essential NET (Addison-Wesley, 2003), by Don Box, with Chris Sells
QR Code Generation In Visual Basic .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create QR Code image in .NET applications.
To avoid losing updates to the cancel flag, ShowProgress instead manually creates and passes a reference type variable (inoutCancel), avoiding the copy After the synchronous call to Invoke, the code casts the object variable back to a Boolean to see whether or not the operation should be canceled
Generating EAN / UCC - 13 In .NET Framework
Using Barcode generator for .NET framework Control to generate, create GS1 - 13 image in .NET framework applications.
Make Data Matrix ECC200 In VS .NET
Using Barcode printer for Visual Studio .NET Control to generate, create ECC200 image in VS .NET applications.
ANSI/AIM Code 39 Maker In .NET
Using Barcode creator for .NET Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications.
Barcode Generator In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Bar Code Printer In Java
Using Barcode generator for Java Control to generate, create barcode image in Java applications.
Barcode Creation In Java
Using Barcode creator for Java Control to generate, create bar code image in Java applications.
Paint Code 128B In Java
Using Barcode creator for Java Control to generate, create Code 128 Code Set B image in Java applications.