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

while(true)


  • Please log in to reply
9 replies to this topic

#1 sebastianpatten

sebastianpatten

    Member

  • Members
  • PipPip
  • 21 posts

Posted 18 February 2011 - 12:47 AM

I'm doing some code for my NetDuino Plus to act as a web server. So basically right now I put a request to http://localhost:8080 and its responding with a 200 OK - Hello World.

Im building and testing this using the emalator. Without a while(true) the application quits. Is this what I should be doing? Is there a better way to keep the program running and not just drop out?

Thanks!

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using MFToolkit.Net.Web;

namespace WebServer
{
    public class Program
    {
        public static void Main()
        {
            using (HttpServer httpServer = new HttpServer(8080, new MyHttpHandler()))
            {
                httpServer.Start();
            }
        }
    }

    public class MyHttpHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            BuildHttpResponse(context.Response);
        }

        public void BuildHttpResponse(HttpResponse httpResponse)
        {
            httpResponse.HttpStatus = HttpStatusCode.OK;
            httpResponse.WriteLine("Hello World");
        }
    }
}


#2 Dan Morphis

Dan Morphis

    Advanced Member

  • Members
  • PipPipPip
  • 188 posts

Posted 18 February 2011 - 12:49 AM

Im building and testing this using the emalator. Without a while(true) the application quits. Is this what I should be doing? Is there a better way to keep the program running and not just drop out?


Thread.Sleep(Timeout.Infinite);

Will work great.

#3 sebastianpatten

sebastianpatten

    Member

  • Members
  • PipPip
  • 21 posts

Posted 18 February 2011 - 01:25 AM

Yes this works. It looks nicer too. Thanks.

#4 Mark H

Mark H

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationPerth, Western Australia

Posted 18 February 2011 - 01:58 AM

while (true) is bad to use (Thread.Sleep is the correct way) because it is basically keeping the CPU doing something constantly. This makes it so that other threads may be delayed in getting access to processor time giving you a slow response and also doesn't let the chip go into a power saving mode.

#5 Luke Cummings

Luke Cummings

    Advanced Member

  • Members
  • PipPipPip
  • 38 posts
  • LocationCalgary, AB

Posted 18 February 2011 - 07:07 AM

There is another way to grab execution without requiring the checking of a while, try this:
for(;;);

It looks funny but if you think about how a for loop works you can see how this basically turns into a looping nop instruction.

That said since you are dealing with an interpreted language I'm not sure if this would actually turn into an infinitely looping nop instruction like it would in c.


This makes it so that other threads may be delayed in getting access to processor time giving you a slow response and also doesn't let the chip go into a power saving mode.


It shouldn't delay any other threads as the processor is always doing "something" nop's or otherwise, but you're correct that it will increase power consumption.

Edit: This statement is unverifiably vauge
Cheap, Fast, Good... Pick two

#6 Mark H

Mark H

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationPerth, Western Australia

Posted 18 February 2011 - 07:18 AM

It actually does delay other threads, try it :) Other threads will wait up to 5ms depending on their thread priority.

#7 Miha Markic

Miha Markic

    Advanced Member

  • Members
  • PipPipPip
  • 74 posts
  • LocationNova Gorica, Slovenia

Posted 18 February 2011 - 08:05 AM

It actually does delay other threads, try it :) Other threads will wait up to 5ms depending on their thread priority.


Any loop will cause CPU to work 100%. Even if it is doing NOPs it will do that all the time. I guess placing Thread.Sleep(something) statements within the loop will greatly reduce the CPU utilization.
However, as previously stated, avoid the loops if they are not necessary.

Miha Markic, Microsoft MVP C#
Righthand .net consulting and software development
http://blog.rthand.com/


#8 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 18 February 2011 - 08:07 AM

Yup, Mark speaks the truth.

An empty while(true) loop (or for(;;);) will basically create a thread which executes an infinite number of nop and loop instructions.

Since .NET Micro Framework is a cooperative multitasking framework, the thread scheduler will happily give the while(true) thread a big chunk of execution time (up to 20ms) before taking execution away and awarding it to another thread.

If you want to put a thread to sleep forever (particularly your main thread), use:
Thread.Sleep(Timeout.Infinite);
Chris

#9 Fred

Fred

    Advanced Member

  • Members
  • PipPipPip
  • 302 posts
  • LocationUK

Posted 18 February 2011 - 10:40 AM

I tend to go for a middle ground of:
while (true)
  Thread.Sleep(10000);

I just have a feeling that if you're debugging it gives Visual Studio a chance to interrupt execution every 10s. I have to say I can't really justify it any better than that though!

#10 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 18 February 2011 - 10:42 AM

I tend to go for a middle ground of:

while (true)
  Thread.Sleep(10000);

I just have a feeling that if you're debugging it gives Visual Studio a chance to interrupt execution every 10s. I have to say I can't really justify it any better than that though!


Hi Fred,

Unless there's a bug in the .NET MF firmware which disables the thread scheduler, the Visual Studio debugger should get a timeslice along with the other threads (and the repeated 10 sec sleep won't have any notice-able effect).

That said, you're welcome to use whatever feels natural...

Chris




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.