c# - Stop a thread if it takes too long -
i new in windows phone development , trying create windows phone app using c#
thread t = new thread(doaheavywork); t.start(); if (!t.join(1000)) // give operation 1s complete { t.abort(); }
i cannot alter doaheavywork function.
i need result omputed within 1 or 2 seconds, since may run long time.
i have read using abort wrong way.
i using above in photochoosertask complete function. first run executes fine. is, when click button select photo, doaheavywork function doesn't exceed 1 sec. if try second time, photochoosertask throws exception , stops.
is because aborted process in 1st run? or else? there other way it?
.abort()
causes thread destroyed completely. if generating new thread each time in example, work. if using same t
object, need create new thread object, can't run .start()
on aborted thread.
however fact aborting thread concern. happens application when take more 2 seconds. should showing user, please wait, taking longer expected. doing in if
block it. .join()
won't stop thread if doesn't manage join.
edit
you have 2 options might want consider in rewriting section:
- backgroundworker - http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx
- thread task - http://msdn.microsoft.com/en-us/library/windowsphone/develop/cc221403(v=vs.105).aspx
tasks seem appropriate solution in scenario.
Comments
Post a Comment