C # interaction between UI and work within winform

Source: Internet
Author: User

C # interaction between UI and work within winform

1. Use Multiple Programming

Void calcpi (INT digits)
{
//... Any token can be generated here, but if you interact with the UI, the token will be generated.
}

Void calcbutton_click (Object sender, eventargs E)
{
//... This is still in the UI program, which can be replaced by any token.
// Initiate a new route for processing tasks at the specified time
Thread pithread = new thread (calcpithreadstart );
Pithread. Start (INT) This. decimalplacesnumericupdown. value );
}
Void calcpithreadstart (Object digits)
{
// The program can only contain the number of bytes of the object type.
Calcpi (INT) digits );
}

2. Use other programming languages in the UI.

When multiple programming procedures are used, there are still two problems (1) the programming procedure can only handle object-type data (2) in the new programming process, you cannot directly modify the content in the form. in fact, when interacting with the UI, we generally do not use multiple programming threads, but use custom delegation. this is also the principle of multi-programming. however, operations are much more convenient.

-- 1. synchronous failover
Void calcpi (INT digits)
{
//... If there is a token for interaction with the UI interface, you must use the callback method.
}

Delegate void calcpidelegate (INT digits );
Void calcbutton_click (Object sender, eventargs E)
{
//...
//
Calcpidelegate calcpi = new calcpidelegate (calcpi );
// Synchronous token, same as calcpi. Invoke
Calcpi (INT) This. decimalplacesnumericupdown. value );
}

-- 2. asynchronous callback
Void calcpi (INT digits)
{
//... If there is a token for interaction with the UI interface, you must use the callback method.
}

Delegate void calcpidelegate (INT digits );
Void calcbutton_click (Object sender, eventargs E)
{
//...
//
Calcpidelegate calcpi = new calcpidelegate (calcpi );
// Used for asynchronous callback
Calcpi. begininvoke (
(INT) This. decimalplacesnumericupdown. Value, // assign this metric to the calcpi Method
Endcalcpi, // This method is used when calcpi ends
Calcpi); // this parameter is provided to the endcalcpi method. It seems that it must be the same as the delegate name.

}
Void endcalcpi (iasyncresult result)
{
// The result is obtained to handle exceptions and clear resources.
Try
{
Calcpidelegate calcpi = (calcpidelegate) result. asyncstate;
Calcpi. endinvoke (result); // invoke endinvoke
}
Catch (exception ex)
{
// The endcalcpi statement is used in the worker program.
//... The token that cannot interact with the UI. Otherwise
}

}

3. In the worker's workshop, return to the worker's UI program.
Whether it is synchronous or asynchronous, if you need to expose messages to the UI in the work schedule, such as exceptions or need to know the progress in the work schedule, at this time, we still need to restore the UI program. the callback is also divided into synchronous and asynchronous callback.
-- 1. synchronous back-to-memory
Change the calcpi Method to the following:

Void showprogress (string Pi, int totaldigits, int digitssofar)
{
// Ensure that the process is in the UI
Debug. Assert (this. invokerequired = false );
 
//... This generally refers to the generation of interaction with the UI program, and some short data processing.

}
Delegate void showprogressdelegate (string Pi, int totaldigits, int digitssofar );
Void calcpi (INT digits)
{
Stringbuilder Pi = new stringbuilder ("3", digits + 2 );
// Quasi-linear metric display progress
Showprogressdelegate showprogress = new showprogressdelegate (showprogress );

// When a response message needs to be sent back, the consumer uses control. invoke.
// Indicates the initial progress, and the work schedule is stopped. Wait until the execution is finished and then wait for the job schedule.
This. Invoke (showprogress, new object [] {pi. tostring (), digits, 0 });
If (digits> 0)
{
Pi. append (".");
For (INT I = 0; I <digits; I + = 9)
{
//... Processing of data, which is a time-consuming substitute
// Progress indicates the progress of the change, and the work schedule is stopped. Wait until the progress is displayed, and then wait for the job schedule.
This. Invoke (showprogress, new object [] {pi. tostring (), digits, I + digitcount });
}
}
}

