خرید بک لینک

Vote count: 0

I have a c# winforms application with a tab control and several tabs. One of the tabs contains a data grid control - it only has about 10 elements in it but the data is populated by querying multiple servers and thus is slow to load.

When I run my application and select the tab with the datagrid control, the application appears to hang, while its trying to query all the servers and populate the grid.

Instead of hanging I'd like the application to be responsive and for it to display a "please wait..." message which will disappear after the datagrid is populated.

What I've tried to do is create a background worker as such:

        if (tabctrl.SelectedTab == tabctrl.TabPages["tabServices"])
        {

            this.dgrdServices.RowPrePaint += new DataGridViewRowPrePaintEventHandler(dgrdServices_RowPrePaint);
            this.dgrdServices.CellContentClick += new DataGridViewCellEventHandler(dgrdServices_CellClick);

            BackgroundWorker bw = new BackgroundWorker();
            lblLoading.Visible = true;
            bw.RunWorkerAsync();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);


        }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {

        PopulateServicesDataGrid();
    }

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        lblLoading.Visible = false;
    }

PopulateServicesDataGrid() contains code which iterates through some objects and queries several different servers for service status.

When I try and run the above though the grid doesn't get populated. If I don't use a background worker and just call PopulateServicesDataGrid directly it does work (albeit the app hangs).

Why isn't the background worker/datagrid populate working?

Thanks Brad

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 12 فروردين 1395 ساعت: 2:20

صفحه بندی