Search code examples
c#winformstime-waitdynamic-splash-screen

Show Wait Form for dynamic time until processing is done


I have searched in many threads of stackoverflow, Code Project, C# Corner and many other forums. My Question is cloned. But i can't any satisfactory solution. I want to show a wait form that will appear when there is some sort of background work . Back ground work may include opening a form , filling Grid View etc batching and processing commands.

private void newVendorBtn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            if (newVendor==null||newVendor.Text=="")
            {

              SplashScreenManager.ShowForm(this, typeof(WaitForm1), true, true, false);
                newVendor = new newVendorForm();
                newVendor.MdiParent = this;
                for (int i = 0; i <= 100; i++)
                {
                    SplashScreenManager.Default.SetWaitFormDescription(i.ToString() + "%");
                    Thread.Sleep(5);


                }
                SplashScreenManager.CloseForm(true);

                    newVendor.Show();
            }
            else if(CheckOpened(newVendor.Text))
            {
                newVendor.WindowState = FormWindowState.Normal;
                newVendor.Show();
                newVendor.Focus(); 
            }
            //newvendorBool = addPanel(ref newVendorDP, newVendor, ref newvendorBool, "New Vendor");

        }

Now here is question , how wait can form should be appear for dynamic time dependent upon machine processing. If application is run on Latest fast machine (i.e. i7, i5 ,others etc) wait form should appear fewer time where as if is on Pentium 3 ,4 It should appear for long time until processing is complete. I am Developing c# Winforms application.


Solution

  • Here's pseudo-code to show you an example of this concept. You can modify it for C#:

    newVendorBtn_ItemClick{
    
                    SplashScreenManager.ShowForm(this, typeof(WaitForm1), true, true, false);
                    newVendor = new newVendorForm();
                    newVendor.MdiParent = this;
    
                    //This triggers the newVendor_Load
                    newVendor.Show(); 
    
                    //newVendor is done loading, close the splash screen
                    SplashScreenManager.CloseForm(true);
    }
    
    newVendor_Load{
         //newVendor Form does some tasks when it's first loaded
         //these tasks will take different times to complete on different computers
         //as each is completed, the splash screen is updated.
    
         performLoadingTask1();
         SplashScreenManager.Default.SetWaitFormDescription("20% complete");
    
         performLoadingTask2(); 
         SplashScreenManager.Default.SetWaitFormDescription("40% complete");
    
         performLoadingTask3(); 
         SplashScreenManager.Default.SetWaitFormDescription("60% complete");
    
         performLoadingTask4(); 
         SplashScreenManager.Default.SetWaitFormDescription("80% complete");
    
         performLoadingTask5(); 
         SplashScreenManager.Default.SetWaitFormDescription("100% complete");
    
    }