Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?


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[^],
Control events not firing after enable disable + multithreading[^].

Share this answer
Albin Abel 16-May-11 6:45am    
Helpful collections. My 5
Sergey Alexandrovich Kryukov 16-May-11 6:49am    
Thank you, Albin.
If you instantiate a new thread, I believe you will have to register new event handlers.
Share this answer
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...

// call the Set() method in as the last statement of asynchronous method or Thread that you have called before
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900