有什么办法可以在C#方法中拥有不可变参数吗?下面我给一些messenger应用程序提供了一些代码,这些代码给了我一些问题。这个想法是有一台服务器可以处理多个客户端。该代码通过创建TcpListener
来监视传入连接,获取底层TcpClient
(通过NetworkStream
作为字符串发送)的IP地址,启动一个处理来自该客户端的消息的新线程,然后监听更多连接。主要问题是client
对象被传递到新的Thread
后,主代码将循环并在等待另一个连接时将值client
设置为null
。C# - 不可变的方法参数
错误类型:System.InvalidOperationException
错误消息:The operation is not allowed on non-connected sockets.
这是什么对我说的是,TcpClient
的的handleMessages
线程内在价值正受到该线程的.Start()
方法启动后会发生什么client
。
这里是我的代码:
private void watchForConnections()
{
TcpListener listener = new TcpListener(IPAddress.Any, this.Port); //Listener that listens on the specified port for incoming clients
listener.Start();
TcpClient client = new TcpClient();
do
{
client = listener.AcceptTcpClient(); //Wait for connection request
StreamReader reader = new StreamReader(client.GetStream());
string clientIP = reader.ReadLine(); //Use the StreamReader to read the IP address of the client
RelayMessage("SERVER_NEW_" + clientIP + "_" + DateTime.Now.ToString("HH:mm:ss")); //Tell all machines who just connected
Thread messageWatcher = new Thread(() => handleMessages(client));
messageWatcher.Start(); //Start new thread to listen for messages from that specific client
} while (AllowingConnections == true);
listener.Stop();
}
private void handleMessages(TcpClient _client)
{
using (StreamReader readMsg = new StreamReader(_client.GetStream())) //I get the error here
using (StreamWriter writeMsg = new StreamWriter(_client.GetStream())) //And here
{
//Handle messages from client here
}
}
我的问题:有什么办法可以在handleMessages
参数将不被会发生什么方法之外有什么影响?迄今为止,我的研究并没有涉及到这个问题,或者任何类似的问题。我需要的是有点像ref
参数的反面。也许我只是没有在寻找正确的东西。我不知道我是否解释正确。
用绳子又如:
string data = "hello";
Thread doStuff = new Thread(() => DoStuff(data)); //Value of data is equal to: "hello"
doStuff.Start();
data = "goodbye"; //I want the value of the string in the doStuff thread to still be: "hello", not "goodbye" (Don't reflect changes made to string after thread is started/method is called)
如果有什么不清楚,请让我知道!我可能和你一样困惑!
UPDATE /解决方案: 的人谁需要它的未来,这是问题是如何解决的,按照维鲁的回答是:
Thread messageWatcher = new Thread(() => handleMessages(client.Clone());
你为什么不这样称呼它复制'DoStuff(string.Copy(数据))' ,从而创建一个新的'数据'的无关副本? – dotNET
创建副本。 'var copy = data'? –
这已经在这里讨论:http://stackoverflow.com/questions/2339074/can-parameters-be-constant –