线程同步和线程暂停\恢复
我正在尝试将文件从本地发送到 FTP,因为我在发送文件之前锁定了 TCPClient。文件发送是在另一个线程中进行的。这样主线程就不会受到影响。
当我在解锁之前尝试使用锁定的 TCPClient 时,它会挂起。那么我应该如何继续,以便在发送文件的同时也可以接收文件。(两个函数位于不同的线程中,但锁定同一个 TCPClient 对象)。
我还想暂停第一个线程并执行第二个线程,然后当第二个线程完成时,然后恢复第一个线程。
请帮助,我迷失在线程中。
I am trying to send the files from local to FTP, for that I am locking the TCPClient before sending the file. And that file sending is doing in another thread. So that the main thread doesn't affected.
As when I try to use the locked TCPClient before Unlocking it, it hangs. So how should I proceed, so that at the same time I can send the file also receive the file.(Both function are in different thread but locks the same TCPClient object).
I am also thinking to pause the first thread and perform second one, then when second complete and the after resume the first one.
Please help, I am lost in threads.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该在不同线程中使用相同的
TcpClient
来执行不同的操作 - 它代表单个连接,因此在两个不同线程上发送和接收的数据会相互干扰。 (即使您在一个中“发送”文件并在另一个中“接收”文件,两者都需要发送和接收数据。)我建议您使用两个不同的
TcpClient
实例。You shouldn't use the same
TcpClient
from different threads to do different things - it represents a single connection, so the data sent and received on the two different threads would interfere with each other. (Even if you're "sending" a file in one and "receiving" a file in the other, both will need to send and receive data.)I suggest you use two different
TcpClient
instances.TCP 是一种双向协议。即使您收到文件,您也始终在发送确认。这意味着 TCP 在一个上下文中工作:有一个接收端和一个发送端。
您不应该在一个实例中混合角色。使用 2 个不同的实例。它们可以并行工作。
TCP is a bi-directionnal protocol. Even if you receive a file, you are sending acknowledgment all along. This means TCP works within a context : there is one receiver end, and one sender end.
You should not mix role within an instance. Use 2 different instances. They can work in parallel.