❔ How to organize Windows Forms application

Hi, I'm developing a Windows Forms app that can perform a bunch of translation-related tasks such as: 1) Parsing a c# file, looking for terms, listing those for which there is no translation on database and printing this list 2) Use a file containing <term, translation, language> records and write them onto database and so on Now, I'd like to organize properly this application. In particular, I'd like to keep in the Form class only the code related to the form controls, and everything else somewhere else. Furthermore, I'd like the front-end not to be frozen while the app is working, but rather showing progress bars and so on. I understand I need to use BackgroundWorker for this. So I have my form, where I'll initialize a BackgroudWorker... but where should the method that will be executed by the Background Worker be placed? And should I use services (like in .net web app)? Thanks!
8 Replies
Pobiega
Pobiega2y ago
You can absolutely use a service, or even some kind of "command" pattern.. The only problem is that your method needs to be "aware" of the background worker to be able to give progress updates If you are fine with not having a progressbar, instead just having some kind of "working..." prompt then it gets easier winforms supports having async void eventhandlers, so you can use that to await a method that is async Task<T> that does the actual heavy lifting without freezing your UI thread - but this gives no progress info back at all,
Pobiega
Pobiega2y ago
Just to demonstrate how easy this can be, here is the handler for the "start" button
private async void button1_Click(object sender, EventArgs e)
{
label1.Text = "Working...";
button1.Enabled = false;

await Task.Delay(5000);

button1.Enabled = true;
label1.Text = "Done";
}
private async void button1_Click(object sender, EventArgs e)
{
label1.Text = "Working...";
button1.Enabled = false;

await Task.Delay(5000);

button1.Enabled = true;
label1.Text = "Done";
}
Pobiega
Pobiega2y ago
as you can see, the UI thread is fully unblocked and can still handle clicks while we are "working" on the Task.Delay
alkasel#159
alkasel#159OP2y ago
Very interesting! I didn't know of this possibility. Thank you very much for the info! Actually I just discovered you can have tasks reporting back progress to caller
alkasel#159
alkasel#159OP2y ago
.NET Blog
Async in 4.5: Enabling Progress and Cancellation in Async APIs - .N...
The apps developers want to develop today are fast and fluid and the async features in .NET make this easier than ever. The world is also highly connected now which makes waiting for data a real concern for building great customer experiences.
alkasel#159
alkasel#159OP2y ago
This way I can do everything using just tasks, with no need of using a BackgroundWorker!
Pobiega
Pobiega2y ago
Yep that should work! Pass it in as an optional parameter It won't work for things that don't really progress thou, like waiting for a single database query
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server