在我的程序中,此刻我有2个类,最终它将随着更多的类而增长,第一个类是Windows窗体的主类,其中所有按钮和文本等都已存在。单击关闭连接的按钮,单击此按钮后,循环将从第二类结束。我遇到的问题是如何完成可以从主类按钮关闭该循环的操作。
这是主要的课程代码
using System;
using System.Windows.Forms;
namespace BarcodeReceivingApp
{
//TelnetConnection stopConnection = new TelnetConnection();
public partial class BarcodeReceivingForm : Form
{
//GLOBAL VARIABLES
private const string Hostname = "myip";
private const int Port = 23;
public BarcodeReceivingForm()
{
InitializeComponent();
//FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
private void btn_ConnectT_Click(object sender, EventArgs e)
{
var readData = new TelnetConnection(Hostname, Port);
readData.ServerSocket(Hostname, Port, this);
}
private void btn_StopConnection_Click(object sender, EventArgs e)
{
var connection = new TelnetConnection(Hostname, Port);
connection.CloseConnection();
}
private void btn_RemoveItemFromListAt_Click(object sender, EventArgs e)
{
for (var i = lst_BarcodeScan.SelectedIndices.Count - 1; i >= 0; i--)
{
lst_BarcodeScan.Items.RemoveAt(lst_BarcodeScan.SelectedIndices[i]);
}
}
private void BarcodeReceivingForm_Load(object sender, EventArgs e)
{
lst_BarcodeScan.SelectionMode = SelectionMode.MultiSimple;
}
private void btn_ApplicationSettings_Click(object sender, EventArgs e)
{
var bcSettingsForm = new BarcodeReceivingSettingsForm();
bcSettingsForm.Show();
}
}
}
大多数逻辑是第二类代码 在ReadWrite()方法中,我需要在其中放置if例如button.click从stopconnection然后断开循环并关闭端口23连接,这是我在while循环后调用的close连接方法中完成的部分。现在写我有命令变量=退出然后打破循环,但我不想成为为什么我想要按钮做到这一点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.Remoting.Messaging;
using System.Security.Authentication.ExtendedProtection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BarcodeReceivingApp
{
public class TelnetConnection
{
private Thread _readWriteThread;
private TcpClient _client;
private NetworkStream _networkStream;
private string _hostname;
private int _port;
private BarcodeReceivingForm _form;
public TelnetConnection(string hostname, int port)
{
this._hostname = hostname;
this._port = port;
}
public TelnetConnection()
{
}
public void ServerSocket(string ip, int port, BarcodeReceivingForm f)
{
this._form = f;
try
{
_client = new TcpClient(ip, port);
}
catch (SocketException)
{
MessageBox.Show(@"Failed to connect to server");
return;
}
_networkStream = _client.GetStream();
_readWriteThread = new Thread(ReadWrite);
//_readWriteThread = new Thread(() => ReadWrite(f));
_readWriteThread.Start();
}
public void ReadWrite()
{
while (true)
{
var command = "";
if (command == "Exit")
break;
var received = Read();
if (_form.lst_BarcodeScan.InvokeRequired)
{
_form.lst_BarcodeScan.Invoke(new MethodInvoker(delegate { _form.lst_BarcodeScan.Items.Add(received + Environment.NewLine); }));
}
}
CloseConnection();
}
public string BarcodeRead()
{
var reading = Read();
return reading;
}
public string Read()
{
var data = new byte[1024];
var received = "";
var size = _networkStream.Read(data, 0, data.Length);
received = Encoding.ASCII.GetString(data, 0, size);
return received;
}
public void CloseConnection()
{
Console.WriteLine(@"Closed Connection");
_networkStream.Close();
_client.Close();
}
}
}
此外,我一直在Google上搜索,并且还在堆栈上溢出类似于问题的内容,但找不到任何可以解决我问题的东西。
答案 0 :(得分:1)
您应该将循环更改为在布尔条件改变时自动停止。您可以在这里做些零钱。您将需要创建一个新方法来调用结束,并稍微更改ReadWrite
方法
private bool isExiting = false;
public void Exit()
{
isExiting = true;
}
public void ReadWrite()
{
do
{
var received = Read();
if (_form.lst_BarcodeScan.InvokeRequired)
{
_form.lst_BarcodeScan.Invoke(new MethodInvoker(delegate { _form.lst_BarcodeScan.Items.Add(received + Environment.NewLine); }));
}
} while (!isExiting)
CloseConnection();
}
现在调用connection.Exit();
将关闭所有内容,但是您仍然遇到问题。您没有将类的实例保留在连接形式中。在表单中,您需要更改连接和关闭以使用同一对象。这是这两种方法的细微变化
private TelnetConnection connection;
private void btn_ConnectT_Click(object sender, EventArgs e)
{
connection = new TelnetConnection(Hostname, Port);
connection.ServerSocket(Hostname, Port, this);
}
private void btn_StopConnection_Click(object sender, EventArgs e)
{
connection.Exit();
}
现在他们两个都使用同一个对象,因此它应该可以正常工作。
答案 1 :(得分:0)
NetworkStream.Read()在套接字关闭时返回0,因此您可以使用它来打破循环。因此,当读取返回0时,返回null。
<lastmod>2019-01-01</lastmod>
并在您的ReadWrite方法中
public string Read()
{
var data = new byte[1024];
var received = "";
var size = _networkStream.Read(data, 0, data.Length);
if(size == 0)
return null;
received = Encoding.ASCII.GetString(data, 0, size);
return received;
}