Silverlight-Multithreading Application: Update UI Control with Background Threading and Deliver Parameter to Multithreading

It is common to update UI control after doing lots of algorithms in project. But it costs much time and UI process may not operated. If we want to update UI and avoid process error when completing these complicated algorithms on background, we need to use multithreading technology. We can create one tread to do the complicated algorithm to save time to update UI. However, the error “Invalid to Access Across Thread” appears if update UI in thread execution function directly. In the following example, there are two UI controls: label1 and label2 in xaml file. Call this.lable1.Dispatcher.BeginInvoke method to execute UpdateLabel(string content) function which labelInvoke entrusts, then we can update displayed content of label1 directly in UpdateLabel function. Also, other UI controls can be updated like this.

The following code shows how to update label1 of UI in created background thread.

        #regionStart One Custom Background Thread which just Updates UI without Parameter

        public void NoParamThread()

        {

            Thread thread = new Thread(ShowStr);

            thread.IsBackground = true;

            thread.Start();

        }

        ///<summary>

        ///Dispaly Str function

        ///</summary>

        public void ShowStr()

        {

            string content=“Thread Update without Parameter Delivering”;

            //Run UpdateLabel which labelInvoke Entrusts and Deliver Parameter content

            this.label1.Dispatcher.BeginInvoke(new labelInvoke(UpdateLabel),content);

        }

        ///<summary>

        ///Update Label Delegate

        ///</summary>

        ///<param name=”content”></param>

        public delegate void labelInvoke(string content);

        ///<summary>

        ///Update Label Method

        ///</summary>

        ///<param name=”content”></param>

        public void UpdateLabel(string content)

        {

            //Update Value of this.label1.Contnet

            this.label1.Content = content;

        }

        #endregion

We can get 5 results after importing five different 5 parameters. It costs 2 sec to run one function so that it costs 10 sec for one thread to do this. We can create a loop to use five threads to operate this function with parameter at the same time. Then, it just costs 2 sec totally. This is background multi-threaded parallel task.

        #regionStart One Process to Deliver Parameter

        public void HaveParamThread(string str)

        {

            //Create 5 treads using For Loop

            for (int i = 0; i < 5; i++)

            {

                Thread thread = new Thread(new ParameterizedThreadStart(ShowString));

                thread.IsBackground = true;

                thread.Start(str + i.ToString());

            }

        }

        ///<summary>

        ///Display Str function

        ///</summary>

        public void ShowString(object content)

        {

            string con = content as string;

            //Do Complicated Operation

            string strcon = con.Substring(0, con.Length – 2);

            //Run labelDelegate which is delegated by labelInvoke and send paraneter con.

            this.label2.Dispatcher.BeginInvoke(new labelDelegate(UpdateLab), con);

        }

        ///<summary>

        ///Update Label2 delegate

        ///</summary>

        ///<param name=”content”></param>

        public delegate void labelDelegate(string content);

        ///<summary>

        ///Update label2 method

        ///</summary>

        ///<param name=”content”></param>

        public void UpdateLab(string content)

        {

            //Update value of this.label1.Contnet

            this.label2.Content =this.label2.Content+“–“+ content;

        }

        #endregion

Conclusion:

Multithreading is often used in the following two aspects:

1. Accelerate UI control response and imporve user experience. It makes background thread focus on operating through separating UI thread and background opertion thread. After completing, the result will be displayed by UI thread.

2. Backstage multitasking parallel computing. It saves time to run a complicated operation function with several thread meanwhile.

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