Silverlight 3-multithreading Programming

Source: Internet
Author: User

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 /]

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.