线程和事件问题 [英] Thread and Event problem

查看:73
本文介绍了线程和事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的读者,

似乎在穿线方面有一点问题.

我设置了一个套接字连接,其中有一个新的AsyncCallback(OnDataReceived);

从现在开始我的主线程不可用:
我们就称它为System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

调用OnDataReceived时,必须创建一个新的类实例,
但是此类内的事件不会触发.

我相信是因为主线程不可用,并且OndataReceived Worker线程结束了,
所以我尝试将类实例化到线程中:new Thread(new ThreadStart(startclass));

但是在运行构造函数之后,线程结束,并且事件永不被调用.
如何保持该线程活动并可以处理事件?

谢谢

Dear readers,

Seem to have a little problem with threading.

I setup a socket connection which a new AsyncCallback(OnDataReceived);

From now my Main Thread is unavaible:
Let''s just call it System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

When the OnDataReceived is called it has to create a new class instance,
But events inside this class won''t fire.

I believe it''s because that the main thread is unavailible and the OndataReceived Worker Thread ends,
So i tried putting the class instancing into a Thread: new Thread(new ThreadStart(startclass));

But after running the constructor the thread ends and the events never get called.
How do i keep this thread alive and so that it can handle events?

Thanks

推荐答案

哇!您试图击败几乎所有您在这里尝试的目的不是很明显吗?

使用System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)与根本不使用线程相同.这样的睡眠是没有用的.您人为地创建了不可用的线程,并且作为变通方法,您创建了另一个线程!太可恶了!

首先,一旦使用了线程(是的,最好将所有网络都放在单独的线程中),为什么还要使用异步套接字呢?而是使用线程并使用阻塞调用.当线程正在使用阻塞调用时,它也处于睡眠状态",但以生产方式"处于睡眠状态,当系统将其关闭时,它将进入等待状态,并且不计划重新执行(因此浪费了时间)零CPU时间),直到它被唤醒.唤醒的原因包括:阻塞方法调用的完成,超时,Thread.InterruptThread.Abort.

也许您需要重新考虑整个设计.我不知道您想要实现什么,但是请查看我过去的一些答案,以了解有关设计的可行变体的想法:
来自同一端口号的多个客户端 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

-SA
Wow! Isn''t that obvious that you try to defeat the purpose of nearly everything you try here?

Using System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite) is the same as not using the thread at all. There is not situation when such kind of sleep is useful. You artificially create unavailable thread and as a work-around you create yet another thread! What an abuse!

First of all, once you use threads (and yes, you should better do all networking in separate threads), why using asynchronous sockets at all? Instead, use threads and use blocking calls. When the thread is using blocking call, it''s also "sleeping", but sleeping in a "productive manner", it enters the wait state when it is switched off by the system and is not scheduled back to execution (so it waste zero CPU time) until it is waken up. What wakes it up include: completion of blocking method call, timeout, Thread.Interrupt or Thread.Abort.

Perhaps you need to rethink the whole design. I don''t know what you want to achieve, but please look at some my past answers to get an idea on working variants of design:
Multple clients from same port Number[^].
This is my collection of links to some my past answers on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


如果实例化一个新线程,我相信您将必须注册新的事件处理程序.
If you instantiate a new thread, I believe you will have to register new event handlers.


那里是.net框架中的AutoResetEvent类...
使用此类的两个方法,分别称为WaitOne()和Set();



There is AutoResetEvent class in .net framework...
Use two methods of this class named WaitOne() and Set();



//declare this object on the constructor of that class...
AutoResetEvent obj = new AutoResetEvent(false);

// call the WaitOne() method after calling any asynchronous method or Thread...
obj.WaitOne();


// call the Set() method in as the last statement of asynchronous method or Thread that you have called before
obj.Set();


这篇关于线程和事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