参考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
}
}