Click here to Skip to main content
15,892,965 members
Articles / Web Development / ASP.NET

Cross Thread

Rate me:
Please Sign up or sign in to vote.
3.00/5 (6 votes)
23 Oct 2006CPOL 55.5K   950   25  
We can't perform operations on a control, if that control was created by another thread. When we try to do thatm, we will get cross thread exceptions.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace CrossThread
{
    public partial class Form1 : Form
    {
        private Thread guidThread;
        private string newGUID;
        public delegate void GuidThreadCallBack();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            guidThread = new Thread(new ThreadStart(CreateNewGuid));
            guidThread.Start();
        }
        public void CreateNewGuid()
        {
            while (true)
            {
                newGUID = Guid.NewGuid().ToString();
                //textBox1.Text = Guid.NewGuid().ToString();
                try
                {
                    this.Invoke(new GuidThreadCallBack(setTextBoxValue));
                }
                catch { guidThread.Abort(); }
            }
        }
        private void setTextBoxValue()
        {
            textBox1.Text = newGUID;
            textBox1.Refresh();
        }
    
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer General Physics Corporation (GP)
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