Socket 中级篇(二)C# 开线程监听:无法将方法组转为ThreadStart

参考https://www.cnblogs.com/yangjinwang/p/4193760.html

一、问题描述

遇到的问题,如下图所示:

无法将方法组转为ThreadStart

解决方法:

二、源码

using KeenRay.Common;
using KeenRay.Common.SocketCommu;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using System.Xml.Linq;

namespace KeenRay.SocketCommu
{
    public class SocketCommuInit : BindableBase
    {
        #region 属性集

        //服务端
        public Socket Client
        {
            get;
            set;
        }

        public int _receiveFlagValue = 0;
        /// <summary>
        ///  接收标志值
        /// </summary>
        public int ReceiveFlagValue
        {
            get { return _receiveFlagValue; }
            set { SetProperty(ref _receiveFlagValue, value); }
        }

        #endregion

        #region 字段集

        //单例:视频监控
        SurveillanceVideo globalSurveillanceVideo = SurveillanceVideo.CreateInstance();

        //服务器
        Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //监听多个Client
        Dictionary<string, Socket> clientList = new Dictionary<string, Socket>();
        //IP
        string strSocketIP = null;
        //端口号
        int point = 40000;
        int iRecBuffeActualByteCount = -1;//缓存区接收到的的字节数
        #endregion

        #region 单例
        private volatile static SocketCommuInit _instance = null;
        private static readonly object lockobj = new object();
        /// <summary>
        /// 创建实例
        /// </summary>
        /// <returns></returns>
        public static SocketCommuInit CreateInstance()
        {
            if (_instance == null)
            {
                lock (lockobj)
                {
                    if (_instance == null)
                    {
                        _instance = new SocketCommuInit();
                    }
                }
            }
            return _instance;
        }
        #endregion

        #region IP配置
        /// <summary>
        /// IP、断开配置
        /// </summary>
        public void IPandPortConfig()
        {
            try
            {
                string strSocketConfigFilePath;
                strSocketConfigFilePath = Environment.CurrentDirectory + "\\Config" + "\\SocketAndBullHeadConfig.xml";
                XDocument SocketDocument = XDocument.Load(strSocketConfigFilePath);
                XElement SocketRoot = SocketDocument.Root;
                //Socket节点
                XElement SocketPoint = SocketRoot.Element("Socket");
                //IP
                XElement SocketIP = SocketPoint.Element("SocketIP");
                strSocketIP = SocketIP.Value;
                //端口
                XElement SocketPort = SocketPoint.Element("SocketPort");
                point = Convert.ToInt32(SocketPort.Value);
            }
            catch(Exception ex)
            {
                LogHelper.Error("SocketClass.cs::IPandPortConfig() IP/端口配置发生错误:--------------" + ex.ToString());
            }
        }
        #endregion

        #region 启动Socket
        /// <summary>
        /// 启动
        /// </summary>
        public void StartFlag()
        {
            //启动Socket
            string strSocketStartFlag = null;
            string strSocketConfigFilePath;
            strSocketConfigFilePath = Environment.CurrentDirectory + "\\Config" + "\\SocketAndBullHeadConfig.xml";
            XDocument SocketDocument = XDocument.Load(strSocketConfigFilePath);
            XElement SocketRoot = SocketDocument.Root;
            //Socket节点
            XElement SocketPoint = SocketRoot.Element("Socket");
            //启动Socket
            XElement SocketStart = SocketPoint.Element("SocketStart");
            strSocketStartFlag = SocketStart.Value;

            //strSocketStartFlag = "1";
            if (strSocketStartFlag == "1")
            {
                IPAddress IP = IPAddress.Parse(strSocketIP);
                IPEndPoint iPEndPoint = new IPEndPoint(IP, point);
                Server.Bind(iPEndPoint);
                Server.Listen(10);
                //开启线程监听
                Thread th = new Thread(ListenFunc);
                th.IsBackground = true;
                th.Start(Server);
            }
        }
        #endregion

        #region 监听
        /// <summary>
        /// 监听
        /// </summary>
        /// <param name="objListen"></param>
        public void ListenFunc(object Server)
        {
            while (true)
            {
                try
                {
                    //客户端
                    Client = ((Socket)Server).Accept();
                    //clientList.Add(Client.RemoteEndPoint.ToString(), Client); //可以监听多个Client
                    Thread threadServer = new Thread(new ParameterizedThreadStart(Receive));
                    threadServer.IsBackground = true;
                    threadServer.Start(Client);
                }
                catch (Exception ex)
                {
                    LogHelper.Error("SocketClass.cs::Listen()监听发错了错误:--------------" + ex.ToString());
                }
            }
        }
        #endregion
    }
}

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值