小弟用了两个线程,分别计数,显示在textBox1和richTextBox1中,运行之后发现还是一个框先计数到9之后,另一个才从0开始计数,怎么能实现两个线程同时计数呢?请高手指点一下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread newthread = new Thread(new ThreadStart(tthread));
newthread.Start();
Thread twothread = new Thread(new ThreadStart(thread_2));
twothread.Start();
} private void thread_2()
{
dc dc_2 = new dc(fun2);
this.textBox1.Invoke(dc_2);
} private void fun2()
{
for (int i = 0; i < 10; i++)
{
textBox1.Text = Convert.ToString(i);
textBox1.Refresh();
Thread.Sleep(1000);
}
} private void tthread()
{
dc dc_fun = new dc(fun);
this.richTextBox1.Invoke(dc_fun);
}
delegate void dc(); private void fun()
{
for (int i = 0; i < 10; i++)
{
richTextBox1.Text = Convert.ToString(i);
richTextBox1.Refresh();
Thread.Sleep(500);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread newthread = new Thread(new ThreadStart(tthread));
newthread.Start();
Thread twothread = new Thread(new ThreadStart(thread_2));
twothread.Start();
} private void thread_2()
{
dc dc_2 = new dc(fun2);
this.textBox1.Invoke(dc_2);
} private void fun2()
{
for (int i = 0; i < 10; i++)
{
textBox1.Text = Convert.ToString(i);
textBox1.Refresh();
Thread.Sleep(1000);
}
} private void tthread()
{
dc dc_fun = new dc(fun);
this.richTextBox1.Invoke(dc_fun);
}
delegate void dc(); private void fun()
{
for (int i = 0; i < 10; i++)
{
richTextBox1.Text = Convert.ToString(i);
richTextBox1.Refresh();
Thread.Sleep(500);
}
}
}
}
解决方案 »
- ◆vs2010 winform 水晶报表 如何给未绑定字段UnboundString赋值◆
- C#这个怎么写更好
- 关于C#事件
- C#模拟键盘按着不动
- 怎么在xml中以覆盖方式添加节点
- 又遇倒了讨厌的LoaderExceptions错误,谁能帮忙解决下
- 求教pro.Start()的调用参数问题
- 在问:Enterprise Library2.0那有下载!!!
- C#如何实现程序只有打包安装后才能运行,而直接copy安装后的文件夹到其他电脑是无法运行的?
- 大家现在都还用Winsock编程吗,Winsock过时了吗,比它更优秀的最新代替方法是什么?
- C#自定义按钮控件的问题WINFORM 方面的大家来看看那
- c# dll patch问题
Form上的控件是不支持多线程的。