multithreading - How to Kill a C# Thread? -
i've got thread goes out , looks data on our (old) sql server.
as data comes in, post information modal dialog box - user can't & shouldn't else while processing going on. modal dialog box let them see i'm doing , prevent them running query @ same time.
sometimes (rarely) when code makes call sql server, server not respond (it has down maintenance, lan line got cut, or pc isn't on network) or person doing query runs out of time. so, modal dialog box have cancel button.
the thread object (system.threading.thread) has isbackground=true
.
when clicks cancel, call killthread
method.
note: can not use backgroundworker component in class because shared windows mobile 5 code & wm5 not have backgroundworker.
void killthread(thread th) { if (th != null) { manualresetevent mre = new manualresetevent(false); thread thread1 = new thread( () => { try { if (th.isalive) { //th.stop(); // 'system.threading.thread' not contain definition 'stop' // , no extension method 'stop' accepting first argument of type // 'system.threading.thread' found (are missing using // directive or assembly reference?) th.abort(); } } catch (exception err) { console.writeline(err); } { mre.set(); } } ); string text = "thread killer"; thread1.isbackground = true; thread1.name = text; thread1.start(); bool worked = mre.waitone(1000); if (!worked) { console.writeline(text + " failed"); } th = null; } }
in output window, see "thread killer failed" no exception ever thrown.
how should stop thread?
the best related posts found 2 below:
edit:
there seems confusion method listed above.
first, when clicks cancel button, routine called:
void cancel_click(object sender, eventargs e) { killthread(mythread); }
next, when go in kill thread, i'd rather not have wait forever thread stop. @ same time, don't want let code proceed if thread still active. so, use manualresetevent
object. should not take full second (1000ms) stop thread, every time waitone
method times out.
still listening ideas.
short answer: don't. signaling want quit. if you're firing sql query, asynchronously (pardon spelling), , cancel if necessary. goes lengthy task in separate thread.
for further reading see eric lippert's articles: careful axe, part one: should specify timeout? , careful axe, part two: exceptions?
edit: how call sql server? ado, tds, standard/custom library, etc... ? call should made asynchrone. thus: startopeningconnection, waitfor openingcomplete, startquery, waitfor querycomplete, start closeconnection, waitfor closeconnectioncomplete etc. during of waits thread should sleep. after waking up, check if parent thread (the ui thread) has cancelled, or timeout has occurred , exit thread , possibly inform sqlserver you're done (closing connection).
it's not easy, is...
edit 2:in case, if unable change database code asynchrone, make seperate process , kill if neccesary. way resources (connection etc.) released. threads, won't case. but it's ugly hack.
edit 3: should use beginexecutereader/endexecutereader pattern. this article reference: require rewriting data access code, it's way properly.
Comments
Post a Comment