我确定这是一个非常简单的问题,但我在这里遇到了一些问题。我将在最后粘贴 WPC 表单 + 另一个类的整个代码,对不起,它很长,但只需搜索 ISSUE 这个词,您就会立即解决问题
我想要达到的目标
--我的 WPF c# 主窗口称为 MainWindow 并有一个文本框 txtLog
--我的一个名为clientHandler的类需要更改txtLog中的值
--目前我将 MainWindow 类的引用从我的 MainWindow 窗口传递给 MainWindowHandle 方法。
长代码 - 请在我的 ClientHandler 类中搜索问题行的单词 ISSUE
主窗口
public partial class ServerWindow : Window
{
public static int NoOfRunningClients;
public static ClientHandler[] RunningClientsPool = new ClientHandler[50];
public static ClientHandler test;
public static ManualResetEvent allDone = new ManualResetEvent(false);
public ServerWindow()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
NoOfRunningClients = 0;
//RunningClientsPool[NoOfRunningClients];
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
RunningClientsPool[NoOfRunningClients] = new ClientHandler();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(RunningClientsPool[NoOfRunningClients].AcceptCallback),
listener);
RunningClientsPool[NoOfRunningClients].MainWindowHandle = this;
// Wait until a connection is made before continuing.
allDone.WaitOne();
NoOfRunningClients++;
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
}
}
客户端处理程序
public class ClientHandler
{
private Socket clientSocket;
public static ManualResetEvent allDone = new ManualResetEvent(false);
public ClientHandler(Socket newSocket)
{
this.clientSocket = newSocket;
}
public ClientHandler()
{
}
public String MyName { get; set; }
public Window MainWindowHandle { get; set; }
public void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
public void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
// All the data has been read from the
// client. Display it on the console.
// ISSUE: I want to use something like this
//but have the static reference problem
//MainWindowHandle.txtLog.Text = "It WORKS!!!!!";
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
content.Length, content);
// Echo the data back to the client.
Send(handler, content);
}
else
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
private void Send(Socket handler, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
private void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
非常感谢您抽出宝贵的时间,重复一遍,我希望能够访问 MainWindow 中的 txtLog 和其他 GUI 元素,而不会遇到恒定的静态成员/非静态成员问题。