Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I using below code to fetching data and i showing a window with a short message:"please wait..."

in main window:

C#
private static frmLoading _loading = null;

        private void StartLoading(bool value)
        {
            if (value == true)
            {
                _loading = new frmLoading();
                _loading.Show(this);
            }
            else
            {
                CloseLoading();
            }
        }

        public static void CloseLoading()
        {
            if (_loading != null)
            {
                _loading.CloseLoading();
            }
        }


in button click event (main window):


XML
Thread t = new Thread(new ThreadStart(delegate
            {
                this.Invoke(new Action<bool>(StartLoading), true);

                this.Invoke((MethodInvoker)delegate { GetData(); });

                this.Invoke(new Action<bool>(StartLoading), false);

            }));
            t.Start();


in frmLoading:


C#
public void CloseLoading()
        {
            Invoke((MethodInvoker)delegate
            {
                this.Close();
            });
        }


but my problem is: i click on button cod is working and frmLoading is show but when i click on main form, application hanging and hang is continue until data fetching is complete.
and when i using ShowDialog() method (in this line: _loading.Show(this)) No Data Fetching!
what i do?!

another question, is this solution efficient?

thanks
Posted
Updated 29-Aug-14 3:39am
v3

When you use this.Invoke to call GetData, you are forcing GetData to execute on the UI thread. This means that any interaction with the main window or the dialog window will have to wait for GetData to finish executing.

Try changing your button click event to:
C#
Thread t = new Thread(new ThreadStart(delegate
{
   this.Invoke(new Action<bool>(StartLoading), true);
 
   GetData();
 
   this.Invoke(new Action<bool>(StartLoading), false);
 
}));
t.Start();


ShowDialog is a blocking call, which means it will show the window and wait for the user to close the window before continuing with the next statement. This means that GetData will only be called after you close the window.


Edit:
To prevent the user from messing with the main window, you could also kick off GetData on a background thread just before showing frmLoading as a dialog. This will block the UI thread while frmLoading is open, so you will have to programmatically close it just before updating your datagrid.
C#
void button_Click(object sender, EventArgs e)
{
   new Action(GetData).BeginInvoke(null, null);
   this.Invoke(new Action<bool>(StartLoading), true);
}

void GetData()
{
   // Fetch your data from database or wherever...

   // Do any processing you want to do on your data

   this.Invoke(new Action<bool>(StartLoading), false);
   this.Invoke(new Action(FillDataGrid));
}

void FillDataGrid()
{
   // Fill your datagrid
}


Links you might find useful:
http://stackoverflow.com/questions/1130208/how-to-disable-the-parent-form-when-a-child-form-is-active[^]
http://support.microsoft.com/kb/318607[^]
 
Share this answer
 
v2
Comments
maysamfth 29-Aug-14 10:24am    
thanks, but i using this.Invoke because this error recieved: Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on.

my question in better form is: how i can when a Please wait window is showing the main window be unaccessible?
BotCar 29-Aug-14 11:23am    
Then you need to use this.Invoke when updating your datagrid.

To prevent the user from messing with the main window, you could also kick off GetData on a background thread just before showing frmLoading as a dialog. This will block the UI thread while frmLoading is open, so you will have to programmatically close it just before updating your datagrid.

I updated my answer to show this.
Use a BackgroundWorker to fetch your data. This will allow your UI to be notified of the fetch progress and completion, and will also allow the fetch to be cancelled by the user.

See this[^] article as an example.

/ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900