Figure 146 Letting the User Cancel a Long-Running Operation in .NET

Drawer Denso QR Bar Code in .NET Figure 146 Letting the User Cancel a Long-Running Operation
Figure 146 Letting the User Cancel a Long-Running Operation
Generate Denso QR Bar Code In .NET
Using Barcode encoder for VS .NET Control to generate, create Quick Response Code image in VS .NET applications.
Implementing cancel for a long-running operation is a multistep process First, you need to provide a UI that lets the user cancel the operation In this case, the Calc button is changed to a Cancel button after the calculation has begun Another popular choice is to display a separate progress dialog, which typically includes current progress details, a progress bar
QR Code ISO/IEC18004 Scanner In .NET Framework
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
showing percentage of work complete, and a Cancel button If the user decides to cancel, that is noted in a member variable In addition, the UI is disabled for the short time between the time when the UI thread knows the worker thread should stop and the time the worker thread itself knows and has had a chance to stop sending progress If you ignore this lag time, the user could start another operation before the first worker thread stops sending progress, making it the job of the UI thread to figure out whether it's getting progress from the new worker thread or the old worker thread, which is supposed to be shutting down You could assign each worker thread a unique ID so that the UI thread can keep such things organized (and, in the face of multiple simultaneous long-running operations, you may well need to do this), but it's often simpler to pause the UI for this brief amount of time The sample application keeps track of its current processing state using a value from a three-value enumeration: enum CalcState Pending, Calculating, Canceled, } { // No calculation running or canceling // Calculation in progress // Calculation canceled in UI but not worker
Make Bar Code In .NET
Using Barcode printer for .NET Control to generate, create bar code image in Visual Studio .NET applications.
CalcState state = CalcStatePending; Now, depending on what state the application is in, it treats the Calc button differently: void calcButton_Click(object sender, EventArgs e) { // Calc button does double duty as Cancel button switch( state ) { // Start a new calculation case CalcStatePending: // Allow canceling state = CalcStateCalculating; calcButtonText = "Cancel"; // Async delegate method CalcPiDelegate calcPi = new CalcPiDelegate(CalcPi); calcPiBeginInvoke((int)digitsUpDownValue, null, null); break; // Cancel a running calculation case CalcStateCalculating: state = CalcStateCanceled; calcButtonEnabled = false; break; // Shouldn't be able to press Calc button while it's canceling case CalcStateCanceled: DebugAssert(false); break; } } Notice that when the Calc/Cancel button is pressed in the Pending state, the application sets the state to Calculating, changes the label on the button, and starts the calculation asynchronously, just as it did before If the state is Calculating when the Calc/Cancel button is pressed, the application switches the state to Canceled and disables the UI The UI will remain disabled, preventing the start of a new calculation, for as long as it takes to communicate the canceled state to the worker thread
Decode Bar Code In .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications.
After the UI thread has communicated with the worker thread that the operation has been canceled, the UI thread enables the UI again and resets the state to Pending (shown later) so that the user can start another operation To communicate to the worker that it should cancel, the sample augments the ShowProgress method to include a new out parameter: void ShowProgress(, out bool cancel); void CalcPi(int digits) { bool cancel = false; for( int i = 0; i < digits; i += 9 ) { // Show progress (checking for Cancel) ShowProgress(, out cancel); if( cancel ) break; } } You may be tempted to make the cancel indicator a Boolean return value from ShowProgress, but I find it hard to remember whether a return value of "true" means to cancel or to continue as normal I use the out parameter technique to make it very clear what's going on The only thing left to do is to update the ShowProgress method to notice whether the user has asked to cancel and to let CalcPi know accordingly Exactly how that information is communicated depends on which technique we'd like to use
Create QR In Visual C#
Using Barcode maker for Visual Studio .NET Control to generate, create QR Code ISO/IEC18004 image in VS .NET applications.
QR Code 2d Barcode Generator In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
Barcode Encoder In .NET
Using Barcode creator for .NET Control to generate, create barcode image in VS .NET applications.
Bar Code Creator In Visual Studio .NET
Using Barcode generator for VS .NET Control to generate, create bar code image in .NET framework applications.
Encoding USS ITF 2/5 In .NET
Using Barcode printer for VS .NET Control to generate, create 2 of 5 Interleaved image in Visual Studio .NET applications.
Barcode Creation In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Code 39 Full ASCII Encoder In C#
Using Barcode printer for VS .NET Control to generate, create Code39 image in .NET applications.
Barcode Maker In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Generate Barcode In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create bar code image in ASP.NET applications.