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

Application always in running


  • Please log in to reply
4 replies to this topic

#1 Paolo Patierno

Paolo Patierno

    Advanced Member

  • Members
  • PipPipPip
  • 169 posts
  • LocationItaly

Posted 17 September 2010 - 07:04 PM

Hello, I'm a newbie to .Net Micro Framework and Netduino board...my question is simple... Because after boot, the CLR executes Main() method in Program class in .Net Micro Framework project if the Main() method hasn't some codes to loop the application, it ends after the last instruction. To force application always running there are two possibilities : - while (true) loop; - Thread.Sleep(Timeout.Infinite) at the bottom of the Main() method; What are the differences ? Does another way exist ? Thanks, Paolo

Paolo Patierno

Microsoft MVP on Windows Embedded & IoT

Azure Advisor

Twitter : @ppatierno
Linkedin : paolopatierno
Blog : DevExperience

Blog : Embedded101
?


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 September 2010 - 07:52 PM

Hi Paulo, Those are the two basic options. It sounds like you understand them both well, but it's probably worth explaining the details a bit more here for others. If you use while(true), the code within the loop will run over and over again. If you use Thread.Sleep(Timeout.Infinite), then your other threads/event handlers will continue--but no more code will run in the Main method. If you create code in Main() but then let execution stop, everything in the background will end. Now, there may be another creative option that someone will come up with. :) Chris

#3 Ravenheart

Ravenheart

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationBulgaria

Posted 17 September 2010 - 08:20 PM

What I do is write all my code in separate classes out of Program.cs with one class being the central unit, then I declare a static variable in Program.cs of the type of my central unit class, instantiate the variable in Main() and at that point the central unit class will handle everything and I use Thread.Sleep(-1); at the end. Its wasteful to lock the Main() method in an endless loop, even if it does nothing.

#4 Paolo Patierno

Paolo Patierno

    Advanced Member

  • Members
  • PipPipPip
  • 169 posts
  • LocationItaly

Posted 18 September 2010 - 10:36 AM

What I do is write all my code in separate classes out of Program.cs with one class being the central unit, then I declare a static variable in Program.cs of the type of my central unit class, instantiate the variable in Main() and at that point the central unit class will handle everything and I use Thread.Sleep(-1); at the end.

Its wasteful to lock the Main() method in an endless loop, even if it does nothing.


Your is only a different design solution and I agree with you but the functionality is the same.
I won't use while loop because in this way the cpu load is always 100%.
I have understood that with the statement Thread.Sleep(-1) at the end of the Main() method, the process is active but the main thread sleeps forever but the event handlers (for example for InterruptPort) are active to catch events.
Is it right ?

Thanks,
Paolo

Paolo Patierno

Microsoft MVP on Windows Embedded & IoT

Azure Advisor

Twitter : @ppatierno
Linkedin : paolopatierno
Blog : DevExperience

Blog : Embedded101
?


#5 Eric Burdo

Eric Burdo

    Advanced Member

  • Members
  • PipPipPip
  • 130 posts

Posted 18 September 2010 - 11:34 AM

And for fun (I do this on some of my test apps), I code for the integrated switch (SW1). If you press that... the application ends. I only do this on my test apps... but it lets me end the app on the Netduino, so it's not always running anymore.
~ Eric D. Burdo ~ http://brick-labs.com/

Today LED's, tomorrow, the world!!! Well, OK, maybe servos.




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.