静态类、另一个线程和事件

发布于 2024-11-24 02:40:59 字数 622 浏览 3 评论 0原文

我正在尝试通过制作自己的小型网络库来学习网络。目前我有一个新客户端列表,这样的代码可以使用它们。如果列表中有任何内容,NewClientConnected() 方法将返回 true 并删除第一个元素。

ConnectedClient newClient; // ConnectedClient is my class storing it's socket and stream and all that.
if(NewClientConnected(out newClient)
{
...handling new client, notification, etc...
}

检查新数据包(流的前缀切片)也是如此。我试图将其提升到一个新的水平,并尝试在发生此类事情时引发事件并从新的联系开始。问题是,该事件是从另一个线程引发的,导致在 form1.cs 中处理该事件时出现异常。

如何确保事件由静态非控制类的主线程处理?我应该继续做我所做的事情吗? (上面的片段)我听到了消费者-生产者关系的想法,如果我没记错的话,它仍然需要一个计时器(使用它自己的线程)或另一个线程。

我尝试用谷歌搜索并查看这里,但每个人似乎都有不同的问题。它位于一个库项目中,而另一个项目(我的服务器和客户端测试)正在引用它。

提前致谢。

I am trying to learn networking by making my own little network library. Currently I have a list of new clients that a code like this can use. The NewClientConnected() method returns true if there's anything in the list and removes the first element.

ConnectedClient newClient; // ConnectedClient is my class storing it's socket and stream and all that.
if(NewClientConnected(out newClient)
{
...handling new client, notification, etc...
}

Same goes for checking for new packets (prefixed slice of stream). I sought to take it to the next level and attempt to raise events when things like this happen and started with new connections. Problem is, the event is raised from another thread, causing an exception as the event is handled in the form1.cs.

How can I make sure the event is handled by the main thread from a static non-control class? Should I just keep doing what I do? (snippet above) I hear the idea of a consumer-produces relationship, it would still require a timer (uses its own thread) or another thread if I recall correctly.

I have tried to google it and look on here, but everyone seems to have a different problems. It is in a library project while another projects (my server and client test) is referring to it.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知你几分 2024-12-01 02:41:00

看一下 Control.Invoke 和 Control.BeginInvoke 方法。

Control.Invoke - “在拥有控件的基础窗口句柄的线程上执行指定的委托。”

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
http://msdn.microsoft.com/en -us/library/system.windows.forms.control.begininvoke.aspx

这应该可以修复您的“另一个线程异常”

Take a look at Control.Invoke and Control.BeginInvoke methods.

Control.Invoke - "Executes the specified delegate on the thread that owns the control's underlying window handle."

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx

This should fix your "another thread exception"

神魇的王 2024-12-01 02:40:59

如果您想要执行的操作与用户界面交互,那么您必须在 UI 线程上调用它们。

因此,不要直接处理事件,而是尝试这样做:

// Somewhere in the form1 code:
Server.newConnectionEvent += ConnectionEVentHandler(myMethod)

public void myMethod()
{
  //Event method called from another thread
  //can only do things here that do affect the UI!
  this.Dispatcher.Invoke(CalledOnUIThread);
}

public void CalledOnUIThread()
{
  //Handle event on UI thread here
  //Can do things here that affect the UI
}

此代码必须从 form1 类运行,因为它使用“this”。因此,您所做的所有更改(大概是您没有显示事件处理程序代码)就是将要运行的实际代码放在单独的方法中,然后从事件处理程序调用该单独的方法。

请参阅 http://msdn.microsoft.com/en -us/library/system.windows.threading.dispatcher.aspx 有关调度程序类的 API 文档。

请参阅UI 线程的定义是什么? .NET 应用程序中是否只有一个 UI 线程? 对 UI 线程进行简单说明。

If the operations you want to perform interact with the user interface then you have to invoke them on the UI thread.

So instead of handling the event directly, try this:

// Somewhere in the form1 code:
Server.newConnectionEvent += ConnectionEVentHandler(myMethod)

public void myMethod()
{
  //Event method called from another thread
  //can only do things here that do affect the UI!
  this.Dispatcher.Invoke(CalledOnUIThread);
}

public void CalledOnUIThread()
{
  //Handle event on UI thread here
  //Can do things here that affect the UI
}

This code has to run from the form1 class, since it uses 'this'. So all you change (presumably, you didn't show your event handler code) is put the actual code that you want to run in a seperate method and then invoke that seperate method from the event handler.

See http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx for the API documentation on the dispatcher class.

See What is the definition of a UI thread? Is there only one UI thread in a .NET application? for a simple explanation of the UI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文