怎么把线程里边信息 同步到界面 线程每个几秒回去数据库读取,是否有新的短消息,如果有就发送到串口猫每次读取数据库时,如果不能链接数据库,我想把状态显示到界面上;每次发送消息到串口,我也想把串口能否打开状态,显示到界面上;这个该怎么弄好一些? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 能否在Program.cs设置一个静态变量,然后线程里边写;界面timer里边读,同时显示在界面 using System.Timers; public delegate void AddCheck(string val);//委托 public int flag = 0; void ExecuteWork(object sender, ElapsedEventArgs e) { flag++; this.Invoke(new AddCheck(AddCheckHandler), new object[] { flag.ToString() }); } void AddCheckHandler(string val) { checkedListBox1.Items.Add(val); } System.Timers.Timer tm = new System.Timers.Timer(6000); tm.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteWork); tm.AutoReset = true; tm.Enabled = true;一个示例,看是否有帮助 没用过委托,具体该怎么弄?一般我也不会,所以就会去google Thread th = new Thread((ThreadStart)delegate(){ //... while(flag) { //后台操作 this.Invoke((Action)delegate() { label1.Text = "更新界面"; }); }});th.Start(); //新创建的线程不能访问UI线程创建的窗口控件 //如果需要访问窗口中的控件,就要在窗口构造函数中将CheckForIllegalCrossThreadCalls设置为 false System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; 別忘了在主面板load上+這個 委托如果不熟悉,可以用backgroundworker,这个比委托好理解些 winform控制台必须用委托,线程安全。 我看到网上例子,想用但是发现状态栏toolStripStatusLabel1没有invoke这个方法 private void btn_Start_Click(object sender, EventArgs e) { Thread myThread = new Thread(ThreadMethod); myThread.IsBackground = true; myThread.Start(); } /// <summary> /// 多线程执行指定方法 /// </summary> private void ThreadMethod() { while (true) { Thread.Sleep(100); //线程暂停100毫秒 ThreadStart(); } } private delegate void ThreadStartDelegate(); /// <summary> /// 线程方法 /// </summary> private void ThreadStart() { if (txt_Text.InvokeRequired) { ThreadStartDelegate d = ThreadStart; txt_Text.Invoke(d); } else { txt_Text.Text += "a"; } } 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 threadtest{ public partial class Form1 : Form { delegate void AddTextCallback(string text); AddTextCallback textCallback; public Form1() { InitializeComponent(); textCallback = new AddTextCallback(AddText); } private void AddText(string text) { if (txtNum.InvokeRequired) { this.Invoke(textCallback, text); } else { txtNum.Text = text; } } void ChangeTxt() { for (int i = 0; i < 1000; i++) { int a = int.Parse(txtNum.Text); a++; AddText(a.ToString()); } } private void btnProblem_Click(object sender, EventArgs e) { Thread thread = new Thread(ChangeTxt); thread.Name = "t1"; thread.IsBackground = true; thread.Start(); } }}看这个,txtNum可以为textbox,label 是的,好像不能是 fomr最下方状态栏,他没有Invoke这个方法 是的,好像不能是 fomr最下方状态栏,他没有Invoke这个方法==========================================没有的话换个别的,没有也用吗? 换个 textbox 可以用 你可以随便使用一个控件,包括Form本身,来执行Invoke。不是非得找什么toolStripStatusLabel1。另外,toolStripStatusLabel也根本不是控件。充其量你可以说它是组件。所有的控件都有Invoke。 比如说,我们可以在Form里边的某一个事件处理方法中写this.Invoke(new Action(()=> { var i=0; });这里,我们不过就是在窗体主线程中执行一个 i=0 赋值而已。我们可以把任何代码放到这里,例如可以写this.Invoke(new Action(()=> { toolStripStatusLabel1.BackColor = Colors.Blue; Label1.Text = "蓝色的"; });之类的。不是说你必须去找什么 toolStripStatusLabel1.Invoke(....)、Label1.Invoke(....)。你对 Invoke 的理解是错误的! 例如你可以写 Label1.Invoke(new Action(()=> { toolStripStatusLabel1.BackColor = Colors.Blue; textbox1.Text= "蓝色的"; })); [求教]如果让socket连接可以排队? 有什么访问数据库的第三方插件? 问候非常基础的问题 如何向WORD,excel,ppt窗口发送保存命令 如何更改 vs2003 编写代码时IDE的底色。 這段遞歸小程序有什麼錯? 不为秋香,我为工作(100相送) Window程序模板窗体如何做? 高分求救-关于EXCEL表格格式问题 请推荐一个小巧的C#编辑器 MySQLConnection open()用了5秒 求助谁遇到过这种情况
public delegate void AddCheck(string val);//委托
public int flag = 0;
void ExecuteWork(object sender, ElapsedEventArgs e)
{
flag++;
this.Invoke(new AddCheck(AddCheckHandler), new object[] { flag.ToString() });
}
void AddCheckHandler(string val)
{
checkedListBox1.Items.Add(val);
}
System.Timers.Timer tm = new System.Timers.Timer(6000);
tm.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteWork);
tm.AutoReset = true;
tm.Enabled = true;
一个示例,看是否有帮助
没用过委托,具体该怎么弄?一般我也不会,所以就会去google
{
//...
while(flag)
{
//后台操作
this.Invoke((Action)delegate()
{
label1.Text = "更新界面";
});
}
});
th.Start();
//如果需要访问窗口中的控件,就要在窗口构造函数中将CheckForIllegalCrossThreadCalls设置为 false
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
別忘了在主面板load上+這個
我看到网上例子,想用但是发现状态栏toolStripStatusLabel1没有invoke这个方法 private void btn_Start_Click(object sender, EventArgs e)
{
Thread myThread = new Thread(ThreadMethod);
myThread.IsBackground = true;
myThread.Start();
} /// <summary>
/// 多线程执行指定方法
/// </summary>
private void ThreadMethod()
{
while (true)
{
Thread.Sleep(100); //线程暂停100毫秒
ThreadStart();
}
} private delegate void ThreadStartDelegate();
/// <summary>
/// 线程方法
/// </summary>
private void ThreadStart()
{
if (txt_Text.InvokeRequired)
{
ThreadStartDelegate d = ThreadStart;
txt_Text.Invoke(d);
}
else
{
txt_Text.Text += "a";
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace threadtest
{
public partial class Form1 : Form
{
delegate void AddTextCallback(string text);
AddTextCallback textCallback; public Form1()
{
InitializeComponent();
textCallback = new AddTextCallback(AddText);
} private void AddText(string text)
{
if (txtNum.InvokeRequired)
{
this.Invoke(textCallback, text);
}
else
{
txtNum.Text = text;
}
} void ChangeTxt()
{
for (int i = 0; i < 1000; i++)
{
int a = int.Parse(txtNum.Text);
a++;
AddText(a.ToString());
}
} private void btnProblem_Click(object sender, EventArgs e)
{
Thread thread = new Thread(ChangeTxt);
thread.Name = "t1";
thread.IsBackground = true;
thread.Start();
}
}
}
看这个,txtNum可以为textbox,label
==========================================
没有的话换个别的,没有也用吗?
换个 textbox 可以用
{
var i=0;
});这里,我们不过就是在窗体主线程中执行一个 i=0 赋值而已。我们可以把任何代码放到这里,例如可以写this.Invoke(new Action(()=>
{
toolStripStatusLabel1.BackColor = Colors.Blue;
Label1.Text = "蓝色的";
});
之类的。不是说你必须去找什么 toolStripStatusLabel1.Invoke(....)、Label1.Invoke(....)。你对 Invoke 的理解是错误的!
{
toolStripStatusLabel1.BackColor = Colors.Blue;
textbox1.Text= "蓝色的";
}));