简体   繁体   English

在线程中使用块

[英]Using Block with Threading

I'm reasonably experienced with C#, however I've never come across this problem before, and I was wondering if any more experienced C# Developers know what to do in this situation. 我对C#有一定的经验,但是我之前从未遇到过这个问题,我想知道是否有更多经验丰富的C#开发人员知道在这种情况下该怎么做。 Here's the code for the method in question: (the problem is explained after the block of code) 这是所讨论方法的代码:(该问题在代码块之后进行了解释)

public void ConnectToRemoteServer() 
{
    Console.WriteLine("Attempting to connect to " + config.GetString(ConfigParams.MasterServerIp) + ":" + config.GetString(ConfigParams.MasterServerPort));
    TcpClient client = new TcpClient();
    IPEndPoint address = new IPEndPoint(IPAddress.Parse(config.GetString(ConfigParams.MasterServerIp)), config.GetInt(ConfigParams.MasterServerPort));
    Console.WriteLine("Connecting...");
    //Begin asynchronous sever communication
    if (this.autoTask == null)
    {
        communicator = new CommunicationListener(client, config, address);
    }
    else
    {
        communicator = new CommunicationListener(client, config, address, this.autoTask);
    }
    Thread communicationThread = new Thread(new ThreadStart(communicator.Start));
    communicationThread.Start();
}

The part that I'm wondering about is if I should be using a using statement in this block of code. 我想知道的部分是我是否应该在此代码块中使用using语句。 I know that TcpClient implements the interface IDisposable , and as such should be encapsulated in a using statement, however, in this case, a new thread is started that uses the TcpClient , and as such the end of the using block will be reached before the TcpClient is done being used. 我知道TcpClient实现了接口IDisposable ,因此应该封装在using语句中,但是,在这种情况下,将启动一个使用TcpClient的新线程,因此using块的结尾将在TcpClient已完成使用。 So should I be using the using statement here? 那我应该在这里使用using语句吗?

Don't use using here as it will make your program not work due to early disposal. 不要在这里使用using,因为它会由于早期处理而使您的程序无法正常工作。 Just make sure you hand off the TcpClient correctly to the new thread and make sure the thread disposes of it eventually. 只需确保将TcpClient正确传递给新线程,并确保该线程最终对其进行处理即可。

I think it would be preferable to create the TcpClient in the child thread so you can use using there. 我认为这将是最好创建TcpClient在子线程,这样就可以使用using那里。

I think you are right to avoid the using block in this case since using results in an implicit close() at the end of the using block. 我认为您在这种情况下应该避免使用using块,因为在using块的末尾使用隐式close()的结果。 I think this is a fairly common source of aggravation because the usual advice on when to use using blocks is "whenever the object impliments IDisposable". 我认为这是一个比较常见的恶化原因,因为有关何时使用块的通常建议是“只要对象隐含IDisposable”。

Here is a definitive article on when NOT to use using, regardless of the implementation of IDisposable. 这是有关何时不使用use的权威文章,无论IDisposable的实现如何。 http://msdn.microsoft.com/en-us/library/aa355056.aspx . http://msdn.microsoft.com/zh-CN/library/aa355056.aspx

The general rule of thumb is that if its IDisposable then you should dispose of that object. 一般的经验法则是,如果其IDisposposable,则应处置该对象。

A using block gives you a nice easy way to do that, but since your TCPClient will persist outside of this method, then it cant be used in this case. using块为您提供了一种很好的简便方法,但是由于TCPClient将在该方法之外持久存在,因此在这种情况下无法使用它。

If you really wanted to write nice code then should; 如果您真的想编写漂亮的代码,那么应该; declare your TCPClient within your class, have your class implement IDisposable, dispose of your TCPClient within your new Dispose method. 在您的类中声明您的TCPClient,让您的类实现IDisposable,在新的Dispose方法中处置您的TCPClient。 (and maybe do something about ending your thread). (也许可以做一些有关结束线程的事情)。

That way you can wrap your class within using block. 这样,您可以将类包装在using块中。

I think there is a different kind of problem. 我认为这是另一种问题。 You have to implement IDisposable in CommunicationListener and instantiate the TcpClient also there and dispose the TcpClient in the CommunicationListener.Dispose implementation. 您必须在CommunicationListener中实现IDisposable,并在那里也实例化TcpClient,并在CommunicationListener.Dispose实现中部署TcpClient。

When would be better to dispose CommunicationListener? 什么时候最好配置CommunicationListener? It depends. 这取决于。

I would make the class in which this method exists and CommunicationListener both disposable. 我将使该方法所在的类和CommunicationListener都成为一次性的。 Then I would implement a way to cancel the communication listener's thread by setting a flag on it so that when your own class is disposed, it doesn't leave the other thread running. 然后,我将通过在其上设置一个标记来实现一种取消通信侦听器线程的方法,以便在处理您自己的类时,它不会使其他线程运行。 Then in the Dispose of the parent class, set the flag so CommunicationListener can stop, dispose of the CommunicationListener, which should then internally dipose of the TcpClient. 然后,在“处置父类”中,设置标志,以便CommunicationListener可以停止,处置CommunicationListener,然后再在内部处置TcpClient。

I hope that makes sense. 我希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM