c# - Thread vs Threadstart -
in c#, practically, haven't observed difference between following:
new thread(somemethod).start();
,
new thread(new parameterizedthreadstart(somemethod));
and
new thread(new threadstart(somemethod));
what difference, if there @ all?
the thread(threadstart)
constructor can used when signature of somemethod
method matches threadstart
delegate. conversely, thread(parameterizedthreadstart)
requires somemethod
match parameterizedthreadstart
delegate. signatures below:
public delegate void threadstart() public delegate void parameterizedthreadstart(object obj)
concretely, means should use threadstart
when method not take parameters, , parameterizedthreadstart
when takes single object
parameter. threads created former should started calling start()
, whilst threads created latter should have argument specified through start(object)
.
public static void main(string[] args) { thread threada = new thread(new threadstart(executea)); threada.start(); thread threadb = new thread(new parameterizedthreadstart(executeb)); threadb.start("abc"); threada.join(); threadb.join(); } private static void executea() { console.writeline("executing parameterless thread!"); } private static void executeb(object obj) { console.writeline("executing thread parameter \"{0}\"!", obj); }
finally, can call thread
constructors without specifying threadstart
or parameterizedthreadstart
delegate. in case, compiler match method constructor overload based on signature, performing cast implicitly.
thread threada = new thread(executea); // implicit cast threadstart threada.start(); thread threadb = new thread(executeb); // implicit cast parameterizedthreadstart threadb.start("abc");
Comments
Post a Comment