一个timer中可以同时运行多个线程吗?
怎么才能访问线程中的控件?现在有一个timer,每隔1分钟查询出A表的数据后插入到另一个数据库的B表中
一个有4张表,我想在一个timer中用4个线程,但是不能对控件进行赋值
请问怎么解决比较好?才能对控件赋值?代码:
public partial class SystemSet : Form
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); int timeSum = 0;
int upCount = 0;
int thread1Count = 0;
int thread2Count = 0;
int thread3Count = 0;
int thread4Count = 0;
int timeSpan = 60000;//毫秒
DateTime timeStart = new DateTime();
DateTime timeCurrent = new DateTime(); public SystemSet()
{
InitializeComponent(); string strTimeSpan = Common.Config.GetTimeSpan();
if (!string.IsNullOrEmpty(strTimeSpan))
{
timeSpan = int.Parse(strTimeSpan);
}
timer_Set();
timer_Start(); Rock.Utils.AutoStartup.SetAutoRun(Application.ExecutablePath, true);
} private void timer_Event(object sender, EventArgs args)
{
try
{
insert2Center();
timeSum += timeSpan / 1000;//转换成秒 TimeSpan ts = TimeSpan.FromMilliseconds(timeSum * 1000);
string strTimeSpan = string.Format("{0}天{1}时{2}分{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
LB_RunTime.Text = strTimeSpan; //显示到lable
LB_CurrentTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
// this.Invoke(new UpdateUICallBack(Update_UIEvent));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void insert2Center()
{
try
{
Thread thread1 = new Thread(new ThreadStart(thread1_Run));
thread1.Start();
Thread thread2 = new Thread(new ThreadStart(thread2_Run));
thread2.Start();
Thread thread3 = new Thread(new ThreadStart(thread3_Run));
thread3.Start();
Thread thread4 = new Thread(new ThreadStart(thread4_Run));
thread4.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} }
public void thread1_Run()
{
try
{
string strResult1 = WebService.XianKeShengXian.SuperMarBaseInfoWebService.Insert();
thread1Count++;
LB_BaseInfo_Count.Text = thread1Count.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void thread2_Run()
{
//对表2的插入
}
public void thread3_Run()
{
//对表3的插入
}
public void thread4_Run()
{
//对表4的插入
}
public void timer_Set()
{
timer.Interval = timeSpan;
timer.Tick += new EventHandler(timer_Event);
}
public void timer_Start()
{
try
{
timer.Start();
timeStart = DateTime.Now;
timeCurrent = timeStart;
LB_StartTime.Text = timeStart.ToString("yyyy-MM-dd hh:mm:ss");
LB_CurrentTime.Text = timeCurrent.ToString("yyyy-MM-dd hh:mm:ss");
timeSum = 0;
string strTimeSpan = string.Format("{0}天{1}时{2}分{3}秒", 0, 0, 0, 0);
LB_RunTime.Text = strTimeSpan;
LB_Tip.Text = "当前系统正在运行"; thread1Count = 0;
thread2Count = 0;
thread3Count = 0;
thread4Count = 0;
LB_BaseInfo.Text = "运行中";
LB_SupplierBaseInfo.Text = "运行中";
LB_MeatEntryInfo.Text = "运行中";
LB_VegEntryInfo.Text = "运行中";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void timer_Stop()
{
try
{
timer.Stop();
LB_Tip.Text = "当前系统已停止运行"; LB_BaseInfo.Text = "已停止";
LB_SupplierBaseInfo.Text = "已停止";
LB_MeatEntryInfo.Text = "已停止";
LB_VegEntryInfo.Text = "已停止";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} }
private void Btn_Start_Click(object sender, EventArgs e)
{
.....
} private void Btn_Stop_Click(object sender, EventArgs e)
{
......
}
}
怎么才能访问线程中的控件?现在有一个timer,每隔1分钟查询出A表的数据后插入到另一个数据库的B表中
一个有4张表,我想在一个timer中用4个线程,但是不能对控件进行赋值
请问怎么解决比较好?才能对控件赋值?代码:
public partial class SystemSet : Form
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); int timeSum = 0;
int upCount = 0;
int thread1Count = 0;
int thread2Count = 0;
int thread3Count = 0;
int thread4Count = 0;
int timeSpan = 60000;//毫秒
DateTime timeStart = new DateTime();
DateTime timeCurrent = new DateTime(); public SystemSet()
{
InitializeComponent(); string strTimeSpan = Common.Config.GetTimeSpan();
if (!string.IsNullOrEmpty(strTimeSpan))
{
timeSpan = int.Parse(strTimeSpan);
}
timer_Set();
timer_Start(); Rock.Utils.AutoStartup.SetAutoRun(Application.ExecutablePath, true);
} private void timer_Event(object sender, EventArgs args)
{
try
{
insert2Center();
timeSum += timeSpan / 1000;//转换成秒 TimeSpan ts = TimeSpan.FromMilliseconds(timeSum * 1000);
string strTimeSpan = string.Format("{0}天{1}时{2}分{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
LB_RunTime.Text = strTimeSpan; //显示到lable
LB_CurrentTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
// this.Invoke(new UpdateUICallBack(Update_UIEvent));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void insert2Center()
{
try
{
Thread thread1 = new Thread(new ThreadStart(thread1_Run));
thread1.Start();
Thread thread2 = new Thread(new ThreadStart(thread2_Run));
thread2.Start();
Thread thread3 = new Thread(new ThreadStart(thread3_Run));
thread3.Start();
Thread thread4 = new Thread(new ThreadStart(thread4_Run));
thread4.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} }
public void thread1_Run()
{
try
{
string strResult1 = WebService.XianKeShengXian.SuperMarBaseInfoWebService.Insert();
thread1Count++;
LB_BaseInfo_Count.Text = thread1Count.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void thread2_Run()
{
//对表2的插入
}
public void thread3_Run()
{
//对表3的插入
}
public void thread4_Run()
{
//对表4的插入
}
public void timer_Set()
{
timer.Interval = timeSpan;
timer.Tick += new EventHandler(timer_Event);
}
public void timer_Start()
{
try
{
timer.Start();
timeStart = DateTime.Now;
timeCurrent = timeStart;
LB_StartTime.Text = timeStart.ToString("yyyy-MM-dd hh:mm:ss");
LB_CurrentTime.Text = timeCurrent.ToString("yyyy-MM-dd hh:mm:ss");
timeSum = 0;
string strTimeSpan = string.Format("{0}天{1}时{2}分{3}秒", 0, 0, 0, 0);
LB_RunTime.Text = strTimeSpan;
LB_Tip.Text = "当前系统正在运行"; thread1Count = 0;
thread2Count = 0;
thread3Count = 0;
thread4Count = 0;
LB_BaseInfo.Text = "运行中";
LB_SupplierBaseInfo.Text = "运行中";
LB_MeatEntryInfo.Text = "运行中";
LB_VegEntryInfo.Text = "运行中";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void timer_Stop()
{
try
{
timer.Stop();
LB_Tip.Text = "当前系统已停止运行"; LB_BaseInfo.Text = "已停止";
LB_SupplierBaseInfo.Text = "已停止";
LB_MeatEntryInfo.Text = "已停止";
LB_VegEntryInfo.Text = "已停止";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} }
private void Btn_Start_Click(object sender, EventArgs e)
{
.....
} private void Btn_Stop_Click(object sender, EventArgs e)
{
......
}
}
解决方案 »
- seo到底是怎么样做的?来着有分。
- 向高手求救:The process cannot access the file 'X:\X.XXX' because it is being used by another process
- C#操作excel 如何将单元格内容设置为支持html格式的文本?能做到吗?
- 请问在SqlDataAdapter中能不能用带参数的SQL语句进行查询
- Repeater嵌套,CheckBox选择问题,请帮助
- 打开一个没有最大最小化标识的窗体用哪个教本或者函数?
- 一个index.aspx 页内有 2 个Button: Button1 和 Button2,同时还有4个TextBox tb1,tb2,tb3,tb4,我如何在按了 Button1按钮时,只对tb1和tb
- ASP.NET(2005)treeview如何实现接点拖动,最好有源代码,谢谢
- GridView如何不用SqlDataSource,还可以使用编辑功能?
- asp.net过期后怎么处理
- 一个简单的Sql查询语句,忘赐教!
- 问个比较简单,但是我目前不知道的问题(泛解析)
private setLabelTextDelegate setLabelText;//定义委托变量
public Form1()
{
InitializeComponent();
this.setLabelText = this.SetLableText
}
private void ThreadMethod()
{
this.label1.Invoke(this.setLabelText);//setLabelText为上面定义的委托变量
}
System.Threading.Thread setLabelTextThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.SetLableText));//这个放到你要访问控件的方法或者事件里面
BeginInvoke+EndInvoke+CallBack
system.window.form.timer是基于UI的,是单线程的
你可以在一个system.timer.timer里面创建多个线程,在线程里面调用invoke
如:
private delegate void SetLabelTextHandler(Label lableInvoke, string text);
private void SetLabelText(Label lableInvoke, string text)
{
try
{
// 如果返回 True ,则访问控件的线程不是创建控件的线程
if (lableInvoke.InvokeRequired)
{
SetLabelTextHandler handler = new SetLabelTextHandler(SetLabelText);
//if (!this.IsDisposed)//注意:要判断窗体是否已销毁,否则会出现,异常提示:“在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke”
if (this.IsHandleCreated)
{
this.BeginInvoke(handler, new object[] { lableInvoke, text });
}
}
else
{
lableInvoke.Text = text;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}