Silverlight-ThreadPool Usage of Multithreading

ThreadPool is the most frequently used multithreading operation. It is convenient to deal with time-consuming functions by using threads in Thread Pool. Also, it can release this thread for the next time after completing function operation.

There is one thread pool in every application. So, thread pool is static method, not needing to be instantiated. Of course, it is limited to use thread in thread pool. For example, current system supports to operate 50 tasks everytime. It will block if we requires to operate 150 tasks. Therefore, it is very important to use thread pool reasonably.

In this post, I will talk something about the simplest operation to run ThreadPool in Silverlight.

First, ThreadPool.QueueUserWorkItem (new WaitCallback(MethodA), argA);

Start one threading operation task MethodA and deliver parameter argA by calling QueueUserWorkItem method.

Second, calculate data in MethodA and display the result on foreground website. The following method can be used to call UI thread if we want to display data on forground website in silverlight. this.label1.Dispatcher.BeginInvoke (new DoThingDele(DoThing), str.ToString());

  • DoThing (string str): operation function to display data to foreground.
  • DoThingDele: delegate of the above function.
  • this.label1.Dispatcher.BeginInvoke(): function to call UI thread which is related to label1 control to execute DoThingDele. In other words, display data by across visiting UI thread.

Third, display and operate result in DoThing (string str).

If we need to start several threads, call QueueUserWorkItem method as following and we can operate 5 tasks by calling 5 threads in thread pool meanwhile.

ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “1”);

ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “2”);

ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “3”);

ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “4”);

ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “5”);

We can also call the following tow functions.

            //GetMaxThreads(out int workerThreads,out int completionPortThreads) Get Max Threads in Thread Pool

            //Parameter workerThreads  Max Worker Threads in Thread Pool

            //Parameter completionPortThreads   Max Asynchronous I/O Threads in Thread Pool

            ThreadPool.GetMaxThreads(out maxWorkerThread,out maxCompletionThread);

            //GetMinThreads(out int workerThreads,out int completionPortThreads) Get Min Threads in Thread Pool

            //Parameter workerThreads  Min Worker Threads in Thread Pool

            //Parameter completionPortThreads Min Asynchronous I/O Threads in Thread Pool

            ThreadPool.GetMinThreads(out minWorkerThread, out minCompletionThread);

Using the Code:

        public partial class MainPage : UserControl

        {

            public MainPage()

            {

                InitializeComponent();

            }

            //Display Data on Foreground through the Function

            public void DoThing(string arg)

            {

                this.label1.Content = this.label1.Content + “-“+arg;

            }

            //State DoThing Method Delegate

            public delegate void DoThingDele(string arg);

            ///<summary>

            ///Display Data. This method Used to Call Thread in Thread Pool

            ///</summary>

            ///<param name=”str”></param>

            public void ShowInfo(object str)

            {

                //Cost 3000 Millisecond when Simulating Calculation

                Thread.Sleep(1000);

                //Execute DoThingDele Function by Calling UI Thread which Related to label1. I.E. Display Data by Across Visiting UI Thread

                this.label1.Dispatcher.BeginInvoke(new DoThingDele(DoThing), str.ToString());

            }

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                //Thread Pool Call Method

                ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “1”);

                ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “2”);

                ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “3”);

                ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “4”);

                ThreadPool.QueueUserWorkItem(new WaitCallback(ShowInfo), “5”);

            }

            private void button2_Click(object sender, RoutedEventArgs e)

            {

                int maxWorkerThread, maxCompletionThread;

                int minWorkerThread, minCompletionThread;

                //GetMaxThreads(out int workerThreads,out int completionPortThreads) Get Max Threads in Thread Pool

                //Parameter workerThreads  Max Worker Threads in Thread Pool

                //Parameter completionPortThreads   Max Asynchronous I/O Threads in Thread Pool

                ThreadPool.GetMaxThreads(out maxWorkerThread,out maxCompletionThread);

                //GetMinThreads(out int workerThreads,out int completionPortThreads) Get Min Threads in Thread Pool

                //Parameter workerThreads  Min Worker Threads in Thread Pool

                //Parameter completionPortThreads   Min Asynchronous I/O Threads in Thread Pool

                ThreadPool.GetMinThreads(out minWorkerThread, out minCompletionThread);

                this.label2.Content = “Max Worker Threads:” + maxWorkerThread + “– Max Asynchronous I/O Threads:” + maxCompletionThread + “—aMin Worker Threads:” + minWorkerThread + “– Min Asynchronous I/O Threads:” + minCompletionThread;

            }

        }

MainPage.xaml

<UserControl x:Class=“SLThreadPool.MainPage”

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml&#8221;

    xmlns:d=http://schemas.microsoft.com/expression/blend/2008&#8221;

    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006&#8221;

    mc:Ignorable=“d”

    d:DesignHeight=“300” d:DesignWidth=“400” xmlns:sdk=http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&#8221;>

    <Grid x:Name=“LayoutRoot” Background=“White”>

        <sdk:Label Height=“28” HorizontalAlignment=“Left” Margin=“28,84,0,0” Name=“label1” VerticalAlignment=“Top” Width=“Auto” />

        <Button Content=“Start Thread Pool” Height=“23” HorizontalAlignment=“Left” Margin=“53,118,0,0” Name=“button1” VerticalAlignment=“Top” Width=“75” Click=“button1_Click” />

        <Button Content=“Button” Height=“23” HorizontalAlignment=“Left” Margin=“296,244,0,0” Name=“button1” VerticalAlignment=“Top” Width=“75” Click=“button1_Click” />

        <Button Content=“Display Spare Threads” Height=“23” HorizontalAlignment=“Left” Margin=“54,189,0,0” Name=“button2” VerticalAlignment=“Top” Width=“75” Click=“button2_Click” />

        <sdk:Label Height=“28” HorizontalAlignment=“Left” Margin=“28,155,0,0” Name=“label2” VerticalAlignment=“Top” Width=“Auto” />

    </Grid>

</UserControl>

Two Components Recommend:

Spire.Doc, is used to operate MS Word document for .Net and Silverlight, including basic manipulations (generate, open, edit documents), mail merge and other Word functions manipulations.

Spire.XLS, is used to operate MS Excel for .NET and Silverlight, including basic manipulations (generate, open, edit files), chart creation and data exporting.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s