我独立测试这些代码工作正常。一旦我将其写入用户控件并将其添加到应用程序参考中,并在WPF中使用它,Visual Studio就会转向" Not Responding"。这让我很疯狂。
下面是用户控制代码:
namespace ServerUserContorl
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public Socket server;
public Socket Command_Listening_Socket; //used to listen for connections for exchanging commands between server and mobile
public Socket Interaction_Listening_Socket1;// designed for receiving information from mobile
public Socket Interaction_Listening_Socket2;// designed for sending information to mobile
public Socket Interaction_Receiving_Socket;// receiving
public Socket Interaction_Sending_Socket;// sending
public UserControl1()
{
IPAddress local = IPAddress.Parse("134.129.125.126");
IPEndPoint iepCommand = new IPEndPoint(local, 8080);// IP end point for command listening socket
IPEndPoint iepReceiving = new IPEndPoint(local, 8090);// IP end point for receiving socket
IPEndPoint iepSending = new IPEndPoint(local, 9090);// Ip end point for sending socket
//part for command listening socket connection
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(iepCommand);
server.Listen(20);
// part for receiving Socket
Interaction_Listening_Socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Interaction_Listening_Socket1.Bind(iepReceiving);
Interaction_Listening_Socket1.Listen(20);
// part for sending Socket
Interaction_Listening_Socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Interaction_Listening_Socket2.Bind(iepSending);
Interaction_Listening_Socket2.Listen(20);
while (true)
{
// Here is three blocks for accepting the new client,
// once client connect to the command listening socket,
// pass the command listening socket to the threadService class
// and start a thread for receiving the userID
Command_Listening_Socket = server.Accept();
Interaction_Receiving_Socket = Interaction_Listening_Socket1.Accept();
Interaction_Sending_Socket = Interaction_Listening_Socket2.Accept();
threadService service = new threadService(Command_Listening_Socket,Interaction_Receiving_Socket,Interaction_Sending_Socket);
Thread userCommand = new Thread(service.userCommand);
userCommand.Start();
Debug.WriteLine("thread starts");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Threading;
namespace ServerUserContorl
{
class CommunicationThread
{
public Socket Interaction_Receiving_Socket, Interaction_Sending_Socket;
public CommunicationThread(Socket receiving, Socket sending)
{
Interaction_Receiving_Socket = receiving;
Interaction_Sending_Socket = sending;
}
// connect with iep2 9090, clientCom1,clientCommunication1
public void sendThread()
{
Debug.WriteLine("In the SendThread method");
byte[] bytes = new byte[1024];
string greeting = "hello, this is the message from the server.";
bytes = System.Text.Encoding.ASCII.GetBytes(greeting);
Debug.WriteLine("the byte is " + bytes);
int i = 0;
while (i < 10)
{
Interaction_Sending_Socket.Send(bytes);
Thread.Sleep(1000);
i++;
}
Debug.WriteLine("send is successful");
// clientCommunication1.Close();
}
//8080
public void receiveThread()
{
int i = 0;
string data = null;
Debug.WriteLine("In the receive method");
byte[] bytes = new byte[1024];
while ((i = Interaction_Receiving_Socket.Receive(bytes)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Debug.WriteLine("receive from the client " + data);
}
//clientCommunication.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading;
namespace ServerUserContorl
{
class threadService
{
public Socket client;
//public Socket clientCommunication;
public string status;
public Socket Interaction_Receiving_Socket, Interaction_Sending_Socket;
public static int connections = 0;
int i = 0;
public threadService(Socket client,Socket receiving, Socket sending)
{
this.client = client;
Interaction_Receiving_Socket = receiving;
Interaction_Sending_Socket = sending;
}
public void userCommand()
{
string data = null;
byte[] bytes = new byte[1024];
if (client != null)
{
connections++;
}
Debug.WriteLine("new client connects, {0} connections", connections);
while ((i = client.Receive(bytes)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
status = data;
Debug.WriteLine("receive from client: " + status);
if (status == "1")
{
CommunicationThread communication = new CommunicationThread(Interaction_Receiving_Socket,Interaction_Sending_Socket);
Thread newSendThread = new Thread(communication.sendThread);
newSendThread.Start();
Debug.WriteLine("start the communication thread--status 1");
// CommunicationThread communication = new CommunicationThread();
Thread newReceiveThread = new Thread(communication.receiveThread);
newReceiveThread.Start();
Debug.WriteLine("start the communication thread--status 2");
}
}
}
}
}
在世界粮食计划署,我只添加它:
xmlns:myControl="clr-namespace:ServerUserContorl;assembly=ServerUserContorl"
<myControl:UserControl1>
答案 0 :(得分:2)
Visual Studio的Designer必须在添加控件时调用控件的构造函数,这意味着Designer将进入构造函数中的while(true)
循环。
首先:不要在控件的构造函数中放置无限循环。 EVER。 如果您需要无休止地进行投票,那么应该在程序的主要部分中处理,或者更好的是,在单独的线程中处理。在构造函数完成之前,控件不能被其他对象引用,因此需要让构造函数完成。
第二:如果你的构造函数中有代码,你希望 Designer 跳过,你可以这样做:
using System.ComponentModel;
public MyControl()
{
// code that Designer should run
if (LicenseManager.UsageMode == LicenseUsageMode.DesignTime)
return;
// code that Designer should not run
}