How does the callback function obtain the variables generated by the asynchronous function during the asynchronous callback of WPF?

Source: Internet
Author: User

How does the callback function obtain the variables generated by the asynchronous function during the asynchronous callback of WPF?

There is such a problem that when using asynchronous callback in WPF, the callback function needs to use a variable generated in the asynchronous function. For example, when querying the database in the asynchronous function, a DataTable is obtained, how do I pass the callback function?

Solution 1: Use global variables

It is easy to think of using global variables, which is also the simplest way. But if I want to call it cyclically, for example, if the callback function determines whether there is data in the DataTable after the asynchronous function is executed, the data will continue to be asynchronous (BeginInvoke ), at this time, it may be unexpected to use global variables. Because it is a loop call, it is hard to say whether the DataTable used by the callback function is the value you want.

[Solution 2] Closure

This is also a more common method. If the closure is used, internal variables are easily transmitted, as shown in the following code:

Private void QueryDateBase () {DataTable dtTarget = new DataTable (); // share variable Action handler = delegate () // asynchronous anonymous delegate {dtTarget = XXX query database ;}; asyncCallback functionCallBack = delegate (IAsyncResult asyResult) // callback anonymous delegate {handler. endInvoke (asyResult); if (dtTarget. rows. count> 0) {QueryDateBase () ;}}; handler. beginInvoke (functionCallBack, null );}
This is the so-called closure. The anonymous delegate is used, and the callback function and asynchronous function are defined in a method body so that the variables can be shared. Similarly, the WPF animation has a Completed event, this method can also be used to share variables if you want to use some variables at the beginning of execution. Note the following two points:

So can we not use global variables?

Solution 3: return values

Use a delegate with a return value to obtain the return value of the delegate when the EndInvoke is delegated. The code looks like this:

Public class Student {public Func <DataTable> queryHandler; public Student () {queryHandler = QueryDateBase; queryHandler. beginInvoke (CallBack, null);} private DataTable QueryDateBase () {DataTable dtTarget = XXX query database; return dtTarget;} private void CallBack (IAsyncResult ar) {DataTable dtCallBack = queryHandler. endInvoke (ar); if (dtCallBack. rows. count> 0) {queryHandler. beginInvoke (CallBack, null );}}}
I personally think this is a more orthodox way of writing, precise return values, no global variables. In fact, this is also true for Winform. There is no difference in use, but attention should be paid when wpf involves the UI.
How to obtain the return value of the asynchronous callback function cyclically

This is not the case. For Asynchronous callback functions, you can obtain the return value of the callback function. The time is uncertain. Add a flag to determine whether the callback function has been executed.

In c # Where is the exception thrown by the asynchronous callback function?

In fact, you can look at the callback function in two different threads.
When you call back, another thread is enabled to execute Connected.
So if an error occurs in Connected.
Will be thrown in another thread

Connected if you want to capture
There must be a mechanism for returning errors

That is to say, if
Throw an exception in the function.
Returns the exception to the main thread.

The use of delegate delegation may meet your requirements.

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.