The importance of multithreading for development and user experience is self-evident. Silverlight Bcl provides almost a complete Thread class.
1. Dispatcher
Like WPF/winform, we can only update display control attributes in Ui thread. In multi-threaded coding, dispatcher is required.
Private void button_click (Object sender, routedeventargs E)
{
New thread () =>
{
This. Dispatcher. begininvoke () =>
{
This. textblock1.text = datetime. Now. tostring ();
});
}). Start ();
}
You can use deployment. Current. Dispatcher in the class library to obtain the dispatcher reference.
Private void button_click (Object sender, routedeventargs E)
{
New thread () =>
{
Deployment. Current. Dispatcher. begininvoke () =>
{
This. textblock1.text = datetime. Now. tostring ();
});
}). Start ();
}
Of course, you can also use synchronous context to perform similar operations.
Private void button_click (Object sender, routedeventargs E)
{
VaR context = synchronizationcontext. Current;
New thread () =>
{
Context. Send (S) =>
{
This. textblock1.text = datetime. Now. tostring ();
}, Null );
}). Start ();
}
2. threadpool
The default number of threads in Silverlight threadpool is:
Workerthreads: 2 ~ 500
Completionportthreads: 2 ~ 1000
The usage is the same as before.
Private void button_click (Object sender, routedeventargs E)
{
Threadpool. queueuserworkitem (S) =>
{
This. Dispatcher. begininvoke () =>
{
Int minworkerthreads, mincompletionportthreads, maxworkerthreads, and maxcompletionportthreads;
Threadpool. getminthreads (Out minworkerthreads, out mincompletionportthreads );
Threadpool. getmaxthreads (Out maxworkerthreads, out maxcompletionportthreads );
This. textbox1.text = string. Format ("workerthreads = {0 }~ {1}, completionportthreads = {2 }~ {3 }",
Minworkerthreads, maxworkerthreads, mincompletionportthreads, maxcompletionportthreads );
});
});
}
It should be noted that, although threadpool provides the setminthreads and setmaxthreads methods, but they cannot be used, the call will trigger an exception.
Speaking of the thread pool, we will naturally think of the asynchronous call of the Delegate, but this is also a problem.
In Silverlight, using delegates to make Asynchronous Method CILS is not supported. Calling begininvoke causes a notsupportedexception.
3. waithandle
Waiting for a handle is essential to multi-threaded programming.
Public partial class mainpage: usercontrol
{
Autoresetevent handle = new autoresetevent (true );
Public mainpage ()
{
Initializecomponent ();
New thread () =>
{
While (true)
{
Handle. waitone ();
This. Dispatcher. begininvoke () =>
{
This. textblock1.text = datetime. Now. tostring ();
});
}
}). Start ();
}
Private void button_click (Object sender, routedeventargs E)
{
Handle. Set ();
}
}
4. Timer
System. thread. Timer is a multithreaded timer.
Public partial class mainpage: usercontrol
{
Timer timer;
Public mainpage ()
{
Initializecomponent ();
Timer = new timer (state) =>
{
This. Dispatcher. begininvoke () =>
{
This. textblock1.text = datetime. Now. ticks. tostring ();
});
}, Null, 0,100 );
}
}
Timercallback is executed in the thread pool, which means that it will trigger the next event without waiting for the event code to be executed completely, depending on whether the code execution time is earlier than the interval.
In addition, there is a system. Windows. Threading. dispatchertimer, which directly uses dispatcher queue to execute the Event code, so you can directly update the interface element.
Public partial class mainpage: usercontrol
{
Dispatchertimer timer;
Public mainpage ()
{
Initializecomponent ();
Timer = new dispatchertimer ();
Timer. Tick + = (S, e) =>{ this. textblock1.text = datetime. Now. tostring ();};
Timer. interval = timespan. frommilliseconds (1000 );
Timer. Start ();
}
} [Reprinted from http://www.rainsts.net /]