Question

My main form has one button. When the button is clicked, I populate a grid. Here is my button click routine

private void btnSearch_Click(object sender, EventArgs e)
{
    if (chkExcludeCallCust.Checked)
    {
        if (chkEnable.Checked)
            RangeExclude = 1;
        else
            RangeExclude = 0;
    }

    new Thread(() =>
    {
        lock (thisLock)
        {
            Search();
        }
    }).Start();

}

A thread is called when the user clicks on the search button. From that thread I fetch data from the database and populate the grid.

So here is my search routine:

private void Search()
{
    DataSet ds = null;

    if (wfrm.InvokeRequired)
    {
        wfrm.Invoke(new MethodInvoker(delegate
        {
            wfrm.Show();
            wfrm.Refresh();
            Application.DoEvents();
        }));
    }
    else
    {
        wfrm.Show();
        wfrm.Refresh();
        Application.DoEvents();
    }

    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(delegate { 
            this.Cursor = Cursors.WaitCursor;
            btnSearch.Enabled = false;
        }));
    }
    else
    {
        this.Cursor = Cursors.WaitCursor;
        btnSearch.Enabled = false;
    }

    if (!PingTest())
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new MethodInvoker(delegate
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show("VPN disconnected. Please connect and try again.", "DebtorCallerAssistance");
                return;
            }));
        }
        else
        {
            this.Cursor = Cursors.Default;
            MessageBox.Show("VPN disconnected. Please connect and try again.", "DebtorCallerAssistance");
            return;
        }
    }

    if (chkExcludeCallCust.InvokeRequired)
    {
        chkExcludeCallCust.Invoke(new MethodInvoker(delegate
        {
            DataLayer.SetConnectionString(_country);
            ds = LoadData(dtfrom.Value.ToString("yyyyMMdd"), dtto.Value.ToString("yyyyMMdd"), (chkExcludeCallCust.Checked ? 1 : 0), txtSearch.Text, 1, Pager.PageSize, false);
        }));
    }
    else
    {
        DataLayer.SetConnectionString(_country);
        ds = LoadData(dtfrom.Value.ToString("yyyyMMdd"), dtto.Value.ToString("yyyyMMdd"), (chkExcludeCallCust.Checked ? 1 : 0), txtSearch.Text, 1, Pager.PageSize, false);
    }

    if (ds.Tables.Count > 0)
    {
        if (ds.Tables[0] != null)
        {
            dtExport = ds.Tables[0];
        }
    }

    if (outlookGrid1.InvokeRequired)
    {
        outlookGrid1.Invoke(new MethodInvoker(delegate
        {
            outlookGrid1.BindData(ds, "data");
            View = "BoundInvoices";
            DataGridViewCellEventArgs evt = new DataGridViewCellEventArgs(2, -1);
            object sender = null;
            //outlookGrid1_CellClick(sender, evt);
        }));
    }
    else
    {
        //outlookGrid1.DataSource = ds.Tables[0];
        outlookGrid1.BindData(ds, "data");
        View = "BoundInvoices";
        DataGridViewCellEventArgs evt = new DataGridViewCellEventArgs(2, -1);
        object sender = null;
        //outlookGrid1_CellClick(sender, evt);
    }

    if (wfrm.InvokeRequired)
    {
        wfrm.Invoke(new MethodInvoker(delegate
        {
            wfrm.Hide();
        }));
    }
    else
    {
        wfrm.Hide();
    }

    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(delegate { 
        this.Cursor = Cursors.Default;
        btnSearch.Enabled = true;
        }));
    }
    else
    {
        btnSearch.Enabled = true;
        this.Cursor = Cursors.Default;
    }
}

At form load, I instantiate a borderless windows like:

WaitForm wfrm = null;
public Feedback(string country)
{
    InitializeComponent();
    wfrm = new WaitForm();
}

I also set a few more properties for this window like:

startposition = CenterScreen
showinicon=false
showintaskbar=false
formboderstyle=none

WaitForm is shown only the first time I click the search button I am not able to understand why WaitForm is not shown if the search button is clicked a second time.

Another issue is that there is a PictureBox on WaitForm which has been assigned an animated GIF. The first time WaitForm is shown the animation is not playing. Why animation isn't the animation playing when I show the WinForm?

Please tell me what I need to change in code in order to show the WaitForm whenever I click on the search button. I'd also like to know how I can make the GIF in WaitForm's PictureBox animate.

Was it helpful?

Solution

It is total nonsense to start a thread and invoke everything back to the GUI thread. And DoEvents is a indicator for code smell. Bundle your invokes as invokes create a large Overhead

Change your code to something like this:

   private void btnSearch_Click(object sender, EventArgs e)
    {
        this.btnSearch.Enabled = false;
        this.Cursor = Cursors.WaitCursor;
        this.wfrm.Show();

        Thread t = new Thread(this.Search);
        t.Start();
    }

    private void Search()
    {
        while (isWorking)
        {
            DoHeavyWork();
            this.Invoke(new Action(ReportToWaitForm);
        }

        this.Invoke(new Action(() =>
            {
                this.btnSearch.Enabled = true;
                this.Cursor = Cursors.Default;
                this.wfrm.Hide();
            }));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top