关于C# 通过MPI通讯协议,连接西门子plc300,经常卡死崩溃问题!!
现有一wpf程序,通过MPI通讯协议 连接西门子PLC300
连接成功后 开启2个线程监听两个M地址的值是否为true,如果为true则向本地数据库插入数据,
监听时间M1地址2秒,M2地址5秒
连接PLC成功后
isThreadRunFZ = true;
threadFZ = new Thread(ThreadReadServerFZ);
threadFZ.IsBackground = true;
threadFZ.Start();
isThreadRunZL = true;
threadZL = new Thread(ThreadReadServerZL);
threadZL.IsBackground = true;
threadZL.Start();
======================================================
private void ThreadReadServerZL()
{
while (isThreadRunZL)
{
Thread.Sleep(timeSleepZL);//2秒
try
{
bool saveType = false;
DCProdave.GetMBitData(150, 0, ref saveType);
if (saveType)//如果M地址值为true
{
Dispatcher.Invoke(new Action<short>(AddDataCurve), (short)1);//执行数据库插入方法
}
}
catch (Exception ex)
{
return;
//MessageBox.Show("读取失败1:" + ex.Message);
}
}
}
private void ThreadReadServerFZ()
{
while (isThreadRunFZ)
{
Thread.Sleep(timeSleepFZ);//5秒
try
{
bool saveTypeFZ = false;
DCProdave.GetMBitData(42, 0, ref saveTypeFZ);
if (saveTypeFZ)
{
if (PitNoFZ == "")
{
。。。访问webservice得到返回值。。
}
Dispatcher.Invoke(new Action<string>(AddDataCurveFZ), PitNoFZ);//插入数据库
}
else
{
。。。。。
}
}
catch (Exception ex)
{
//return;
//MessageBox.Show("读取失败2:" + ex.Message);
}
}
}
========================================================================
一直监听,只要点了wincc上某个按钮,改变M地址值,我就向数据库插入数据。
可是运行一会就程序崩溃。。根本就不catch任何错误,直接“xxx应用程序已停止工作” 欲哭无泪啊。。。。