Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

thread starts thread but 2nd thread does not finish


  • Please log in to reply
3 replies to this topic

#1 watzlaf

watzlaf

    New Member

  • Members
  • Pip
  • 2 posts

Posted 15 February 2012 - 08:47 PM

Hello,

I have the following code in a function:
            new Thread(
                delegate(){
                    while (true) {
                        Socket clientSocket = _listeningSocket.Accept();
                        WebHandler handler = new WebHandler(clientSocket);
                        Thread worker = new Thread(handler.handleRequest);
                        worker.Start();
                    }
                }
            ).Start();
This function is called by Main. If I start a request the worker thread is started and after a short while interrupted by the thread above(worker thread has not finished). The Accept method blocks the thread and the worker thread never gets back focus and cannot be finished. Can someone explain this behaviour?
Thx.
Watzlaf

#2 Magpie

Magpie

    Advanced Member

  • Members
  • PipPipPip
  • 279 posts
  • LocationAustralia (south island)

Posted 15 February 2012 - 11:22 PM

Two things I can think of 1. Did you send a packet? otherwise it will block forever. 2. Does your inline delegate cause an exception to be thrown? Ps. I wouldn't use inline delegates, I find them so confusing. I would put the .Start () on a separate line too, to make it easier to debug. Also any thread you start should probably have a try catch finally, unless you are 100% sure you wont get an exception.
STEFF Shield High Powered Led Driver shield.

#3 Bainesbunch

Bainesbunch

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts
  • LocationFrance

Posted 16 February 2012 - 02:03 PM

Why does this code loop in a "while" creating new instances of the thread and then starting them. Surely if you re instantiate a thread using the same object variable it will mess with the thread created the first time through the loop. I am not a c# coder so i could be getting it all wrong. Cheers Pete.
I was going to change the world, then I discovered Netduino.
The world will have to wait.

#4 watzlaf

watzlaf

    New Member

  • Members
  • Pip
  • 2 posts

Posted 16 February 2012 - 02:47 PM

1. Did you send a packet? otherwise it will block forever.

Yes. I sent a packet. Worker thread is started. I set breakpoints at the beginning of the worker thread function (handler.handleRequest) and the execution was stopped there.
If I do a step by step execution the worker thread can be finished.

2. Does your inline delegate cause an exception to be thrown?

There is no exception handling (try/catch or using). So if a exception will be thrown I will notice it.


Ps. I wouldn't use inline delegates, I find them so confusing.
I would put the .Start () on a separate line too, to make it easier to debug.
Also any thread you start should probably have a try catch finally, unless you are 100% sure you wont get an exception.

Normally I don't use delegate. Here I used the inline delegate to shorten the displayed code in the forum ;).


Why does this code loop in a "while" creating new instances of the thread and then starting them.

Surely if you re instantiate a thread using the same object variable it will mess with the thread created the first time through the loop.

I am not a c# coder so i could be getting it all wrong.

Cheers Pete.

Intention is to have a server waiting for incoming requests. Handling of the request (analyse request, create response, send response) shall be done in a separate thread so that other incoming requests can be handled (e.g. two connections at the same time).

Socket clientSocket = _listeningSocket.Accept();
blocks until an incoming connection attempt.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.