当您试图从单独的线程更新一个win form时,您将得到如下错误信息: 
"Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on." 
 
本文将介绍如何处理此错误:

问题:

 
重现该错误, 添加一个 progress bar 控件 (progressbar1) 以及一个  button(btnStart)到您的窗体上:.

private void btnStart_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;

System.Threading.Thread t1 = new System.Threading.Thread(startProgress);
t1.Start();
}
void startProgress()
{
for (int i = 0; i {
progressBar1.Value = i; //You will get error at this line
System.Threading.Thread.Sleep(100);
}
}

window.google_render_ad();

解决方案:

private void btnStart_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;

System.Threading.Thread t1 = new System.Threading.Thread(startProgress);
t1.Start();
}
void startProgress()
{
for (int i = 0; i {
SetControlPropertyValue(progressBar1, "value", i); //This is a thread safe method
System.Threading.Thread.Sleep(100);
}
}

delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
if (oControl.InvokeRequired)
{
SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
oControl.Invoke(d, new object[] { oControl, propName, propValue });
}
else
{
Type t = oControl.GetType();
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo p in props)
{
if (p.Name.ToUpper() == propName.ToUpper())
{
p.SetValue(oControl, propValue, null);
}
}
}
}

您可以通过该解决方案来处理所有的WIndows 控件. 您所要做的是, 从上方的代码中copy SetControlValueCallback delegate 以及SetControlPropertyValue 函数 function.

例如您想设置一个 label的内容, 使用SetControlPropertyValueSetControlPropertyValue(Label1, "Text", i.ToString());

 

请确认您所应用的属性的值类型.在上面的Demo中 Text 是一个 string 属性. 这就是我为什么将其转换为String。

【转发】Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on的更多相关文章

  1. linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决

    linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决 在linux在需要使用c++11时会遇到 ...

  2. Enable multithreading to use std::thread: Operation not permitted问题解决

    在用g++ 4.8.2编译C++11的线程代码后,运行时遇到了如下报错: terminate called after throwing an instance of 'std::system_err ...

  3. 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常

    在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应 ...

  4. Jmeter Thread Group中如果存在HTTP request执行失败,就对整个Thread Group重新执行,限定最大执行次数N次

    由于在对WEB系统进行自动化测试的过程中,经常会由于握手连接断开等原因导致HTTP请求发送失败,如果重新执行一次,会是成功的.在每天的自动化冒烟测试过程中,生成在测试报告存在误报,严重浪费了测试人员确 ...

  5. 解决ArcEngine开发程序“假死”现象

    在GIS数据处理中,数据量大是一个非常伤脑筋的问题.最近,在写一个CAD注记转Shapefile文件时,又遇到这个问题. 曾经处理一次数据,达130万个点,即测试区域内的栅格转成点全部处理,程序是写好 ...

  6. C# WinForm多线程(一)Thread类库

    Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括 ...

  7. C# WinForm多线程(一)----- Thread类库

    Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括 ...

  8. C#多线程操作界面控件的解决方案(转)

    C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...

  9. WinForm多线程学习文档

    基础篇 怎样创建一个线程 受托管的线程与 Windows线程 前台线程与后台线程 名为BeginXXX和EndXXX的方法是做什么用的 异步和多线程有什么关联 WinForm多线程编程篇 我的多线程W ...

随机推荐

  1. C# 远程网络唤醒介绍及代码

    一.定义 网络唤醒:唤醒休眠状态下的计算机,而不是已关机的计算机. 优势:可通过定时功能实现自动唤醒计算机,减少人力使用. 实现方法:通过被唤醒机的MAC地址进行广播发送请求,唤醒计算机. 二.硬件设 ...

  2. An attempt was made to load a program with an incorrect format

      用.net调用一个C++ 32位的DLL, 编译的时候选择x86, 在部署到一个64位的机器上的时候报错:"An attempt was made to load a program w ...

  3. 随笔—邀请赛前训— Codeforces Round #330 (Div. 2) Vitaly and Night

    题意:给你很多对数,要么是0要么是1.不全0则ans++. 思路即题意. #include<cstdio> #include<cstring> #include<iost ...

  4. 比较详细PHP生成静态页面教程

    一,PHP脚本与动态页面. PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合, 也可以类,函数封装等形式,以模板的方式对用户请求进行处理.无论以何种方式,它的基本原理是这样的.由客 ...

  5. WebForm ASP开发方式、 IIS服务器以及WebForm开发基础

    网页端 B/S两种:(ASP.NET  --网站应用开发技术) WebForm:  微软最先出现的网站开发技术 MVC:  后来开发,为了让java.PHP转过来的开发人员更容易接受使用 客户端 C/ ...

  6. linux系统man命令用法和安装方法

    Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可. Linux的man手册共有以下几个章节: 代號 代表內容 1 使用者在shell中可以操作的指令或可 ...

  7. zoj1260 king

    题目描述:从前有一个王国,皇后怀孕了.她祈祷到:如果我的孩子是儿子,我希望他是一个健康的国王. 9 个月后,她的孩子出生了,的确,她生了一个漂亮的儿子.但不幸的是,正如皇室家庭经常发生的那样,皇后的儿 ...

  8. [aspx]控件及代码小例

    1. 原生 asp 方式遍历 DataTable  2. aspx 的控件 Repeater 后台绑定 <%-- DataTable dt = OleDbHelper.GetTable(&quo ...

  9. iconv命令 gbk 转 UTF-8

    -----linux gbk 转 UTF-8-------- iconv 用法 iconv -f "gbk" -t "utf-8" < infile &g ...

  10. Git克隆

    用法1:Git clone <repository> <directory> 将<repository>指向的版本库创建一个克隆到<directory> ...