c# - How to call Thread.Join for all threads in Win Forms -
i have windows form listed below. has multiple background threads, sta
, etc… have function named myfinalpiece()
. need join threads associated form before calling method.
how can call thread.join here threads (irrespective of how many threads there)?
note: if add new thread in future call should work without break.
code
public partial class form1 : form { int lognumber = 0; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { writelogfunction("**"); //......other threads //..main thread logic //all threads should have been completed before this. myfinalpiece(); } private void myfinalpiece() { } private void writelogfunction(string strmessage) { string filename = "mylog_" + datetime.now.tostring("yyyymmmmdd"); filename = filename + ".txt"; using (streamwriter w = file.appendtext(filename)) { w.writeline("\r\n{0} ..... {1} + {2}ms >>> {3} ", lognumber.tostring(), datetime.now.tolongtimestring(), datetime.now.millisecond.tostring(), strmessage); lognumber++; } } }
may can use tasks shown:
writelogfunction("**"); //......other threads var task1 = task.run(() => this.someotherthread1()); var task2 = task.run(() => this.someotherthread2()); //..main thread logic task.waitall(task1, task2); //all threads should have been completed before this. myfinalpiece();
Comments
Post a Comment