[C#]使用BackgroudWorker刷新UI延迟的解决方法
今天使用BackgroundWorker刷新UI发生延时现象,找了好久才发现AutoResetEvent可以解决,代码如下
private void BgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int i = e.ProgressPercentage;
var str = "{'parameter':{'items':[{'lotId':'" + lstbar.Items[i] + "','parameterSeq':'0','parameterId':'FLD_GRADE','parameterValue':'A'}],'collectionType':'BY_LOT'}}";
var s = mes.dispatchLotForFR("M18", txtUserName.Text, txtDeviceName.Text.Trim(), "[\"" + lstbar.Items[i] + "\"]", str, false);
JObject jObject = (JObject)JsonConvert.DeserializeObject(s, jSettitng);
bool success = Convert.ToBoolean(jObject["success"]);
string msg = jObject["msg"].ToString();
lblDetail.Text = msg;
try
{
//记录MES过站
LotInfo lot = new LotInfo();
if (success)
{
lot.Success = true;
lblResult.Text = "过站成功";
lblResult.ForeColor = Color.Blue;
lblOK.Text = (++iNumOk).ToString();
iWorkerOK++;
WriteListBoxToFile(string.Format("组件序列号[{0}]过站[成功]消息[{1}],成功总计:[{2}]", lstbar.Items[i], msg,iNumOk));
}
else
{
lot.Success = false;
lblResult.Text = "过站失败";
lblResult.ForeColor = Color.Red;
lblNG.Text = (++iNumNG).ToString();
iWorkerNG++;
WriteListBoxToFile(string.Format("组件序列号[{0}]过站[失败]消息[{1}],失败总计[{2}]", lstbar.Items[i], msg,iNumNG));
sbNG.AppendLine(lstbar.Items[i].ToString());
} lot.LotNo = lstbar.Items[i].ToString();
//直接用设备号
//lot.DeviceNo= txtDeviceName.Text.Substring(4, 1) + txtDeviceName.Text.Substring(8, 2);
lot.DeviceNo = txtDeviceName.Text.Trim();
lot.Msg = msg;
lot.CreateTime = DateTime.Now;
if (tSet.IsInsertDb)
{
int k = lot.InsertDispatchLot(lot);
if (k > )
{
WriteListBoxToFile(string.Format("写入MES数据库成功,序列号:[{0}],设备号:[{1}],Success:[{2}],Msg:[{3}],时间:[{4}]", lot.LotNo, lot.DeviceNo, lot.Success, lot.Msg, lot.CreateTime));
}
else
{
WriteListBoxToFile(string.Format("写入MES数据库失败,序列号:[{0}],设备号:[{1}],Success:[{2}],Msg:[{3}],时间:[{4}]", lot.LotNo, lot.DeviceNo, lot.Success, lot.Msg, lot.CreateTime));
log.WriteExceptionLog(string.Format("写入MES数据库失败,序列号:[{0}],设备号:[{1}],Success:[{2}],Msg:[{3}],时间:[{4}]", lot.LotNo, lot.DeviceNo, lot.Success, lot.Msg, lot.CreateTime));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
WriteListBoxToFile(ex.Message);
log.WriteExceptionLog(ex.Message);
}
waitHandler.Set();
}
private void btnDispatch_Click(object sender, EventArgs e)
{
btnDispatch.Enabled = false;
if (txtDeviceName.Text.Length < )
{
MessageBox.Show("设备号名称长度不能小于10个字符");
btnDispatch.Enabled = true;
lstbar.Items.Clear();
return;
} if (lstbar.Items.Count > )
{
if (bgWorker.IsBusy != true)
{
waitHandler = new AutoResetEvent(false);
bgWorker.RunWorkerAsync(lstbar.Items.Count);
}
}
else
{
MessageBox.Show("列表框内至少存在1个条码才可以过站");
}
btnDispatch.Enabled = true;
}
private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = ; i < (int)e.Argument; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
if (tSet.ScanMode == "S")
{
System.Threading.Thread.Sleep();
}
else
{
System.Threading.Thread.Sleep(tSet.IntervalTime);
}
worker.ReportProgress(i);
waitHandler.WaitOne();
}
}
}
这个方法也不是我想出来的,参考了下面的大神
使用BackgroundWorker时,出现ProgressChanged延迟的现象,请帮分析下原因
[C#]使用BackgroudWorker刷新UI延迟的解决方法的更多相关文章
- QT visual stuido 集成插件不能打开ui文件的解决方法(去掉xml的UTF8标记)
QT visual stuido 集成插件不能打开ui文件的解决方法 visual studio里不能打开这个ui文件,出现warning等解决方法是:于是将<?xml version=&quo ...
- C# Winform频繁刷新导致界面闪烁解决方法
C#Winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供的默认双缓冲将 ...
- winform频繁刷新导致界面闪烁解决方法
转自龙心文 原文 winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供 ...
- Linux-485收发切换延迟的解决方法
[前言] 本文引用各种资料甚多,而引用出处标明并不详细,若有侵权,请联系删除. 转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10381616.html ...
- Unity中UI界面颤抖解决方法
将Render Mode中属性改为Screen Space - Camera 摄像机挂在Canvas属性下会出现UI界面颤抖的效果. UI界面颤抖解决方式:将Render Mode中属性改为Scree ...
- 前端vue项目部署到tomcat,一刷新报错404解决方法
公司前端写的后台部署到tomcat webapps目录下后,无法进行刷新,一刷新就会报错404,自动跳的404页面.在网上查了下,官方说是HTML5 History 模式引发的问题,但是解决方案中,并 ...
- 鼠标聚焦到Text输入框时,按回车键刷新页面原因及解决方法
前提 一个form中只有一个输入框,当输入框获取焦点后,点击回车,导致整个页面都刷新,问题解决办法. 1.处理form 在form中添加事件 <form onsubmit="retu ...
- Unity中UGUI鼠标穿透UI问题的解决方法
不过在使用时需要先获取两个红色显示的变量,graphicRaycaster和eventSystem. 这两个变量分别对应的是Canvas中的GraphicRaycaster组件和创建UI时自动生成的“ ...
- easyui refresh 刷新两次的解决方法(推荐)
//这样写刷新两次 $("#windowid").window('refresh','url01.php'); $("#windowid").window('o ...
随机推荐
- Leetcode题目79.单词搜索(回溯+DFS-中等)
题目描述: 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许 ...
- Process.Net
ProcessSharp的构造函数,对应的测试是 https://github.com/lolp1/Process.NET/blob/master/test/Process.NET.Test/Core ...
- ansible-playbook的YAML语法学习
YAML:可以将你打算对多机器的批量操作放到一个文件中,顺序执行,可以根据机器做到根据机器信息判断执行,其他命令执行结果判断执行. YAML有着严格的层级要求,稍微有个缩进问题就会无法运行,所以学习过 ...
- springboot properties
Spring-boot中Conditional介绍 https://blog.csdn.net/tanga842428/article/details/78615070springBoot----@C ...
- highcharts 代码都对,但是不显示图片
$('#container').highcharts({ chart: { zoomType: 'xy' }, title: { text: 'Average Monthly Temperature ...
- Hibernate3主键生成策略
identity 采用数据库生成的主键,用于为long.short.int类型生成唯一标识, Oracle 不支持自增字段. <id name="id" column=&qu ...
- python -v 和-V
python -v 小写v:这是版本信息,包括库版本 python -V 大写v:只看python的版本
- python之scrapy爬取某集团招聘信息
1.创建工程 scrapy startproject gosuncn 2.创建项目 cd gosuncn scrapy genspider gaoxinxing gosuncn.zhiye.com 3 ...
- PHP批量写入数据、批量删除数据
批量插入可以参考$sql = "insert into data (id,ip,data) values ";for($i=0;$i<100;$i++){$sqls[]=& ...
- View的介绍和运用 && FlexBox布局
开始我们今天的项目学习啦~~~~~~ 1> 第一步当然是创建项目啦: 进入终端: 创建ViewDemo项目 命令如下啦,你看懂了对吧...嘻嘻!!! 2>View的介绍和运用 项目安装创建 ...