Any plans for have ParameterizedThreadStart available in the MF?
#1
Posted 10 July 2012 - 04:07 AM
#2
Posted 10 July 2012 - 12:41 PM
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
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
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
Posted 13 July 2012 - 04:23 PM
#6
Posted 13 July 2012 - 04:49 PM
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.)Didn't think of that, do you know if lambda expressions are supported in Framework v4.1 or only in v4.2?
Cuno
#7
Posted 13 July 2012 - 05:27 PM
#8
Posted 13 July 2012 - 11:24 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users