C# threading between forms

prod

Executive Member
Joined
Nov 11, 2009
Messages
8,848
Reaction score
7,217
Location
Cape Town
Hey guru's :)

I want to show a busy dialog (Just a windows form called Busy.cs with some label text and a .gif animation) while my main form is busy processing data from a MySQL database.

Current code I use (don't laugh :P)

Code:
        private void main_Load(object sender, EventArgs e)
        {

            Thread ShowBusy = new Thread(new ThreadStart(ShowBusyThread));
            ShowBusy.Start();
            //bunch of processing to generate datagrids etc
            //I mostly use classes to handle these
         }
....

        private void ShowBusyThread()
        {
            Busy Busydialog = new Busy();
            Busydialog.Show();
            Thread.Sleep(2000);
        }

Currently it shows the busy dialog for a split second, which obviously tells me something is way wrong. As I can actually notice the main form is still unresponsive for a second or so after the busy dialog disappeared. I've looked at http://www.codeproject.com/KB/threads/winformsthreading.aspx for examples, but they mainly just send text through, I failed miserably with forms. I'm also a total C# noob and teaching myself :) So any help / tips are greatly appreciated. Thanks lads
 
Probably not a good way to do it but:

Code:
      private void main_Load(object sender, EventArgs e)
        {

            Thread ShowBusy = new Thread(new ThreadStart(ShowBusyThread));
            ShowBusy.Start();

            //bunch of processing to generate datagrids etc
            //I mostly use classes to handle these

	    ShowBusy.Abort()
         }
....

        private void ShowBusyThread()
        {
            Busy Busydialog = new Busy();
            Busydialog.Show();
        }
 
Thanks for the replies guys ! I'm testing it out a bit.
 
Maybe try making it more efficient with threads instead of slowing it down for so many milliseconds with Thread.sleep?

Don't you also need a try ... catch for database access? (C# not my language).
 
Last edited:
Don't you also need a try ... catch for database access? (C# not my language).

Yea, the database connection is handled in a class I wrote, so everything happens there, this was just about showing a separate window(dialog) to indicate that we are busy populating data.

@dequadin: It worked almost perfectly :) For some reason the .gif on the busy dialog doesn't animate though. Reading up a bit now
 
Yea, the database connection is handled in a class I wrote, so everything happens there, this was just about showing a separate window(dialog) to indicate that we are busy populating data.

@dequadin: It worked almost perfectly :) For some reason the .gif on the busy dialog doesn't animate though. Reading up a bit now
Maybe show us the DB query you are running, in case that is causing a delay.
 
dequadin has the right idea about having the work done in another thread instead of the GUI.

I would however recommend that you take a look at the BackgroundWorker Class because it was made for that purpose.

You can select the .NET version you are using from the Other Versions dropdown menu on the top of the page, and then scroll down to Remarks for some details about the class. There are are some examples just below that.
 
Thank you all :) I'm going on vacation tomorrow so app development will have to wait for a while. I'll try your suggestions as soon as I can. Thanks again, I appreciate it.
 
Top
Sign up to the MyBroadband newsletter
X