There is a potential problem with the ThreadStart
delegate that you pass to the Thread
's constructor, which is defined as public delegate void ThreadStart()
. The fact that you provide an async void
lambda for it makes it a fire-and-forget call. I.e., it's asynchronous but it doesn't return a Task
to observe for result or exceptions.
Your new thread will most likely end as soon as the execution flow inside it hits the first await something
, be it await Task.Delay
or anything else. So, technically, you're not pausing a thread here. The logical execution after that await
will continue on a random thread pool thread, which will most likely be different from the thread you initially created.
You'd be better off just using Task.Run
instead of new Thread
. The former has an override for async Task
lambdas, which you should normally be using instead of async void
anyway. Thus, you could pass your UpdateAsync
directly to Task.Run
and have the proper exception propagation logic for async methods.
If for some reason you still want to stick with new Thread
and pass an async void
lambda to it, make sure to observe all exception thrown by UpdateAsync
. Otherwise, they will be thrown "out-of-band" on a random pool thread, see the above link for more details. Also note, creating a new thread (and then almost instantly ending it) is a rather expensive runtime operation. OTOH, when using Task.Run
, you normally just borrow/return an existing thread from/to thread pool, which is much faster.
That said, in this particular case you may as well just be using Thread.Sleep
instead of async
methods and Task.Delay
, to avoid having to deal with asynchrony and thread switching at all. It's a client-side WinForms application where you normally don't care (to a reasonably extent) about scaling, i.e., the number of busy or blocked threads.