C
C#•7mo 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•7mo 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
Pannekoekje•7mo ago
is the await periodictimer not considered async work?
leowest
leowest•7mo 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
Pannekoekje•7mo ago
I dont understand what you mean with that, I'm creating a worker on line 6 right?
leowest
leowest•7mo ago
No description
leowest
leowest•7mo ago
but yes apparently only synchronous operations make the worker busy
Pannekoekje
Pannekoekje•7mo ago
I see, that is weird ,but thx 🙂
leowest
leowest•7mo 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
Pannekoekje•7mo ago
Im using them for threaded processes should I be using something else?
leowest
leowest•7mo 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
Pannekoekje•7mo ago
Hmm I see
leowest
leowest•7mo 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
Pannekoekje•7mo ago
They involve IO and network IO
leowest
leowest•7mo 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 😉
Want results from more Discord servers?
Add your server