C# Action的使用

项目中要使用tcp通讯,接收事件想通过委托实现,发现Action非常好用

下面是网络通讯部分代码

using BeetleX;
using BeetleX.Clients;
using InfusionBagSmartLabeler;
using InfusionBagSmartLabeler.utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace InfusionBagSmartLaber.Communication
{
    public class TCPCamera
    {
        private TcpClient client;

        private bool TaskIsRunning = true;
        private Thread newThread = null;

        static TCPCamera instance;

        public Action<string> ReceiveTCP;

        public static TCPCamera getInstance()
        {
            if (instance == null)
            {
                instance = new TCPCamera();      
            }
            return instance;
        }

        public void Connect()
        {
            newThread = new Thread(connectNetwork);
            newThread.IsBackground = true;
            newThread.Start();
        }

        private void connectNetwork()
        {
            string strmsg1 = $"0 { util.GetNow()}   connectCamera ";
            Debug.WriteLine(strmsg1);
            App.Log.Info(strmsg1);

            string IP = Config.getInstance().getConfig("CameraIP");
            int Port = Convert.ToInt32(Config.getInstance().getConfig("CameraPort"));

            if (client == null)
            {
                client = SocketFactory.CreateClient<TcpClient>(IP, Port);
                client.TimeOut = 300000;
            }

            while (TaskIsRunning)
            {
                try
                {
                    var read = client.Receive();
                    var str = read.ReadToEnd();
                    ReceiveTCP(str);
 
                }
                catch (Exception e)
                {
                    string msg = $"Camera ErrMSG new : { e.Message}  HResult: {e.HResult}";
                    Debug.WriteLine(msg);
                    App.Log.Error(msg); 
                }
            }
        } 

        public void Send(string cmd)
        {
            // Debug.WriteLine(string.Format("0 {0} 发送命令给相机 SendCamera cmd = {1} ", util.GetNow(), cmd));
            try
            {
                if (client.IsConnected)
                {
                    client.Stream.ToPipeStream().Write(cmd);
                    client.Stream.Flush();
                }
         
            }
            catch (Exception ex)
            {
                string msg = $"SendCamera ERROR {ex.Message}";
                Debug.WriteLine(msg);
                App.Log.Error(msg);
            }
        }

        public void Dispose()
        {
            TaskIsRunning = false;
            client.DisConnect();
            client.Dispose();
            client = null;
        }

    }
}

2.main窗体的代码

        private void Window_Loaded(object sender, RoutedEventArgs e)
        { 

            //连接TCPCamera
            TCPCamera.getInstance().Connect();
            TCPCamera.getInstance().ReceiveTCP += ReceiveTCP;
        }
        

        public void ReceiveTCP(string cmd)
        {
            Debug.WriteLine($" Action ReceiveTCP {cmd}");
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值