c# - Trying to finish only one thread from various -
first of all, must clarify i'm new working threads. now, have application executes multiple threads in different times. mean, have objects each of them executes thread in moment.
i'm gonna more specific. have list of task, each associated particular object. when click of button (that apply object), task associated starts run. in moment, can have more 1 thread. works correctly. problem when finish 1 of them. finish 1 thread, of rest stop too.
of course, there wrong in implementation. don't understand why threads stopped.
here implementation (i'm working in mvc on windows application forms):
in main forms have
//this method starts when press button specific object private void starttask( int idtask ) { int counter = this.controller.gettaskssize(); //this list<objecttask>, method returns count (int = 0; < counter; i++) { //gettasks() returns list<objecttask> if (this.controller.gettasks()[i].idtask == idtask) { threadstart tstask = new threadstart(() => taskloop(this.controller.gettasks()[i].idtask, this.controller.gettasks()[i].time, this.controller.gettasks()[i].mode)); thread task = new thread(tstask); this.controller.gettasks()[i].task = task; this.controller.gettasks()[i].task.start(); task = null; break; } } } private void stoptask(int idtask) { int counter = this.controller.gettaskssize(); (int = 0; < counter; i++) { if (this.controller.gettasks()[i].idtask == idtask) { try { if (this.controller.gettasks()[i].task != null && this.controller.gettasks()[i].task.isalive) this.controller.gettasks()[i].task.abort(); } catch (threadabortexception e) { } break; } } }
my list of objecttask is
public class objecttask { private int idtask; public int idtask { { return idtask; } set { idtask = value; } } private int time; public int time { { return time; } set { time = value; } } private bool mode; public bool mode { { return mode; } set { mode = value; } } private thread task; public thread task { { return task; } set { task = value; } } }
at same time, when stop 1 thread, can see following message on console:
the thread '' (0x1764) has exited code 0 (0x0). first chance exception of type 'system.threading.threadabortexception' occurred in mscorlib.dll
i've been searching , saw these questions
- a first chance exception of type 'system.threading.threadabortexception' occurred in mscorlib.dll
- system.threading.threadabortexception occurred in mscorlib.dll occuring persitently
- stopping 1 thread
- multi threading c# windows forms
but no 1 me.
if necessary more information, please let me know.
instead of using thread class, why don't use tasking library? (ref https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.run(v=vs.110).aspx)
you can use waitall function wait tasks finish. (ref https://msdn.microsoft.com/en-us/library/dd270695(v=vs.110).aspx)
Comments
Post a Comment