Zoltan
Zoltan

Reputation: 93

C# gif not loading -

I've a form that runs a query from a DataGridView on a button click. The below code works how I expect it to;

  1. Opens a form that says "Please Wait while query runs"
  2. Runs query
  3. Closes form when loaded and then fill the DataGridView with the data.

I have added a picturebox with simple gif - purely for the UI and the user to see it is working. But the gif doesn't actually spin - just shows as a picture. Testing it, if I run it on a form on its own it is fine, I can only hazard a guess that the query running is stopping it from working how it should.

        PleaseWaitForm pleaseWait = new PleaseWaitForm();
        try
        {
            pleaseWait.Show();
            Application.DoEvents();

            this.singleCenTableAdapter.Fill(this.singleCenData.SingleCenTable, ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
            int RowC = singleCenTableDataGridView.RowCount;
            pleaseWait.Close();
            if (RowC == 0)
            {
                MessageBox.Show(GlobVar.NoResults, "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            pleaseWait.Close();
        }
        catch (System.Exception exc)
        {
            GlobVar.vid.Open();
            var LogName = GlobVar.LoginName.ExecuteScalar();
            GlobVar.vid.Close();
            MessageBox.Show
                (
                "An error has occured " + LogNam + ". Please try again. \n\n" +
                exc.Message, "An error has occured", MessageBoxButtons.OK, MessageBoxIcon.Warning
                );
        }
        finally
        {
            pleaseWait.Close();
        }

The "Please Wait" form is just a label & picture so there is nothing but the Initalize in there at the minute ;

    public PleaseWaitForm()
    {
        InitializeComponent();
    }

Does anybody have any ideas on how to tackle this to make it work correct? Or anything particular that I am doing wrong? I know for the most part I might get a bit of stick for using Application.DoEvents() anyway, but any help is appreciated!

Upvotes: 0

Views: 289

Answers (2)

Rajendra
Rajendra

Reputation: 191

I read in your comment that you use .NET 4.0 and therefore can not use await and async with Task<>.

Before .NET 4.5, you can use Thread or BackgroundWorker to achieve this,
refer to the following example of BackgroundWorker:

Background Worker loading screen in Winforms

Your current code is running in synchronously, you are expecting it to work in asynchronously.

Upvotes: 2

PJvG
PJvG

Reputation: 1320

You need async and await to keep your UI responsive.

First make the method this code is in async as follows:

private async void Method()
{
    ...
}

Next place the following code in a new method:

this.singleCenTableAdapter.Fill(this.singleCenData.SingleCenTable, ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
int RowC = singleCenTableDataGridView.RowCount;

For example:

private async Task<int> FillAndGetRowCount( ... ) // add parameters if needed
{
    this.singleCenTableAdapter.Fill(this.singleCenData.SingleCenTable, ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
    return singleCenTableDataGridView.RowCount;
}

Finally, change your try block to:

try
{
    pleaseWait.Show();
    Application.DoEvents();

    int RowC = await FillAndGetRowCount( ... );
    pleaseWait.Close();
    if (RowC == 0)
    {
        MessageBox.Show(GlobVar.NoResults, "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
}

Upvotes: 0

Related Questions