Commit e70d8cea authored by shaxuezheng's avatar shaxuezheng

解决线程占用,界面卡死

parent 0b293b14
...@@ -93,6 +93,7 @@ ...@@ -93,6 +93,7 @@
<Compile Include="application\PoolModbusSet.cs" /> <Compile Include="application\PoolModbusSet.cs" />
<Compile Include="application\ModbusToPool.cs" /> <Compile Include="application\ModbusToPool.cs" />
<Compile Include="application\Pond.cs" /> <Compile Include="application\Pond.cs" />
<Compile Include="application\UsThread.cs" />
<Compile Include="MessageFormat\ModbusComm.cs" /> <Compile Include="MessageFormat\ModbusComm.cs" />
<Compile Include="Modular\414P.cs" /> <Compile Include="Modular\414P.cs" />
<Compile Include="Modular\478C.cs" /> <Compile Include="Modular\478C.cs" />
......
...@@ -164,7 +164,7 @@ namespace ModbusDemo.application ...@@ -164,7 +164,7 @@ namespace ModbusDemo.application
} }
catch (Exception exception) catch (Exception exception)
{ {
Form1.totxt.Log("Bump Open Operation Error"); Form1.totxt.Log(exception+"Bump Open Operation Error");
throw; throw;
} }
} }
...@@ -182,7 +182,7 @@ namespace ModbusDemo.application ...@@ -182,7 +182,7 @@ namespace ModbusDemo.application
} }
catch (Exception exception) catch (Exception exception)
{ {
Form1.totxt.Log("Bump Close Operation Error"); Form1.totxt.Log(exception+"Bump Close Operation Error");
throw; throw;
} }
} }
...@@ -195,7 +195,7 @@ namespace ModbusDemo.application ...@@ -195,7 +195,7 @@ namespace ModbusDemo.application
} }
catch (Exception exception) catch (Exception exception)
{ {
Form1.totxt.Log("Bump Read Status Error"); Form1.totxt.Log(exception+"Bump Read Status Error");
throw; throw;
} }
} }
...@@ -220,7 +220,7 @@ namespace ModbusDemo.application ...@@ -220,7 +220,7 @@ namespace ModbusDemo.application
} }
catch (Exception exception) catch (Exception exception)
{ {
Form1.totxt.Log("Agitator Open Operation Error"); Form1.totxt.Log(exception +"Agitator Open Operation Error");
throw; throw;
} }
} }
...@@ -238,7 +238,7 @@ namespace ModbusDemo.application ...@@ -238,7 +238,7 @@ namespace ModbusDemo.application
} }
catch(Exception exception) catch(Exception exception)
{ {
Form1.totxt.Log("Agitator Close Operation Error"); Form1.totxt.Log(exception+"Agitator Close Operation Error");
throw; throw;
} }
} }
...@@ -251,7 +251,7 @@ namespace ModbusDemo.application ...@@ -251,7 +251,7 @@ namespace ModbusDemo.application
} }
catch (Exception exception) catch (Exception exception)
{ {
Form1.totxt.Log("Agitator Read Status Error"); Form1.totxt.Log(exception+"Agitator Read Status Error");
throw; throw;
} }
} }
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ModbusDemo.application
{
}
...@@ -227,10 +227,6 @@ ...@@ -227,10 +227,6 @@
this.txtReceiveMessage.TabIndex = 77; this.txtReceiveMessage.TabIndex = 77;
this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged); this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged);
// //
// modbus_Timer
//
this.modbus_Timer.Tick += new System.EventHandler(this.Modbus_Timer_Tick);
//
// updateops // updateops
// //
this.updateops.Controls.Add(this.imagesname); this.updateops.Controls.Add(this.imagesname);
......
...@@ -25,7 +25,7 @@ namespace ModbusDemo ...@@ -25,7 +25,7 @@ namespace ModbusDemo
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
bool Debug_test = true;//打开测试功能 bool Debug_test = false;//打开测试功能
_414P rtd = new _414P(); _414P rtd = new _414P();
_478C state = new _478C(); _478C state = new _478C();
_418X Analog = new _418X(); _418X Analog = new _418X();
...@@ -231,8 +231,11 @@ namespace ModbusDemo ...@@ -231,8 +231,11 @@ namespace ModbusDemo
.WithAtMostOnceQoS() .WithAtMostOnceQoS()
.WithRetainFlag(false) .WithRetainFlag(false)
.Build(); .Build();
if (mqttData != null)
{
await mqttClient.PublishAsync(message); await mqttClient.PublishAsync(message);
} }
}
private async Task Back() private async Task Back()
{ {
var message = new MqttApplicationMessageBuilder() var message = new MqttApplicationMessageBuilder()
...@@ -242,6 +245,7 @@ namespace ModbusDemo ...@@ -242,6 +245,7 @@ namespace ModbusDemo
.WithRetainFlag(false) .WithRetainFlag(false)
.Build(); .Build();
await mqttClient.PublishAsync(message); await mqttClient.PublishAsync(message);
totxt.Log("我在247,执行了Back");
} }
private async Task Subscribe() private async Task Subscribe()
{ {
...@@ -352,7 +356,7 @@ namespace ModbusDemo ...@@ -352,7 +356,7 @@ namespace ModbusDemo
Invoke((new Action(() => Invoke((new Action(() =>
{ {
txtReceiveMessage.AppendText("正在尝试重新连接" + Environment.NewLine); txtReceiveMessage.AppendText("正在尝试重新连接" + Environment.NewLine);
//totxt.Log("正在尝试重新连接" + Environment.NewLine); totxt.Log("正在尝试重新连接" + Environment.NewLine);
//Console.WriteLine("正在尝试重新连接" + Environment.NewLine); //Console.WriteLine("正在尝试重新连接" + Environment.NewLine);
...@@ -385,7 +389,7 @@ namespace ModbusDemo ...@@ -385,7 +389,7 @@ namespace ModbusDemo
Invoke((new Action(() => Invoke((new Action(() =>
{ {
txtReceiveMessage.AppendText("已下线!" + Environment.NewLine); txtReceiveMessage.AppendText("已下线!" + Environment.NewLine);
//totxt.Log("已下线!" + Environment.NewLine); totxt.Log("已下线!" + Environment.NewLine);
//Console.WriteLine("已下线!" + Environment.NewLine); //Console.WriteLine("已下线!" + Environment.NewLine);
}))); })));
} }
...@@ -397,13 +401,13 @@ namespace ModbusDemo ...@@ -397,13 +401,13 @@ namespace ModbusDemo
{ {
//收到的消息啦 //收到的消息啦
txtReceiveMessage.AppendText($">> {"### RECEIVED APPLICATION MESSAGE ###"}{Environment.NewLine}"); txtReceiveMessage.AppendText($">> {"### RECEIVED APPLICATION MESSAGE ###"}{Environment.NewLine}");
//totxt.Log($">> {"### RECEIVED APPLICATION MESSAGE ###"}{Environment.NewLine}"); totxt.Log($">> {"### RECEIVED APPLICATION MESSAGE ###"}{Environment.NewLine}");
}))); })));
Invoke((new Action(() => Invoke((new Action(() =>
{ {
//我的订阅主题是。。。。。 //我的订阅主题是。。。。。
txtReceiveMessage.AppendText($">> Topic = {e.ApplicationMessage.Topic}{Environment.NewLine}"); txtReceiveMessage.AppendText($">> Topic = {e.ApplicationMessage.Topic}{Environment.NewLine}");
//totxt.Log($">> Topic = {e.ApplicationMessage.Topic}{Environment.NewLine}"); totxt.Log($">> Topic = {e.ApplicationMessage.Topic}{Environment.NewLine}");
}))); })));
Invoke((new Action(async () => Invoke((new Action(async () =>
{ {
...@@ -411,7 +415,7 @@ namespace ModbusDemo ...@@ -411,7 +415,7 @@ namespace ModbusDemo
string passageway; string passageway;
//消息的内容 //消息的内容
txtReceiveMessage.AppendText($">> Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}"); txtReceiveMessage.AppendText($">> Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}");
//totxt.Log($">> Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}"); totxt.Log($">> Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}");
Console.WriteLine(Encoding.UTF8.GetString(e.ApplicationMessage.Payload)); Console.WriteLine(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
Ctrl ctrl = JsonConvert.DeserializeObject<Ctrl>(Encoding.UTF8.GetString(e.ApplicationMessage.Payload)); Ctrl ctrl = JsonConvert.DeserializeObject<Ctrl>(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
Ctrl ctrl_Back = new Ctrl(); Ctrl ctrl_Back = new Ctrl();
...@@ -466,7 +470,7 @@ namespace ModbusDemo ...@@ -466,7 +470,7 @@ namespace ModbusDemo
ctrl_Back.taskId = ctrl.taskId.ToString(); ctrl_Back.taskId = ctrl.taskId.ToString();
ctrl_Back_ss[0] = ctrl.ss[0]; ctrl_Back_ss[0] = ctrl.ss[0];
totxt.Log("我在469_ctrl_Back_ss[0] = ctrl.ss[0];");
//ctrl_Back.ss[0].op.setStartTime(long.Parse(GetTimeStamp())); //ctrl_Back.ss[0].op.setStartTime(long.Parse(GetTimeStamp()));
if (ctrl.ss[0].op.getStartTime() != 0) if (ctrl.ss[0].op.getStartTime() != 0)
...@@ -501,6 +505,7 @@ namespace ModbusDemo ...@@ -501,6 +505,7 @@ namespace ModbusDemo
txtReceiveMessage.AppendText($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}"); txtReceiveMessage.AppendText($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}");
//totxt.Log($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}"); //totxt.Log($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}");
}))); })));
mqttData = null ;
} }
public async Task ThreadMethodTxtAsync(int n) public async Task ThreadMethodTxtAsync(int n)
...@@ -530,6 +535,7 @@ namespace ModbusDemo ...@@ -530,6 +535,7 @@ namespace ModbusDemo
#endregion #endregion
#region modbus线程 #region modbus线程
private void GetData() private void GetData()
{ {
timer.Interval = 5000; timer.Interval = 5000;
...@@ -539,9 +545,9 @@ namespace ModbusDemo ...@@ -539,9 +545,9 @@ namespace ModbusDemo
timer.Elapsed += (o, a) => timer.Elapsed += (o, a) =>
{ {
SetData(); SetData();
//ShowMessage(string.Format("更新时间:" + DateTime.Now));
}; };
} }
private void Sendout() private void Sendout()
{ {
Control.CheckForIllegalCrossThreadCalls = false; Control.CheckForIllegalCrossThreadCalls = false;
...@@ -554,12 +560,11 @@ namespace ModbusDemo ...@@ -554,12 +560,11 @@ namespace ModbusDemo
try try
{ {
await Publish(); await Publish();
//totxt.Log(mqttData + Environment.NewLine + "\n"); totxt.Log(mqttData + Environment.NewLine + "\n");
} }
catch (Exception ex) catch (Exception ex)
{ {
//ShowMessage(string.Format("更新时间:" + DateTime.Now)); totxt.Log(ex + "," + Environment.NewLine + "\n");
//totxt.Log(ex + "," + Environment.NewLine + "\n");
return; return;
} }
}; };
...@@ -567,15 +572,19 @@ namespace ModbusDemo ...@@ -567,15 +572,19 @@ namespace ModbusDemo
//声明委托 //声明委托
private delegate void SetDataDelegate(); private delegate void SetDataDelegate();
private void SetData() private async void SetData()
{ {
if (this.InvokeRequired) if (this.InvokeRequired)
{ {
this.Invoke(new SetDataDelegate(SetData)); this.Invoke(new SetDataDelegate(SetData));
} }
else else
{
await Task.Run(() =>
{ {
Modbus_polling(); Modbus_polling();
});
} }
} }
...@@ -1630,6 +1639,9 @@ namespace ModbusDemo ...@@ -1630,6 +1639,9 @@ namespace ModbusDemo
await Subscribe(); await Subscribe();
} }
private MethodInvoker simpleDelegate1;
private MethodInvoker MQTT_thread;
private void btOpenCOM_Click(object sender, EventArgs e) private void btOpenCOM_Click(object sender, EventArgs e)
{ {
//if (Debug_test == true) //if (Debug_test == true)
...@@ -1676,12 +1688,10 @@ namespace ModbusDemo ...@@ -1676,12 +1688,10 @@ namespace ModbusDemo
comPort.Open(); comPort.Open();
isReconnect = true; isReconnect = true;
SetMqtt(); SetMqtt();
Thread t = new Thread(new ThreadStart(GetData)); simpleDelegate1 = new MethodInvoker(GetData);
t.IsBackground = false; simpleDelegate1.BeginInvoke(null, null);
t.Start(); MQTT_thread = new MethodInvoker(Sendout);
Thread MQTT_thread = new Thread(new ThreadStart(Sendout)); MQTT_thread.BeginInvoke(null,null);
MQTT_thread.IsBackground = true;
MQTT_thread.Start();
Task.Run(async () => { await ConnectMqttServerAsync(); }); Task.Run(async () => { await ConnectMqttServerAsync(); });
master = ModbusSerialMaster.CreateRtu(comPort); master = ModbusSerialMaster.CreateRtu(comPort);
master.Transport.Retries = 0; //重试次数 master.Transport.Retries = 0; //重试次数
...@@ -1714,9 +1724,36 @@ namespace ModbusDemo ...@@ -1714,9 +1724,36 @@ namespace ModbusDemo
totxt.Log(DateTime.Now.ToString() + " =>Disconnect " + comPort.PortName); totxt.Log(DateTime.Now.ToString() + " =>Disconnect " + comPort.PortName);
} }
private void Modbus_Timer_Tick(object sender, EventArgs e)
public class ThreadDemoClass
{
public void Run1(object obj)
{
#region 注释部分
//string name = obj as string;
//Console.WriteLine();
//Console.WriteLine("Child thread working...");
//Console.WriteLine("My name is " + name);
//Console.WriteLine("Child thread ID is:" + Thread.CurrentThread.ManagedThreadId.ToString());
#endregion
}
public void Run2(object obj)
{ {
#region 注释部分
//UserInfo userInfo = (UserInfo)obj;
//Console.WriteLine();
//Console.WriteLine("Child thread working...");
//Console.WriteLine("My name is " + userInfo.Name);
//Console.WriteLine("I'm " + userInfo.Age + " years old this year");
//Console.WriteLine("Child thread ID is:" + Thread.CurrentThread.ManagedThreadId.ToString());
#endregion
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment