private void button1_Click(object sender, EventArgs e) { MyClass cls = new MyClass(); cls.UserName = "******"; cls.PassWord = "******"; Thread t2 = new Thread(new ParameterizedThreadStart(Calc)); t2.Start(cls); t2 = new Thread(new ThreadStart(Calc)); t2.Start(); }
void ThreadProc() { while (true) { MyClass cls = new MyClass(); cls.dgv = "asdf,wer,234".Split(','); cls.chk = true; cls.txt = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss:fff"); cls.cbx = "3"; if (IsHandleCreated) { this.Invoke(updater, cls); } Thread.Sleep(1000); } }
void ThreadProc() { while (true) { MyClass cls = new MyClass(); cls.dgv = "asdf,wer,234".Split(','); cls.chk = true; cls.txt = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss:fff"); cls.cbx = "3"; if (IsHandleCreated) { //Method 001 this.Invoke(new MethodInvoker(delegate() { dataGridView1.Rows.Add(cls.dgv); textBox1.Text = cls.txt; checkBox1.Checked = cls.chk; comboBox1.Text = cls.cbx; })); //Method 002 this.Invoke(new MethodInvoker(() => { dataGridView1.Rows.Add(cls.dgv); textBox1.Text = cls.txt; checkBox1.Checked = cls.chk; comboBox1.Text = cls.cbx; })); //Method 003 this.SafeInvoke(() => { dataGridView1.Rows.Add(cls.dgv); textBox1.Text = cls.txt; checkBox1.Checked = cls.chk; comboBox1.Text = cls.cbx; }); } Thread.Sleep(1000); } }