-- 2. asynchronous callback
Change the calcpi Method to the following. In fact, the only difference between this generation and synchronous return is that begininvoke replaces invoke. asynchronous callback is a little different from Asynchronous callback, that is, asynchronous callback is in control. after begininvoke, you do not need to use the volume control. endinvoke, which is completely secure.
Void showprogress (string Pi, int totaldigits, int digitssofar)
{
// Ensure that the process is in the UI
Debug. Assert (this. invokerequired = false );
 
//... This generally refers to the generation of interaction with the UI program, and some short data processing.

}
Delegate void showprogressdelegate (string Pi, int totaldigits, int digitssofar );
Void calcpi (INT digits)
{
Stringbuilder Pi = new stringbuilder ("3", digits + 2 );
// Quasi-linear metric display progress
Showprogressdelegate showprogress = new showprogressdelegate (showprogress );

// When a response message needs to be sent back, it will be used again
// Notify the UI progress to indicate the initial progress and return the work progress immediately.
This. begininvoke (showprogress, new object [] {pi. tostring (), digits, 0 });
If (digits> 0)
{
Pi. append (".");
For (INT I = 0; I <digits; I + = 9)
{
//... Processing of data, which is a time-consuming substitute
// Notify the UI progress to demonstrate the progress of the change and return the work progress immediately.
This. begininvoke (showprogress, new object [] {pi. tostring (), digits, I + digitcount });
}
}
}

4. Summary
-- 1. the synchronous token is used together with the synchronous return Token.
Delegate void calcpidelegate (INT digits );
Void calcbutton_click (Object sender, eventargs E)
{
//...
//
Calcpidelegate calcpi = new calcpidelegate (calcpi );
// Synchronous token, same as calcpi. Invoke
Calcpi (INT) This. decimalplacesnumericupdown. value );
}

Void showprogress (string Pi, int totaldigits, int digitssofar)
{
// Ensure that the process is in the UI
Debug. Assert (this. invokerequired = false );
 
//... This generally refers to the generation of interaction with the UI program, and some short data processing.

}
Delegate void showprogressdelegate (string Pi, int totaldigits, int digitssofar );
Void calcpi (INT digits)
{
Stringbuilder Pi = new stringbuilder ("3", digits + 2 );
// Quasi-linear metric display progress
Showprogressdelegate showprogress = new showprogressdelegate (showprogress );

// Synchronous back-to-memory
This. Invoke (showprogress, new object [] {pi. tostring (), digits, 0 });
If (digits> 0)
{
Pi. append (".");
For (INT I = 0; I <digits; I + = 9)
{
//... Processing of data, which is a time-consuming substitute
// Synchronous back-to-memory
This. Invoke (showprogress, new object [] {pi. tostring (), digits, I + digitcount });
}
}
}

-- 2. Link asynchronous callback with asynchronous callback.

Delegate void calcpidelegate (INT digits );
Void calcbutton_click (Object sender, eventargs E)
{
//...
//
Calcpidelegate calcpi = new calcpidelegate (calcpi );
// Used for asynchronous callback
Calcpi. begininvoke (
(INT) This. decimalplacesnumericupdown. Value, // assign this metric to the calcpi Method
Endcalcpi, // This method is used when calcpi ends
Calcpi); // this parameter is provided to the endcalcpi method. It seems that it must be the same as the delegate name.

}
Void endcalcpi (iasyncresult result)
{
// The result is obtained to handle exceptions and clear resources.
Try
{
Calcpidelegate calcpi = (calcpidelegate) result. asyncstate;
Calcpi. endinvoke (result); // invoke endinvoke
}
Catch (exception ex)
{
// The endcalcpi statement is used in the worker program.
// If you need to return the callback UI response, you can use synchronous or asynchronous callback.
}

}

