Backgroundworker IsBusy not reporting correct status?
I have this code :
https://dotnetfiddle.net/pmiBoz
Which does not run in dotnetfiddle somehow, but it writes "False" to console whenever the worker.IsBusy is encountered. Am I doing something wrong?
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
21 Replies
probably because they have a timeout in certain amount of time and all your delays go above that making so it does show what u wanna see in there.
is the await periodictimer not considered async work?
the problem with your code is that you're not creating a worker
you're simple passing your method as an object
or at least that is what I understood
I dont understand what you mean with that, I'm creating a worker on line 6 right?
![No description](https://cdn.answeroverflow.com/1209559240306597958/image.png)
but yes apparently only synchronous operations make the worker busy
I see, that is weird ,but thx 🙂
BackgroundWorker is mainly used to leverage long running synchronously UI tasks as to not hang your ui by running it in a separated thread and having means to let u update the ui from the ui thread by using the progresschanged/and completed events
Im using them for threaded processes
should I be using something else?
Task Factory I would guess, I guess it depends what kind of work they are doing and it they would benefit from being async
https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskfactory?view=net-8.0
Hmm I see
I'm checking out this article https://www.codeproject.com/Articles/5352757/Tasks-BackgroundWorkers-and-Threads-Simple-Compari
CodeProject
Tasks, BackgroundWorkers, and Threads – Simple Comparisons for Conc...
How Tasks, Threads, and BackgroundWorkers operate at high level
well like I said it depends what kind of operations you're doing to determinate what would be best to your scenario
if they involve IO or not etc
They involve IO and network IO
well async/await is great for IO operations because it runs asynchronously while waiting for the system to return without blocking the thread, it may or maynot run on an exclusive thread depending on scenarios as the threadpool decides where to place it, whereas a thread would be exclusive.
so like if you're doing API calls etc with periodic calls a long running task factory could be just what u need
hopefully I explained it right but perhaps some one more knowledge in the in's and out's of threading vs task vs task factory might chim in and say a few words 😉