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

Multi-threading in C#, HELP please.


  • Please log in to reply
6 replies to this topic

#1 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 03 November 2012 - 01:57 AM

Hello, this is my first post on here. I have been tooling around with my new Netduino Plus for a few weeks now and I've been wanting to be able to do multiple things at once, such as controlling two motor speeds and directions. I have done a little digging and have come to find that what I am trying to do it called Multi-Threading. So, to learn, I started with a fairly simple process. I have created a three LED marquee, and a fourth LED that turns on when I press a button. The problem is that I keep getting an error in the debug saying I have unreachable code. Can anyone help me fix this issue? Here is my code:

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;

namespace NetduinoPlusApplication1
{
    public class Program
    {
        static void Main()
        {
            Thread t = new Thread(RedCheck);
            t.Start();

            OutputPort bLED = new OutputPort(Pins.GPIO_PIN_D5,false);
            OutputPort gLED = new OutputPort(Pins.GPIO_PIN_D6,false);
            OutputPort rLED = new OutputPort(Pins.GPIO_PIN_D9,false);
            AnalogInput Pot = new AnalogInput(Pins.GPIO_PIN_A0);
            AnalogInput PV = new AnalogInput(Pins.GPIO_PIN_A1);

            Pot.SetRange(5, 500);
            int PotValue = 0;

            while(true)
            {
                PotValue = Pot.Read();
                gLED.Write(true);
                Thread.Sleep(PotValue);
                gLED.Write(false);
                
                PotValue = Pot.Read();
                bLED.Write(true);
                Thread.Sleep(PotValue);
                bLED.Write(false);
                
                PotValue = Pot.Read();
                rLED.Write(true);
                Thread.Sleep(PotValue);
                rLED.Write(false);                
               
            }
           
        }
        static void RedCheck()
        {
            OutputPort CheckLED = new OutputPort(Pins.GPIO_PIN_D0, false);
            InputPort button = new InputPort(Pins.GPIO_PIN_A2, false, Port.ResistorMode.PullUp);

            bool buttonstate = false;

            while(true)
                
                buttonstate = !button.Read();
                CheckLED.Write(buttonstate);
    }

    }
}

Thank you for your input.

Edited by Chris Walker, 03 November 2012 - 10:01 PM.
added [code][/code] tags


#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 03 November 2012 - 09:18 AM

Your code seems okay. Isn't that the "unreachable code" is just a simple warning instead? A warning shouldn't block the program execution. Cheers
Biggest fault of Netduino? It runs by electricity.

#3 patduino

patduino

    Member

  • Members
  • PipPip
  • 20 posts
  • LocationIndiana, USA

Posted 03 November 2012 - 11:48 AM

Look at the while loop in your RedCheck function. You want to use curly brackets around the two lines that should be in the loop. The while(true) is only looping through the one line that follows it, not both. The second line will never get executed, thus being unreachable. Your "check" LED probably doesn't work, does it? - Pat
There are 10 types of people in the world... Those that can understand binary and those who can't.

#4 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 03 November 2012 - 12:31 PM

you have to write: while(true) { // our code here } <- all from here on never gets executed

#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 03 November 2012 - 10:03 PM

As Pat noted, the following line of code isn't in your while(true) loop...
CheckLED.Write(buttonstate);

If you put curly braces around those two lines of code, they'll both belong to your while loop...and things should start working properly.

BTW, I would avoid having tight loops like that on a background thread. I would recommend adding a "Thread.Sleep(1)" inside that loop so that the other threads, such as the main thread, don't have to wait a few milliseconds for NETMF to force a thread-switch. Otherwise your application will be sluggish.

Chris

#6 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 04 November 2012 - 09:08 AM

Just wondering if you would be better using an InterruptPort instead of RedCheck that way you do not have to use threading. Regards, Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#7 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 05 November 2012 - 03:08 AM

Thank guys! I wrote the code late at night, and totally missed the curlies.... It works very well now.




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.