Preface
When you write a GUI-like program, you encounter a situation where the user taps a button and the program handles the event, but the process takes a long time. We do not want the software to be stuck here, but to allow the user to continue to use other software features. This problem can be solved with multi-threaded event response. Here, I make a simple generalization of WPF's multi-threaded event response.
A simple asynchronous event response
In WPF, for a simple multithreading process, we can use. NET comes complete with the backgroundwork. The BackgroundWork process is asynchronous and does not cause the user interface to stop responding.
usingSystem.ComponentModel;usingSystem.Threading;namespacetestproject{ Public Partial classMainwindow:window {//... Private voidButton1_Click (Objectsender, RoutedEventArgs e) { //StatementBackgroundWorker worker =NewBackgroundWorker (); //What the worker is going to do uses an anonymous event response functionWorker. DoWork + = (o, ea) = { //WPF threads can control only the controls that they create.//If you want to modify the contents of the MainWindow interface created by the main thread,//you can delegate the dispatcher processing of the main thread. //here, the delegate content is an anonymous action object. This. Dispatcher.invoke (Action) (() = { This. TextBox1.Text ="worker started"; })); Thread.Sleep ( +); }; //Worker Completion Event ResponseWorker. runworkercompleted + = (o, ea) = { This. Dispatcher.invoke (Action) (() = { This. TextBox1.Text ="worker finished"; })); }; //Note: When you run the following line of code, the worker really starts working. The above is just a declaration definition. worker. RunWorkerAsync (); } //... }}
II. Multi-Threading process for custom events
Sometimes, in the new thread we create, there may be some events that require the main thread to process. For this more complex asynchronous processing, you can customize the event to be implemented with a delegate (delegate) in C #.
MyThread.cs (custom event)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;namespacetestproject{//defining parameter types for custom events Public classMyeventargs:eventargs {//parameter can carry value, convenient for handling program use Public ReadOnly intvalue; PublicMyEventArgs (intv) {value=v; } } classMyThread {//... //Define Delegate Public Delegate voidMyEventHandler (Objectsender, MyEventArgs e); //declaring custom Events Public EventMyEventHandler MyEvent; //... //Triggering Events protected Virtual voidonmyevent (MyEventArgs e) {if(MyEvent! =NULL) {MyEvent ( This, E); } } //... //test function Public voidTest () {System.Threading.Thread.Sleep ( +); This. Onmyevent (NewMyEventArgs ( -)); System.Threading.Thread.Sleep ( -); } }}
MainWindows.xaml.cs
usingSystem.ComponentModel;usingSystem.Threading;namespacetestproject{ Public Partial classMainwindow:window {//... //Test Private voidButton2_Click (Objectsender, RoutedEventArgs e) {MyThread MyThread=NewMyThread (); //declares a response function for the Mythread myevent eventMythread.myevent + = mythread_myevent; //define a new threadThread thread =NewThread (NewThreadStart (mythread.test)); //start a new threadthread. Start (); } //The response function of myevent in the new thread Public void mythread_myevent(Objectsender, MyEventArgs e) { //Similarly, if you want to modify the control of the main interface,//the dispatcher of the main thread need to be entrusted to handle. This. Dispatcher.invoke (Action) (() = { This. TextBox2.Text ="MyEvent triggered"; })); } //... }}
Summarize
The above describes the WPF multi-threading implementation approach in two scenarios. The first is to meet the general requirements, for example: loading files in the background. The second type is mainly for background processing when there are special events that require a foreground response, for example: loading files in the background, displaying specific content to the user at MainWindow whenever the load is full of 1M.
[02]WPF Asynchronous response, custom event, delegate--multithreading