.net - C# infinitive task loop using Task<> class + cancellation -
i`m trying make small class multithreading usage in winform projects.
tried threads(problems ui), backgroundworker(smth went wrong ui too, leave now:)), trying task class. now, can`t understand, how make infinitive loop , cancelling method (in class) running tasks. examples found used in 1 method.
so, here structure & code of working part (worker.css , methonds used in winform code).
worker.css
class worker { public static int threadcount { get; set; } public void dowork(parameterizedthreadstart method) { task[] tasks = enumerable.range(0, 4).select(i => task.factory.startnew(() => method(i))).toarray(); } }
usage on form1.cs
private void start_btn_click(object sender, eventargs e) { worker.threadcount = 1; //actually doesn`t using now, number of tasks declared in class temporaly worker worker = new worker(); worker.dowork(job); string logstring_1 = string.format("starting {0} threads...", worker.threadcount); log(logstring_1); } public static int j = 0; private void job(object sender) { worker worker = new worker(); random r = new random(); log("thread "+thread.currentthread.managedthreadid +" working..."); (int = 0; < 5; i++) { j++; log("j==" + j); if (j == 50) { //worker.stop(); log("stop"); } } thread.sleep(r.next(500, 1000)); }
so, run example 4 threads, executed, got j==20 in log, it`s ok.
my question is, how implement infinitive loop tasks, created worker.dowork() method. , make .stop() method worker class (which should stop tasks when called). understand it`s related questions, put in 1.
i tryed solutions, of them based on cancellationtoken usage, have create element inside of worker.dowork() method, can`t use same token create worker.stop() method.
someone can help? threads amount range have use in software 5-200 threads. using j computation example of the easy condition used stop software work(stop of tasks/threads). in real, stop conditions queue<> finished, or list<> elements empty(finished).
finally, works.
class worker { public static int threadcount { get; set; } task[] tasks; //ex data public static string exception; static cancellationtokensource wtoken = new cancellationtokensource(); cancellationtoken cancellationtoken = wtoken.token; public void dowork(parameterizedthreadstart method) { try { tasks = enumerable.range(0, 4).select(i => task.factory.startnew(() => { while (!cancellationtoken.iscancellationrequested) { method(i); } }, cancellationtoken)).toarray(); } catch (exception ex) { exception = ex.message; } } public void hardstop() { try { using (wtoken) { wtoken.cancel(); } wtoken = null; tasks = null; } catch (exception ex) { exception = ex.message; } } }
but if i`m using method quit cancellationtoken.throwifcancellationrequested(); error: when job() method reach j == 50, , worker.hardstop() function called, program window crashes , , exception "oparetioncanceledexception unhandled user code" on string
cancellationtoken.throwifcancellationrequested();
so, whats wrong? i`m put in try{} catch(){} understood, boolean properties should changed in task (task.iscancelled == false, task.isfaulted == true) on wtoken.cancel();
Comments
Post a Comment