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?
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 😉
I gave a very simple example, my code has a lot of backgroundworkers in use, which I think I can replace with Tasks but I don't see how exactly for now 🙂
And reading up on it it doesn't seem to be a big improvement
is this a console app or asp.net or ui(Winforms, WPF, etc)?
well for one u have Status and IsCompleted which u can check which is apparently something u can't do right now with BW
.NET console app
I dont really have to check status/iscompleted as they are long running operations (logging, listening to subscribed websocket)
well you were trying to check for the IsBusy, I assume u had a reason for it
I wanted to check whether I can do something with it or not, but seems like no :)]
Whenever you want to use
BackgroundWorker
, simply stop and use async
/await
instead, https://halfblood.pro/how-to-replace-backgroundworker-with-async-await-and-tasks-80d7c8ed89dc Much more simpler.