I'm new to C# so please bear with me. I have a form with a text box and a couple of buttons.
The buttons are used to call some long running methods in a class. I would like to update the text box with some status information regarding the progress or failure of the method. What are some of the preferred best practices to accomplish this and keeping the code for the form isolated from the class code? I've looked at callbacks/delegates, etc, but I need a real simple example to get the gist of how this works.
The BackgroundWorker class is usually what most people will suggest for dealing with tasks such as this. create one and handle three important events:
(From MSDN) DoWork - Occurs when RunWorkerAsync is called. ProgressChanged - Occurs when ReportProgress is called. RunWorkerCompleted - Occurs when the background operation has completed, has been canceled, or has raised an exception.
So, you can do your work in the handler for the DoWork event. When you want to send a message to the UI, call the ReportProgress() method. This will raise the ProgressChanged event, and in the handler for that event you may update the UI without worry of a corss-thread exception. Handle the RunWorkerCompleted event if you need to do anything after the method completes.