Rasheen Ruwisha
Rasheen Ruwisha

Reputation: 211

Open form as form.ShowDialog() and proceed wih function in parent form

I have an application where the users can add or delete products in a dashboard, because I connect it to azure sql the queries sometimes take time, so I need to block the ui in the main form, I tried disabling the currently open form. but that make the ui look bad.. I also tried form.ShowDialog() which made the main ui struck.. how can I block the main UI till the query is completed?

Upvotes: 1

Views: 280

Answers (1)

SH7
SH7

Reputation: 752

A sample "Please Wait" form with. Here there is two form 1. Main Form with a Button 2. Form Designed to show as "Please wait" message. The Form contains two control a. Label b. Progress Bar - the property style for the progress bar is made as Marquee

Sample Image

  1. PleaseWait.cs

    public partial class PleaseWait : Form
    {
        public Action Worker { get; set; }
        public PleaseWait(Action worker)
        {
            InitializeComponent();
    
            if(worker == null)
                throw new ArgumentNullException();
    
            Worker = worker;
        }
    
        protected override void OnLoad(EventArgs e)
        {
             base.OnLoad(e);
             Task.Factory.StartNew(Worker).ContinueWith(a=> 
             {
                 this.Close();
             }, TaskScheduler.FromCurrentSynchronizationContext());
        }
    }
    
  2. MainForm.cs . In button1_click you will initialize the PleaseWait Form as ShowDialog

    private void button1_Click(object sender, EventArgs e)
    {
        using (PleaseWait pw = new PleaseWait(MakeAzureSQLQuery))
        {
            pw.ShowDialog();
        }
    }
    
    private void MakeAzureSQLQuery()
    {
        //Making the Query Function
        //You can also use Background Thread for Querying
        for (int nCount = 0; nCount < 500; nCount++)
            Thread.Sleep(50);
    }
    

Upvotes: 3

Related Questions