Question

I am attempting to use a Microsoft.SqlServer.Management.Smo.Restore object to restore a SQL Server 2000 database. Just before I begin the restore operation, I change the text of a label so that the user knows what is going on. However, the changed text isn't visible on the GUI (i.e., the text remains the same as it was) until AFTER the fullRestore.Wait() line.

lblStatus.Text = "Restoring Database";
Restore fullRestore = new Restore(); 

// Configure fullRestore

fullRestore.SqlRestore(_server);
fullRestore.Wait();

The weird thing is, lblStatus eventually does display "Restoring Database", but not until after the restore is complete. Any ideas?

Was it helpful?

Solution

You're blocking on the GUI thread, which is preventing it from updating. You can call lblStatus.Refresh(), or move your Wait to a background thread (which is ultimately the right thing to do).

OTHER TIPS

It's a threading issue. You can do this on seperate threads or on a background thread. One approach I've seen people use is do a Application.DoEvents() although I typically stay away from that call.

The update to the GUI cannot occur until you have finished processing on the foreground thread and released it. You need to do the restore on a background thread to allow the foreground thread to continue updating the GUI. Consider putting the restore code in a separate method and using ThreadPool.QueueUserWorkItem( ), and passing the restore method. This will trigger your restore method on a thread pool thread.

If you need more control over the thread and notification when it has finished, you can use a BackgroundWorker

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top