今天使用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延迟的解决方法的更多相关文章

  1. QT visual stuido 集成插件不能打开ui文件的解决方法(去掉xml的UTF8标记)

    QT visual stuido 集成插件不能打开ui文件的解决方法 visual studio里不能打开这个ui文件,出现warning等解决方法是:于是将<?xml version=&quo ...

  2. C# Winform频繁刷新导致界面闪烁解决方法

    C#Winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供的默认双缓冲将 ...

  3. winform频繁刷新导致界面闪烁解决方法

    转自龙心文 原文 winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供 ...

  4. Linux-485收发切换延迟的解决方法

    [前言] 本文引用各种资料甚多,而引用出处标明并不详细,若有侵权,请联系删除. 转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10381616.html ...

  5. Unity中UI界面颤抖解决方法

    将Render Mode中属性改为Screen Space - Camera 摄像机挂在Canvas属性下会出现UI界面颤抖的效果. UI界面颤抖解决方式:将Render Mode中属性改为Scree ...

  6. 前端vue项目部署到tomcat,一刷新报错404解决方法

    公司前端写的后台部署到tomcat webapps目录下后,无法进行刷新,一刷新就会报错404,自动跳的404页面.在网上查了下,官方说是HTML5 History 模式引发的问题,但是解决方案中,并 ...

  7. 鼠标聚焦到Text输入框时,按回车键刷新页面原因及解决方法

    前提 一个form中只有一个输入框,当输入框获取焦点后,点击回车,导致整个页面都刷新,问题解决办法. 1.处理form  在form中添加事件 <form onsubmit="retu ...

  8. Unity中UGUI鼠标穿透UI问题的解决方法

    不过在使用时需要先获取两个红色显示的变量,graphicRaycaster和eventSystem. 这两个变量分别对应的是Canvas中的GraphicRaycaster组件和创建UI时自动生成的“ ...

  9. easyui refresh 刷新两次的解决方法(推荐)

    //这样写刷新两次 $("#windowid").window('refresh','url01.php'); $("#windowid").window('o ...

随机推荐

  1. Linux设备驱动程序 之 异步通知

    尽管大多数时候阻塞型和非阻塞型操作的组合以及select方法可以有效的查询设备,但是某些时候用这种技术处理就效率不搞了: 例如:一个进程在低优先级执行长的循环计算,但又需要尽可能快的处理输入数据,如果 ...

  2. 2018icpc 徐州h题

    题目大意: https://codeforces.com/gym/102012/problem/H?csrf_token=c9d0191a64a241166d54a565b1615125 区间[l , ...

  3. iconfont最基础使用

    iconfont最基础使用 一.总结 一句话总结: 你加入购物车然后下载好了图标之后,里面有demo_index.html,会非常详细的教你 Unicode/Font class/Symbol三种使用 ...

  4. PHP-生产随机验证码图片

    // <span style="white-space:pre"> </span>//因为要把产生的验证码保存到session中,此处为session开始 ...

  5. mac 配置apache

    Apache配置 对httpd.conf文件的配置.首先打开 /etc/apache2/httpd.conf文件,也就是Apache2.4的相关配置文件. 连接php 首先我们将相应的PHP版本配置进 ...

  6. Celery分布式队列学习

    1. celery介绍和使用 Celery 是一个 基于python开发的分布式异步消息任务队列(可以简单理解为python多进程或多线程中的queue),通过它可以轻松的实现任务的异步处理.cele ...

  7. ORACLE数据库黑/白名单

    编辑sqlnet.ora文件 #开启ip限制功能tcp.validnode_checking=yes#允许访问数据库的IP地址列表,多个IP地址使用逗号分开tcp.invited_nodes=(10. ...

  8. PHP学习之PHP代码的优化

    if代码块的优化 if(1===$orderState){     $status='success'; }else{     $status='error'; } return $status; 简 ...

  9. mongodb 安装配置及简单使用

    步骤一: 下载网址:https://www.mongodb.com/download-center/community 根据自己的环境下载 步骤二: 安装过程只需要默认即可,需要注意的是连接工具“mo ...

  10. 使用KerasNet

    1.安装Python3.6,必须是3.6因为当前KerasNet的配套版本是3.6 https://www.python.org/ftp/python/3.6.8/python-3.6.8-amd64 ...