Question Main Loop in Windows Form

Abdul Hayee

Member
Joined
Mar 31, 2020
Messages
24
Programming Experience
Beginner
Hi Guys.
I have a very simple question. I am basically a Microcontroller Programmer using C language. In this there is a Main Loop which always executes and we call different functions from here.
In C# Windows form which is the Main Loop which always get executes and do different functions. I have a very simple program, below is the picture of Main Program

1.png

if we see public Form1(), this loop is printing line only for one time, so i think it is not the main loop. right?
Kindly tell me about this.
I am writing a program which have different buttons and according to these buttons i have to execute/stop execute things.
Hopefully you guys are getting my point

Thanks
 
WinForms apps are event-driven. All you need to do is handle the Click event of each Button and do whatever it is you need to do. There's no "main loop" at the level of your code. The application effectively just sits there, waiting for the user to do something.
 
If we have a start button which Starts the process for one time and we want to continue that loop, can i use timer for this purpose?
which will timely call the routine which i want.
 
Yes you can. And if you wanted something to run continuous, you will need to implement your own loop into one of those events. But if that is the case, you should consider using a background worker to run your long running process. A backgroundworker can be started and stopped with some simple checks. Dig into the MSDN documentation for background worker and you will find some helpful code to get you started, as well as all the documentation to explain how it works. Generally, it's not a good idea to run long running tasks on the UI itself, as this will cause a deadlock in your applications UI.
 
Thank Sheeping for your reply. What do you mean by UI? UI means what i am writing code like using timer and it will keep running that code at particular time? OR something else
 
If you need to run code at a specific time, then use a timer. We use threads, tasks, and background workers to run long running processes.
 
Back
Top Bottom