Hans Rudel
Hans Rudel

Reputation: 3611

Creating a thread inside a task

I have a method UploadReportNotifier() which is subscribed to an event, which is raised once i have finished uploading data to a database. In UploadReportNotifier() i want to reset some values on my GUI, ie progressbars etc, but i want there to be a time delay between doing this.

Is it possible to create a thread inside UploadReportNotifier() and call thread.Sleep, or is the timer() class more appropriate?

Thanks for your help.

Upvotes: 0

Views: 70

Answers (2)

Tudor
Tudor

Reputation: 62439

I see you have found your solution. I just want to mention that in this situation you should always choose timers over Thread + Sleep, which imo is just an ugly hack that should be avoided always.

Timers are great because they are also capable of executing their code on the GUI thread, so you don't need to use Invoke or BeginInvoke. For example System.Windows.Forms.Timer for WinForms or System.Windows.Threading.DispatcherTimer for WPF.

Upvotes: 1

ken2k
ken2k

Reputation: 48985

If your goal is to wait a few amount of time after the execution of UploadReportNotifier before updating all your GUI controls, then a timer would be a good solution IMO.

In your UploadReportNotifier method, you can create and start a timer so your "update my GUI" code will be executed after a few time. Don't forget to stop/dispose your timer after your GUI update as you probably don't want it to be executed multiple time.

You could use the System.Windows.Forms.Timer timer class (as you may know, there are multiple timer classes available). This one is not the most accurate one, but it executes the code in the UI thread, so you won't deal with cross-thread exceptions when modiying your UI.

Upvotes: 2

Related Questions