Commit e70d8cea authored by shaxuezheng's avatar shaxuezheng

解决线程占用,界面卡死

parent 0b293b14
......@@ -93,6 +93,7 @@
<Compile Include="application\PoolModbusSet.cs" />
<Compile Include="application\ModbusToPool.cs" />
<Compile Include="application\Pond.cs" />
<Compile Include="application\UsThread.cs" />
<Compile Include="MessageFormat\ModbusComm.cs" />
<Compile Include="Modular\414P.cs" />
<Compile Include="Modular\478C.cs" />
......
......@@ -164,7 +164,7 @@ namespace ModbusDemo.application
}
catch (Exception exception)
{
Form1.totxt.Log("Bump Open Operation Error");
Form1.totxt.Log(exception+"Bump Open Operation Error");
throw;
}
}
......@@ -182,7 +182,7 @@ namespace ModbusDemo.application
}
catch (Exception exception)
{
Form1.totxt.Log("Bump Close Operation Error");
Form1.totxt.Log(exception+"Bump Close Operation Error");
throw;
}
}
......@@ -195,7 +195,7 @@ namespace ModbusDemo.application
}
catch (Exception exception)
{
Form1.totxt.Log("Bump Read Status Error");
Form1.totxt.Log(exception+"Bump Read Status Error");
throw;
}
}
......@@ -220,7 +220,7 @@ namespace ModbusDemo.application
}
catch (Exception exception)
{
Form1.totxt.Log("Agitator Open Operation Error");
Form1.totxt.Log(exception +"Agitator Open Operation Error");
throw;
}
}
......@@ -238,7 +238,7 @@ namespace ModbusDemo.application
}
catch(Exception exception)
{
Form1.totxt.Log("Agitator Close Operation Error");
Form1.totxt.Log(exception+"Agitator Close Operation Error");
throw;
}
}
......@@ -251,7 +251,7 @@ namespace ModbusDemo.application
}
catch (Exception exception)
{
Form1.totxt.Log("Agitator Read Status Error");
Form1.totxt.Log(exception+"Agitator Read Status Error");
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 @@
this.txtReceiveMessage.TabIndex = 77;
this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged);
//
// modbus_Timer
//
this.modbus_Timer.Tick += new System.EventHandler(this.Modbus_Timer_Tick);
//
// updateops
//
this.updateops.Controls.Add(this.imagesname);
......
......@@ -25,7 +25,7 @@ namespace ModbusDemo
{
public partial class Form1 : Form
{
bool Debug_test = true;//打开测试功能
bool Debug_test = false;//打开测试功能
_414P rtd = new _414P();
_478C state = new _478C();
_418X Analog = new _418X();
......@@ -231,7 +231,10 @@ namespace ModbusDemo
.WithAtMostOnceQoS()
.WithRetainFlag(false)
.Build();
await mqttClient.PublishAsync(message);
if (mqttData != null)
{
await mqttClient.PublishAsync(message);
}
}
private async Task Back()
{
......@@ -242,6 +245,7 @@ namespace ModbusDemo
.WithRetainFlag(false)
.Build();
await mqttClient.PublishAsync(message);
totxt.Log("我在247,执行了Back");
}
private async Task Subscribe()
{
......@@ -352,7 +356,7 @@ namespace ModbusDemo
Invoke((new Action(() =>
{
txtReceiveMessage.AppendText("正在尝试重新连接" + Environment.NewLine);
//totxt.Log("正在尝试重新连接" + Environment.NewLine);
totxt.Log("正在尝试重新连接" + Environment.NewLine);
//Console.WriteLine("正在尝试重新连接" + Environment.NewLine);
......@@ -385,7 +389,7 @@ namespace ModbusDemo
Invoke((new Action(() =>
{
txtReceiveMessage.AppendText("已下线!" + Environment.NewLine);
//totxt.Log("已下线!" + Environment.NewLine);
totxt.Log("已下线!" + Environment.NewLine);
//Console.WriteLine("已下线!" + Environment.NewLine);
})));
}
......@@ -397,13 +401,13 @@ namespace ModbusDemo
{
//收到的消息啦
txtReceiveMessage.AppendText($">> {"### RECEIVED APPLICATION MESSAGE ###"}{Environment.NewLine}");
//totxt.Log($">> {"### RECEIVED APPLICATION MESSAGE ###"}{Environment.NewLine}");
totxt.Log($">> {"### RECEIVED APPLICATION MESSAGE ###"}{Environment.NewLine}");
})));
Invoke((new Action(() =>
{
//我的订阅主题是。。。。。
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 () =>
{
......@@ -411,7 +415,7 @@ namespace ModbusDemo
string passageway;
//消息的内容
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));
Ctrl ctrl = JsonConvert.DeserializeObject<Ctrl>(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
Ctrl ctrl_Back = new Ctrl();
......@@ -466,7 +470,7 @@ namespace ModbusDemo
ctrl_Back.taskId = ctrl.taskId.ToString();
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()));
if (ctrl.ss[0].op.getStartTime() != 0)
......@@ -501,7 +505,8 @@ namespace ModbusDemo
txtReceiveMessage.AppendText($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}");
//totxt.Log($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}");
})));
}
mqttData = null ;
}
public async Task ThreadMethodTxtAsync(int n)
{
......@@ -530,6 +535,7 @@ namespace ModbusDemo
#endregion
#region modbus线程
private void GetData()
{
timer.Interval = 5000;
......@@ -539,9 +545,9 @@ namespace ModbusDemo
timer.Elapsed += (o, a) =>
{
SetData();
//ShowMessage(string.Format("更新时间:" + DateTime.Now));
};
}
private void Sendout()
{
Control.CheckForIllegalCrossThreadCalls = false;
......@@ -554,12 +560,11 @@ namespace ModbusDemo
try
{
await Publish();
//totxt.Log(mqttData + Environment.NewLine + "\n");
totxt.Log(mqttData + Environment.NewLine + "\n");
}
catch (Exception ex)
{
//ShowMessage(string.Format("更新时间:" + DateTime.Now));
//totxt.Log(ex + "," + Environment.NewLine + "\n");
totxt.Log(ex + "," + Environment.NewLine + "\n");
return;
}
};
......@@ -567,7 +572,7 @@ namespace ModbusDemo
//声明委托
private delegate void SetDataDelegate();
private void SetData()
private async void SetData()
{
if (this.InvokeRequired)
{
......@@ -575,7 +580,11 @@ namespace ModbusDemo
}
else
{
Modbus_polling();
await Task.Run(() =>
{
Modbus_polling();
});
}
}
......@@ -1630,6 +1639,9 @@ namespace ModbusDemo
await Subscribe();
}
private MethodInvoker simpleDelegate1;
private MethodInvoker MQTT_thread;
private void btOpenCOM_Click(object sender, EventArgs e)
{
//if (Debug_test == true)
......@@ -1676,12 +1688,10 @@ namespace ModbusDemo
comPort.Open();
isReconnect = true;
SetMqtt();
Thread t = new Thread(new ThreadStart(GetData));
t.IsBackground = false;
t.Start();
Thread MQTT_thread = new Thread(new ThreadStart(Sendout));
MQTT_thread.IsBackground = true;
MQTT_thread.Start();
simpleDelegate1 = new MethodInvoker(GetData);
simpleDelegate1.BeginInvoke(null, null);
MQTT_thread = new MethodInvoker(Sendout);
MQTT_thread.BeginInvoke(null,null);
Task.Run(async () => { await ConnectMqttServerAsync(); });
master = ModbusSerialMaster.CreateRtu(comPort);
master.Transport.Retries = 0; //重试次数
......@@ -1714,9 +1724,36 @@ namespace ModbusDemo
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