线程问题 在编辑一个控件的时候,报控件不在该线程的控制 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 子线程访问UI线程要用委托来访问:最方便的方法: 1,把要调用的控件 访问权限为public; 2,在你的那个线程类中定义一个字段 public Form1 form1; 3,在你Form1 f=new Form1(); 的地方顺便将窗体对象传递到线程的那个类(假如那个类叫ThreadClass) ThreadClass threadClass=new ThreadClass(); threadClass.form1=f; 4,在ThreadClass这个类定义个委托,委托方法中用control.invoke访问。 //跨线程访问UI控件的委托 public delegate void InvokeDelegate(); //不带参数 //带参数 public delegate void InvokeDelegateParameter(string address, string port); this.frmgps.listBox1.Invoke(new InvokeDelegate(DisplayStart)); private void DisplayStart()//委托方法 { this.frmgps.listBox1.Items.Add(DateTime.Now.ToLongTimeString() + " 开始监听 终端4 " + this.port.ToString() + " 端口数据..."); this.frmgps.listBox1.SetSelected(this.frmgps.listBox1.Items.Count - 1, true); } 去看下這個,大概跟這個一樣http://topic.csdn.net/u/20090722/10/f5374d22-554e-41b9-81c1-d5645e1bc909.html?89227 我应用程序启动的是 FrmBasic 这个类 ,然后定义线程 Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 而我的控件是在这个类里定义的 ,并且在这个类里调用。怎么会不在这个线程里呢 还可以用VS2005以上版本自带控件Backgroundworker的ReportPorcess事件来实现实时操作前台控件。 参见CSDN How to: Make Thread-Safe Calls to Windows Forms Controls 那是因为.net1.1可以不需要跨线程。。2.0以后的就要用Control.Invoke()访问。为了兼容1.1在编译阶段是允许的,,运行的时候就会出错 要用到委托: 希望对楼主有所启发 delegate void SetTextCallback(string text); private void SetText(string text) { if (this.inforMg.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.inforMg.Text = text; } } 异步线程操作的话最好用BackGroundWorker或者设置属性FrmBasic .CheckForIllegalCrossThreadCalls = false; 请问如何使C#主窗体Form1调用子窗体Form2中textbox内容 动态调整div高度,---附图加代码,求详解 谁使用过VS2010的插入代码片断,一个小问题 怎样判断c sharp 中datagridview中有没有重复的数据? 全局变量声明的疑问 类间变量使用问题 The powerful runtime form designer 权限问题 WebBrowser控件怎样可以截获浏览器发送出去的请求 哪里有C#做的服务程序供下载,及说明? 创建用户控件,怎么将其中的某一控件设为容器,且调用时添加控件不用挡住。 关于AJAX的ModalPopupExtender里有3个按钮
1,把要调用的控件 访问权限为public;
2,在你的那个线程类中定义一个字段 public Form1 form1;
3,在你Form1 f=new Form1(); 的地方顺便将窗体对象传递到线程的那个类(假如那个类叫ThreadClass)
ThreadClass threadClass=new ThreadClass();
threadClass.form1=f;
4,在ThreadClass这个类定义个委托,委托方法中用control.invoke访问。
//跨线程访问UI控件的委托
public delegate void InvokeDelegate(); //不带参数
//带参数
public delegate void InvokeDelegateParameter(string address, string port); this.frmgps.listBox1.Invoke(new InvokeDelegate(DisplayStart)); private void DisplayStart()//委托方法
{
this.frmgps.listBox1.Items.Add(DateTime.Now.ToLongTimeString() + " 开始监听 终端4 " + this.port.ToString() + " 端口数据...");
this.frmgps.listBox1.SetSelected(this.frmgps.listBox1.Items.Count - 1, true);
}
http://topic.csdn.net/u/20090722/10/f5374d22-554e-41b9-81c1-d5645e1bc909.html?89227
那是因为.net1.1可以不需要跨线程。。
2.0以后的就要用Control.Invoke()访问。
为了兼容1.1在编译阶段是允许的,,运行的时候就会出错
希望对楼主有所启发
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.inforMg.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.inforMg.Text = text;
}
}
或者
设置属性FrmBasic .CheckForIllegalCrossThreadCalls = false;