GUI 更新时 C Sharp GUI/Socket 崩溃
每当handleResponse调用委托函数“func”时,我的GUI都会毫无例外地崩溃。委托函数将文本附加到 GUI 上的 RichTextBox。
如果我在“connect”中调用 this.func ,它就可以正常工作。
private void handleResponse(IAsyncResult result)
{
try
{
this.func.Invoke("test");
}
catch (Exception e)
{
throw e;
}
}
public void connect(string ip, int port, delegateFunction func) {
try
{
connection.Connect(ip, port);
socket = connection.Client;
this.func = func;
socket.BeginReceive(incomingBuffer, 0, incomingBuffer.Length, SocketFlags.None, handleResponse, null);
}
catch (Exception e)
{
throw e;
}
}
Whenever handleResponse calls the delegate function "func" my GUI crashes with no exception. The delegate function appends text to a RichTextBox on the GUI.
If I call this.func in "connect" it works just fine.
private void handleResponse(IAsyncResult result)
{
try
{
this.func.Invoke("test");
}
catch (Exception e)
{
throw e;
}
}
public void connect(string ip, int port, delegateFunction func) {
try
{
connection.Connect(ip, port);
socket = connection.Client;
this.func = func;
socket.BeginReceive(incomingBuffer, 0, incomingBuffer.Length, SocketFlags.None, handleResponse, null);
}
catch (Exception e)
{
throw e;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是线程问题。您的 GUI 可能必须在特定的 GUI 线程上更新。如何切换到该线程取决于您使用的 GUI 框架。
Could be a threading issue. Your GUI probably has to be updated on a specific GUI thread. How switching to that thread is achieved is specific to the GUI framework you use.