Void showprogress (string Pi, int totaldigits, int digitssofar)
{
// Ensure that the process is in the UI
Debug. Assert (this. invokerequired = false );
 
//... This generally refers to the generation of interaction with the UI program, and some short data processing.

}
Delegate void showprogressdelegate (string Pi, int totaldigits, int digitssofar );
Void calcpi (INT digits)
{
Stringbuilder Pi = new stringbuilder ("3", digits + 2 );
// Quasi-linear metric display progress
Showprogressdelegate showprogress = new showprogressdelegate (showprogress );

// When a response message needs to be sent back, it will be used again
// Notify the UI progress to indicate the initial progress and return the work progress immediately.
This. begininvoke (showprogress, new object [] {pi. tostring (), digits, 0 });
If (digits> 0)
{
Pi. append (".");
For (INT I = 0; I <digits; I + = 9)
{
//... Processing of data, which is a time-consuming substitute
// Notify the UI progress to demonstrate progress and return the work progress immediately.
This. begininvoke (showprogress, new object [] {pi. tostring (), digits, I + digitcount });
}
}
}

3. synchronous callback and asynchronous callback

Delegate void calcpidelegate (INT digits );
Void calcbutton_click (Object sender, eventargs E)
{
//...
//
Calcpidelegate calcpi = new calcpidelegate (calcpi );
// Synchronous token, same as calcpi. Invoke
Calcpi (INT) This. decimalplacesnumericupdown. value );
}
Void showprogress (string Pi, int totaldigits, int digitssofar)
{
// Ensure that the process is in the UI
Debug. Assert (this. invokerequired = false );
 
//... This generally refers to the generation of interaction with the UI program, and some short data processing.

}

Delegate void showprogressdelegate (string Pi, int totaldigits, int digitssofar );
Void calcpi (INT digits)
{
Stringbuilder Pi = new stringbuilder ("3", digits + 2 );
// Quasi-linear metric display progress
Showprogressdelegate showprogress = new showprogressdelegate (showprogress );

// When a response message needs to be sent back, it will be used again
// Notify the UI progress to indicate the initial progress and return the work progress immediately.
This. begininvoke (showprogress, new object [] {pi. tostring (), digits, 0 });
If (digits> 0)
{
Pi. append (".");
For (INT I = 0; I <digits; I + = 9)
{
//... Processing of data, which is a time-consuming substitute
// Notify the UI progress to demonstrate progress and return the work progress immediately.
This. begininvoke (showprogress, new object [] {pi. tostring (), digits, I + digitcount });
}
}
}

-- 4. Combination of asynchronous callback and synchronous callback
Delegate void calcpidelegate (INT digits );
Void calcbutton_click (Object sender, eventargs E)
{
//...
//
Calcpidelegate calcpi = new calcpidelegate (calcpi );
// Used for asynchronous callback
Calcpi. begininvoke (
(INT) This. decimalplacesnumericupdown. Value, // assign this metric to the calcpi Method
Endcalcpi, // This method is used when calcpi ends
Calcpi); // this parameter is provided to the endcalcpi method. It seems that it must be the same as the delegate name.

}
Void endcalcpi (iasyncresult result)
{
// The result is obtained to handle exceptions and clear resources.
Try
{
Calcpidelegate calcpi = (calcpidelegate) result. asyncstate;
Calcpi. endinvoke (result); // invoke endinvoke
}
Catch (exception ex)
{
// The endcalcpi statement is used in the worker program.
// If you need to return the callback UI response, you can use synchronous or asynchronous callback.
}

}

Void showprogress (string Pi, int totaldigits, int digitssofar)
{
// Ensure that the process is in the UI
Debug. Assert (this. invokerequired = false );
 
//... This generally refers to the generation of interaction with the UI program, and some short data processing.

}
Delegate void showprogressdelegate (string Pi, int totaldigits, int digitssofar );
Void calcpi (INT digits)
{
Stringbuilder Pi = new stringbuilder ("3", digits + 2 );
// Quasi-linear metric display progress
Showprogressdelegate showprogress = new showprogressdelegate (showprogress );

// Synchronous back-to-memory
This. Invoke (showprogress, new object [] {pi. tostring (), digits, 0 });
If (digits> 0)
{
Pi. append (".");
For (INT I = 0; I <digits; I + = 9)
{
//... Processing of data, which is a time-consuming substitute
// Synchronous back-to-memory
This. Invoke (showprogress, new object [] {pi. tostring (), digits, I + digitcount });
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.