In an application scenario, the browser uploads a file and calls the file conversion in the background of this file, it takes a long time. In this way, if it continues to be synchronously, the user feels stuck in the browser, and it is stuck here. Here we use the delegated BeginInvoke and EndInvoke methods to operate the thread. The BeginInvoke method can use the thread to execute the method pointed to by the delegate asynchronously. Then use the EndInvoke method to obtain the return value of the method (the return value of the EndInvoke method is the return value of the called method), or make sure that the method has been successfully called. To put it bluntly, it is equivalent to opening a multi-thread. After your user file is saved, the response will be returned. This BeginInvoke asynchronously executes the delegate method. After that, execute your asynchronous callback function;
Probably the steps
1: First extract the method you want to execute asynchronously;
2: Define a delegate for the asynchronous method;
3: Instantiate this delegation at the call place;
4: Call the BeginInvoke method of this delegate instance. In this method, fill in the delegate parameters first, followed by the callback function after the delegate method is finished;
5: Write the delegated callback function. The callback function is a fixed parameter (IAsyncResultIR). In this, you can obtain a user-defined object, which limits or contains information about asynchronous operations (AsyncState) and then call EndInvoke to obtain the return value at the end of the delegate method.
6: Call custom callback functions;
Let's look at the code:
public class anysFileChange { /// <summary> /// After the file changes, the corresponding business logic needs to make the delegate of the lock call /// After the conversion, the executed function will be called. What does the user need to do? This function will be completed by the user's own logic. /// </summary> public static anysChangingHandlerCallBack _handler = null; /// <summary> /// Convert files /// </summary> /// <param name="filepath">File path</param> /// <returns></returns> public static void ChangingFile(string filepath, string attachId) { //Open asynchronous conversion DEGAsyncChangingFile acf = new DEGAsyncChangingFile(); (filepath, attachId, , acf); } /// <summary> /// Callback function after the asynchronous function is executed /// </summary> /// <param name="IR">Async results </param> /// <returns></returns> private static void CallBackAsync(IAsyncResult IR) { DEGAsyncChangingFile acf = (DEGAsyncChangingFile); ResultObj result = (ResultObj)(IR); //If the file conversion end method is called, the delegate that needs to be done after the conversion is called, then the user-defined delegate function is executed if (_handler != null && result != null) { /// Only after success is successful, the customer's customized callback function is executed if ( == ) { _handler(, ); } } } /// <summary> /// Convert files /// </summary> /// <param name="filepath">File path</param> /// <returns>HSUFResultObj Returns the result object</returns> private static ResultObj AsyncChangeFileToswf(string filepath,string attachId) { ResultObj res = new ResultObj(); //Convert action ConvertFile cf = new ConvertFile(); // This class cf provides a log-writing event. Register a log-writing event. += new (Syslog); //The methods provided by the cf class here are only calculated using the conversion method inside (filepath); if ( == ) { = attachId; = filepath; = ; } else { = ; = ; } return res; } /// <summary> /// Log events for registration /// </summary> /// <param name="messages"></param> private static void Syslog(string messages) { string separateLine = "\r\n============================================================================================\r\n"; string log = (@"{0} {1} {2} {0}", separateLine, (), messages); // If the upload course folder does not exist, create if (!( + "Log\\")) ( + "Log\\"); StreamWriter sw = new StreamWriter(+"Log\\"+("yyyy-MM-dd")+".txt", true); (log); (); } /// <summary> /// File conversion delegation /// </summary> /// <param name="filepath">File path</param> /// <returns> </returns> private delegate object DEGAsyncChangingFile(string filepath,string attachId); } /// <summary> /// Result Set Object /// </summary> public class ResultObj { /// <summary> /// File path /// </summary> public string filepath; /// <summary> /// id /// </summary> public string attachId; /// <summary> /// Convert result message /// </summary> public string changeMessage; /// <summary> /// Transition state /// </summary> public EnumChangeStatus changestaus; } /// <summary> /// After the file is converted, the delegate required by the business logic /// </summary> /// <param name="filepath">File path</param> public delegate void anysChangingHandlerCallBack(string filepath,string attchid);
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links