은 내가 button1을 가진 폼이 코드왜 서버 tcp를 시작하면 양식이 사라 집니까?
private void button1_Click(object sender, EventArgs e)
{
ServerTCP s = new ServerTCP();
}
그리고 문제는 내가 button1을 클릭 한 후, 양식이 사라지고 있다는 것이다 클래스 serverTCP
public ServerTCP()
{
TcpListener listen = new TcpListener(IPAddress.Any, 1200);
Console.WriteLine("Waiting");
listen.Start();
while (true)
{
TcpClient client = listen.AcceptTcpClient();
Console.WriteLine("Client connected");
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
int data = stream.Read(buffer, 0, client.ReceiveBufferSize);
string message = Encoding.Unicode.GetString(buffer, 0, data);
int idgiorno = Int32.Parse(message);
Console.WriteLine("idgiorno: " + idgiorno);
client.Close();
}
}
있습니다. 시작 막대에서 여전히 실행 중임을 볼 수 있지만 아이콘을 클릭하더라도 표시되지 않습니다. 이것은 양식이 초점을 잃는 것과 같습니다.
프로그램의 주 스레드에서 연속 루프가 실행되어 충돌을 일으켜 사용하지 못하는 것처럼 보입니다. – ThePerplexedOne
프로그램은 클라이언트로부터 입력을 기다리는 서버입니다. 하지만 응용 프로그램을 닫으려면 다른 단추가 있어야하기 때문에 양식에 초점이 필요합니다 – Alessandro
그런 다음 주 스레드와 별도의 스레드에서 코드를 실행하십시오. 스레딩에 대한 자세한 내용은 [https://msdn.microsoft.com/en-us/library/aa645740(v=vs.71) .aspx)를 참조하십시오. – ThePerplexedOne