vb.net - thread pool error trapping -
checking on best practice here.
i calling threadpool run async process, sending ssl email attachment seems take long while.
this test code, thought drop in try catch incase failed. thread ever come here? have tested closing web page, browser, clicking button on page. email makes through.
try system.threading.threadpool.queueuserworkitem(addressof doasyncwork) ' catch ex exception throw ex exit sub end try i not trying force failure, yet guess, know how best trap if thread fails.
protected sub doasyncwork(byval state object) dim omyobject new sendsslemail omyobject.ssl(username, , strmessagebody, emailadd, , permfilelocation, , "codemsg") end sub
a more convenient way of doing work thread pool use task.factory.startnew(action). returns task object can awaited or blocked on wait.
once task completes, exception property of task can used determine whether exception thrown , unhandled task's subroutine. (if it's not nothing, innerexception property has real exception thrown.)
dim task = task.factory.startnew(addressof workfunction) ' stuff doesn't depend on workfunction having completed task.wait() if task.exception isnot nothing throw task.exception.innerexception after throw, sub exited anyway (the call stack unwound looking catch), exit sub statement nothing. wrapping queueuserworkitem call in try block nothing, because exception occur on other thread. exceptions thrown queueuserworkitem, off top of head includes complaints delegate being nothing.
asynchronous tasks can return values. more information on that, see the taskfactory methods return func(of task).
Comments
Post a Comment