c# - ManagedThreadId return same value for 2 threads -
please consider code:
textbox1.text = "enter thread current thread id : " + thread.currentthread.managedthreadid.tostring(); int result = 0; task t = task.factory.startnew(delegate { (int = 0; < 5; i++) { result = result + i; system.threading.thread.sleep(1000); this.invoke((action)(() => textbox1.text += environment.newline + result.tostring() + " current thread id : " + thread.currentthread.managedthreadid.tostring())); } },cancellationtoken.none, taskcreationoptions.none);
i got result:
enter thread current thread id : 10
0 current thread id : 10
1 current thread id : 10
3 current thread id : 10
6 current thread id : 10
10 current thread id : 10
i ran code on windows form application. why thread.currentthread.managedthreadid
return same value ui thread , task thread? code inside of task
run in separate thread?
thanks
when did this.invoke
told go ui thread. move thread.currentthread.managedthreadid
outside of invoke , see different id ui thread.
also if want guarantee work done on thread pool thread either use task.run
or pass startnew taskscheduler.default
, if not possible have code invoked task.factory.startnew
accedently run on ui thread. see startnew dangerous
Comments
Post a Comment