C
C#•9mo ago
Pannekoekje

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
leowest
leowest•9mo ago
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.
Pannekoekje
PannekoekjeOP•9mo ago
is the await periodictimer not considered async work?
leowest
leowest•9mo ago
the problem with your code is that you're not creating a worker you're simple passing your method as an object
worker.DoWork += Worker_DoWork;
worker.DoWork += Worker_DoWork;
private static async void Worker_DoWork(object? sender, DoWorkEventArgs e)
{
// your worker code here
}
private static async void Worker_DoWork(object? sender, DoWorkEventArgs e)
{
// your worker code here
}
or at least that is what I understood
Pannekoekje
PannekoekjeOP•9mo ago
I dont understand what you mean with that, I'm creating a worker on line 6 right?
leowest
leowest•9mo ago
No description
leowest
leowest•9mo ago
but yes apparently only synchronous operations make the worker busy
Pannekoekje
PannekoekjeOP•9mo ago
I see, that is weird ,but thx 🙂
leowest
leowest•9mo ago
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
Pannekoekje
PannekoekjeOP•9mo ago
Im using them for threaded processes should I be using something else?
leowest
leowest•9mo ago
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
Pannekoekje
PannekoekjeOP•9mo ago
Hmm I see
leowest
leowest•9mo ago
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
Pannekoekje
PannekoekjeOP•9mo ago
They involve IO and network IO
leowest
leowest•9mo ago
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 😉
Pannekoekje
PannekoekjeOP•9mo ago
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
leowest
leowest•9mo ago
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
Pannekoekje
PannekoekjeOP•9mo ago
.NET console app I dont really have to check status/iscompleted as they are long running operations (logging, listening to subscribed websocket)
leowest
leowest•9mo ago
well you were trying to check for the IsBusy, I assume u had a reason for it
Pannekoekje
PannekoekjeOP•9mo ago
I wanted to check whether I can do something with it or not, but seems like no :)]
Lex Li
Lex Li•9mo ago
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.
Want results from more Discord servers?
Add your server