C# 多线程:发布线程和订阅线程之间的混淆

C# 多线程:发布线程和订阅线程之间的混淆,c#,mqtt,opc,C#,Mqtt,Opc,我已经创建了一个Windows窗体应用程序,它能够将OPC标记发布到MQTT代理中,现在我正尝试做相反的事情,将MQTT标记写入OPC服务器。当我启动代理(发布者)和传输(订阅者)时,这两个线程执行相同的发布工作,我不知道问题出在哪里。 启动方法如下: public byte Start() { try { byte connectResult; if (IsLWT == false)

我已经创建了一个Windows窗体应用程序,它能够将OPC标记发布到MQTT代理中,现在我正尝试做相反的事情,将MQTT标记写入OPC服务器。当我启动代理(发布者)和传输(订阅者)时,这两个线程执行相同的发布工作,我不知道问题出在哪里。 启动方法如下:

public byte Start()
    {
        try
        {
            byte connectResult;
            if (IsLWT == false)
            {


                connectResult = this._MqttClient.Connect(ClientID, Username, Password, IsCleanSession, KeepAlivePeriode);
            }
            else
            {             
                   
                connectResult = this._MqttClient.Connect(ClientID, Username, Password, willRetain, willQos, true, willTopic, willMessage, IsCleanSession, KeepAlivePeriode);

            }

            // 0 means that the connection suceeded
            if (connectResult == 0)
            {
                this.Rate = GetRateFromOPCGroups();
                this._publisherThread = new Thread(() => Publish());
                this._publisherThread.IsBackground = true;
                this._publisherThread.Start();
                IsStarted = true;
            }

            if (connectResult == 0)
            {
              //this.Rate = GetRateFromOPCGroups();
                this._SubscriberThread = new Thread(() => Subscribe(topics));
                this._SubscriberThread.IsBackground = true;
                this._SubscriberThread.Start();
                IsStarted = true;
            }

            return connectResult;

        }
        catch (IntegrationObjects.Networking.M2Mqtt.Exceptions.MqttClientException ex)
        {
            MQTTServiceLogger.TraceLog(MessageType.Error, MQTTServiceMessages.startAgentFailed(this.Name,ex.Message));
            return 11;
        }
        
        catch (IntegrationObjects.Networking.M2Mqtt.Exceptions.MqttCommunicationException ex)
        {
            MQTTServiceLogger.TraceLog(MessageType.Error, MQTTServiceMessages.startAgentFailed(this.Name, ex.Message));
            return 11;
        }
        catch (Exception ex)
        {
            MQTTServiceLogger.TraceLog(MessageType.Error, MQTTServiceMessages.startAgentFailed(this.Name, ex.Message));
            return 11;
        }
    }
1.这是发布代码: 私有无效发布() {


第一:你是故意让两个都只有一个
IsStarted
吗?第二:请添加
Publish()
Subscribe(主题)
实现。“IsLWT”代表什么?
while(true){if(IsStarted)
-这是个坏主意,但可能不是你的问题。你如何确认它们“做同样的工作”?当我单击订阅服务器(传输)的开始按钮并通过另一个MQTT客户端进行检查时,我看到新消息在MQTT代理中发布。当我单击发布服务器(代理)的开始按钮时也会执行相同的操作。好的,我将尝试以这种方式解决问题。非常感谢。
        while (true)
        {
            if (IsStarted)
            {
                try
                {
                    if (_MqttClient.IsConnected)
                    {
                        isConnected = true;
                        if (this.OPCItems.Count != 0)
                        {

                            JsonMQTTMessage JsonMessage = new JsonMQTTMessage();
                            JsonMessage.Timestamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff");
                            JsonMessage.ListofValues = new List<UpdatedOPCItem>();

                          
                        
                            lock (lockOPCItems)
                            {
                                foreach (OPCItem Item in OPCItems.ToList())
                                {
                                    if (Item != null)
                                    {
                                        UpdatedOPCItem upItem = new UpdatedOPCItem();
                                        upItem.ID = Item.ItemID;
                                        upItem.value = Item.ItemCurrentValue;
                                        upItem.quality = Item.ItemQuality;
                                        upItem.timestamp = Item.ItemTimeStamp.ToString("yyyy/MM/dd HH:mm:ss.fff");
                                        upItem.DataType = Item.ItemDataType;
                                        JsonMessage.ListofValues.Add(upItem);
                                    }

                                }
                            }
                           
                            var messageTopublish = Newtonsoft.Json.JsonConvert.SerializeObject(JsonMessage);
                            ushort res = _MqttClient.Publish(Topic, Encoding.UTF8.GetBytes(messageTopublish), Qos, Retain);
                            ResetValues();
                            Thread.Sleep(Rate);
        while (true)
        {
            if (IsStarted)
            {
                try
                {
                    if (_MqttClient.IsConnected)
                    {
                        isConnected = true;
                        foreach (string topic in topics)
                        {
                            ushort msggId = _MqttClient.Subscribe(new string[] { $"{ topic }" },

                           new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
                        }
                        Thread.Sleep(Rate);

                    }
                    else