Click here to Skip to main content
15,892,161 members
Articles / Programming Languages / C#
Tip/Trick

control.invoke must be used to interact with controls created on a separate thread in .net CF

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 Sep 2011CPOL 24.9K   1   4
On version 1 of the .NET Compact Framework, applications which attempted to update their user interface from a worker thread would typically hang.    Having an application hang is an unpleasant user experience.

Control.Invoke must be used to interact with controls created on a separate thread. The first step to fix our snippet is to define a delegate.

C#
public int c = 0;
        private delegate void UpdateStatusDelegate();

Next, we add an implementation of a method that matches our delegate's signature
C#
public void newload()
        {
            int j = 0;
            for (int i = 0; i == j; i++)
            {
                Thread.Sleep(5000);
                if (this.InvokeRequired)
                {
                    // we were called on a worker thread
                    this.Invoke(new UpdateStatusDelegate(fncontrol) );
                }

                MessageBox.Show("working count: "+c+" " );
                c++;
                j++;
            }
        }

private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new System.Threading.ThreadStart(newload));

            t.Start();
            t.IsBackground = true;
        }
        public void fncontrol()
        {
            this.textBox1.Text = c.ToString();
            this.progressBar1.Value = c;
        }
public void newload()
        {
            int j = 0;
            for (int i = 0; i == j; i++)
            {

                Thread.Sleep(5000);
                if (this.InvokeRequired)
                {
                    // we were called on a worker thread

                    this.Invoke(new UpdateStatusDelegate(fncontrol) );

                }

                MessageBox.Show("working count: "+c+" " );
                c++;
                j++;
            }
        }

private void button1_Click(object sender, EventArgs e)
        {

            Thread t = new Thread(new System.Threading.ThreadStart(newload));

            t.Start();
            t.IsBackground = true;
        }
        public void fncontrol()
        {
            this.textBox1.Text = c.ToString();
            this.progressBar1.Value = c;
        }

Full Code


C#
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace sdpThread
{
    public partial class Form1 : Form
    {
        public int c = 0;
        private delegate void UpdateStatusDelegate();

        public Form1()
        {
            InitializeComponent();
        }

        public void newload()
        {

            int j = 0;
            for (int i = 0; i == j; i++)
            {

                Thread.Sleep(5000);
                if (this.InvokeRequired)
                {
                    // we were called on a worker thread

                    this.Invoke(new UpdateStatusDelegate(fncontrol) );

                }

                MessageBox.Show("working count: "+c+" " );

                c++;
                j++;
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {

            Thread t = new Thread(new System.Threading.ThreadStart(newload));
            t.Start();

            t.IsBackground = true;
        }
        public void fncontrol()
        {
            this.textBox1.Text = c.ToString();
            this.progressBar1.Value = c;
        }
    }
}

Join In Codeforum

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Standout IT Solutions(P) Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThe cross-thread limitations are not specific to CF or a par... Pin
Luc Pattyn14-Sep-11 10:36
sitebuilderLuc Pattyn14-Sep-11 10:36 
GeneralJust dumping code does NOT make this a valid tip/trick. How ... Pin
#realJSOP31-Dec-10 8:56
mve#realJSOP31-Dec-10 8:56 
GeneralNo understanding at all, I found similar words in <a href... Pin
Hiren solanki30-Dec-10 21:02
Hiren solanki30-Dec-10 21:02 
GeneralRe: pode pode Pin
Nikhil Purameri20-Sep-11 20:21
Nikhil Purameri20-Sep-11 20:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.