Any plans for have ParameterizedThreadStart available in the MF? - General Discussion - Netduino Forums
   
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

Any plans for have ParameterizedThreadStart available in the MF?


  • Please log in to reply
7 replies to this topic

#1 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 10 July 2012 - 04:07 AM

Good day everyone. Any plans to have ParameterizedThreadStart available in the MF? Thanks

#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 10 July 2012 - 12:41 PM

Don't know about the plans, but in the meantime this might do:

class param
{
    private int p;

    public param(int p)
    {
        // store the param
        this.p = p;
    }

    public void worker()
    {
        // use the param
        if (p == 1)
            Debug.Print("param is 1");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // create a new thread with the integer value 1 as parameter
        Thread t = new Thread(new param(1).worker);

        // start it
        t.Start();
    }
}


#3 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 10 July 2012 - 03:38 PM

Don't know about the plans, but in the meantime this might do:

class param
{
    private int p;

    public param(int p)
    {
        // store the param
        this.p = p;
    }

    public void worker()
    {
        // use the param
        if (p == 1)
            Debug.Print("param is 1");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // create a new thread with the integer value 1 as parameter
        Thread t = new Thread(new param(1).worker);

        // start it
        t.Start();
    }
}



Thank you Hanzibal

#4 Nobby

Nobby

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 13 July 2012 - 03:41 AM

Good day everyone.

Any plans to have ParameterizedThreadStart available in the MF?


Thanks


You can use lambda expressions in conjunction with the standard ThreadStart class. It allows you to have type-safe targets as well.

class MyClass
{
     Thread myThread = null;

     public MyClass()
     {
         this.myThread = new Thread(() => MyClass.threadFunc(this));
         this.myThread.Start();
     }

     private static void threadFunc(MyClass myClassObject)
     {
            //do stuff here
     }
}

Target delegate doesn't have to be static either

#5 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 13 July 2012 - 04:23 PM

Didn't think of that, do you know if lambda expressions are supported in Framework v4.1 or only in v4.2?

#6 Cuno

Cuno

    Advanced Member

  • Members
  • PipPipPip
  • 144 posts
  • LocationZürich / Switzerland

Posted 13 July 2012 - 04:49 PM

Didn't think of that, do you know if lambda expressions are supported in Framework v4.1 or only in v4.2?

Lambdas purely a compiler mechanism; no special support is needed in the framework. So yes, lambdas work in both versions. (I even used them briefly in my book I think.)

Cuno

#7 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 13 July 2012 - 05:27 PM

Great, thanks!

#8 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 13 July 2012 - 11:24 PM

Awesome, this is some great info, I have to try it out. Thank you both.




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.