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

Netduino Tutorial - Multithreading (Methods with Parameters)


  • Please log in to reply
5 replies to this topic

#1 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 12 September 2010 - 05:33 PM

Video 1-


Video 2-


Code -
        public void Pulse(int onTime, int offTime, bool onFirst, uint repeat)
        {
            bool doit = false;
            _worker = new Thread(
                delegate() 
                { 
                    doit = doWork(onTime, offTime, onFirst, repeat); 
                }
                );
            _worker.Start();


        }

        private bool doWork(int onTime, int offTime, bool onFirst, uint repeat)
        {
            if (onFirst)
            {
                for (uint c = 0; c < repeat; c++)
                {
                    _pwmPort.SetDutyCycle((uint)Power);
                    Thread.Sleep(onTime);
                    _pwmPort.SetDutyCycle((uint)0);
                    Thread.Sleep(offTime);
                } 
            }
            else
            {
                for (uint c = 0; c < repeat; c++)
                {
                    Thread.Sleep(offTime);
                    _pwmPort.SetDutyCycle((uint)Power);
                    Thread.Sleep(onTime);
                    _pwmPort.SetDutyCycle((uint)0);
                }
            }
            
            return true;
        }


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 September 2010 - 06:46 PM

Multi-threading, the use of anonymous methods, and thread synchronization--wow, pretty sophisticated. Quick note: In your doWork() call from yourPublicMethod, you're passing one parameter to doWork--which has three. For your sample code, you might want to add two other parameters to make it complete. Chris

#3 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 12 September 2010 - 07:16 PM

Multi-threading, the use of anonymous methods, and thread synchronization--wow, pretty sophisticated.

Quick note:
In your doWork() call from yourPublicMethod, you're passing one parameter to doWork--which has three. For your sample code, you might want to add two other parameters to make it complete.

Chris


Thanks for catching that, didn't change it.

#4 Dave

Dave

    New Member

  • Members
  • Pip
  • 8 posts
  • LocationRock Hill, SC

Posted 13 September 2010 - 06:26 AM

Actually since you're not using the value returned you could do something like this, and do it all in one line of code....

       public void Pulse(int onTime, int offTime, bool onFirst, uint repeat)
        {
            new Thread(() => doWork(onTime, offTime, onFirst, repeat).Start();
        }

--Dave

#5 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 13 September 2010 - 07:37 PM

Actually since you're not using the value returned you could do something like this, and do it all in one line of code....

       public void Pulse(int onTime, int offTime, bool onFirst, uint repeat)
        {
            new Thread(() => doWork(onTime, offTime, onFirst, repeat).Start();
        }

--Dave

I see, but I think its still better to do _worker = new Thread(.....), because without that _worker =, then you can't Join the thread

#6 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 07 May 2011 - 04:03 PM

Just leaving a breadcrum to remember :) Thanks Omar !
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !




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.