winforms 如何在单击按钮后在C#中启用部分代码,特别是另一个事件处理程序

ipakzgxi  于 2022-11-16  发布在  C#
关注(0)|答案(1)|浏览(186)

我有一个场景,我通过TCP/IP协议连接到服务器。这需要在我单击Windows窗体上的“连接到TCP/IP服务器”按钮后完成。单击此按钮后,将显示连接的状态,无论是成功还是失败。这部分我管理得相当容易。我需要的是,当连接成功时,只有这样程序/代码才需要开始监听来自服务器的传入消息,然后继续。我测试了是否可以在没有按钮的情况下获得服务器消息(即,当表单初始化时,它会自动连接到服务器),而且它工作得很好。您将在下面的代码中看到,我在同一台PC上使用了第三方软件的引用命令,该软件处理来自服务器的传入消息。扩展软件的功能,这是允许的软件在他们的文档。我确实会写一些变量到这个第三方软件。这我也很好这样做。因此,我唯一的问题是,只有当连接按钮被按下,连接成功时,才能监听来自服务器的传入消息。
任何帮助都将不胜感激。
下面是我目前为止的代码,但它不起作用:`

namespace Form1
{
    public partial class Main_Form: Form
    {
        private CogJob Job;
        private CogJobManager Job_manager;
        private CogJobIndependent Job_independent;

        String Received_string;
        bool State_of_connection = false;
        ICogIOTCPIP TCP_IP_server_device;
        
        public Main_HMI()
        {
            InitializeComponent();

            //Load application
            Job_manager = (CogJobManager)(CogSerializer.LoadObjectFromFile(@"Project"));

            // Initialize job variables
            Job = Job_manager.Job("Application");
            Job_independent = Job.OwnedIndependent;

            // Flush all job queues
            Job.ImageQueueFlush();
            Job_manager.UserQueueFlush();
            Job_manager.FailureQueueFlush();
            Job_independent.RealTimeQueueFlush();

            if (State_of_connection == true)
            {
                TCP_IP_server_device.MessageReceived += new     CogIOStreamMessageEventHandler(TCP_IP_server_device_MessageReceived);
            }
            
        }
        
        // Decode the message received from the TCP/IP server device
        private void TCP_IP_server_device_MessageReceived(object sender, CogIOStreamMessageEventArgs eventArgs)
        {
            Received_string = eventArgs.DecodedMessage.Substring(2, 9);
        }

        // Savely shutdown application job manager when the form is closed;
        private void Main_HMI_FormClosing(object sender, FormClosingEventArgs e)
        {
            Job_manager.Shutdown();
        }

        private void Connect_to_TCP_IP_server_button_Click(object sender, EventArgs e)
        {
            Job_manager.IOEnable = true;
            TCP_IP_server_device = Job_manager.StreamInput("PLC", 2000, true);

            if (TCP_IP_server_device == null)
            {
                MessageBox.Show("Connection failed", "TCP/IP server device connection status");
            }

            else
            {
                MessageBox.Show("Connection successful", "TCP/IP server device connection status");
                State_of_connection = true;
            }
        }
    }
}

`

svmlkihl

svmlkihl1#

解决方法相当简单:

namespace Form1;

public partial class Main_Form: Form
{
    private CogJob Job;
    private CogJobManager Job_manager;
    private CogJobIndependent Job_independent;

    String Received_string;
    ICogIOTCPIP TCP_IP_server_device;
    
    public Main_HMI()
    {
        InitializeComponent();

        //Load application
        Job_manager = (CogJobManager)(CogSerializer.LoadObjectFromFile(@"Project"));

        // Initialize job variables
        Job = Job_manager.Job("Application");
        Job_independent = Job.OwnedIndependent;

        // Flush all job queues
        Job.ImageQueueFlush();
        Job_manager.UserQueueFlush();
        Job_manager.FailureQueueFlush();
        Job_independent.RealTimeQueueFlush();
    }
    
    // Decode the message received from the TCP/IP server device
    private void TCP_IP_server_device_MessageReceived(object sender, CogIOStreamMessageEventArgs eventArgs)
    {
        Received_string = eventArgs.DecodedMessage.Substring(2, 9);
    }

    // Savely shutdown application job manager when the form is closed;
    private void Main_HMI_FormClosing(object sender, FormClosingEventArgs e)
    {
        Job_manager.Shutdown();
    }

    private void Connect_to_TCP_IP_server_button_Click(object sender, EventArgs e)
    {
        Job_manager.IOEnable = true;
        TCP_IP_server_device = Job_manager.StreamInput("PLC", 2000, true);

        if (TCP_IP_server_device == null)
        {
            MessageBox.Show("Connection failed", "TCP/IP server device connection status");
        }
        else
        {
            MessageBox.Show("Connection successful", "TCP/IP server device connection status");
            TCP_IP_server_device.MessageReceived += new     CogIOStreamMessageEventHandler(TCP_IP_server_device_MessageReceived);
        }
    }
}

仅在成功连接时启用事件处理。

相关问题