This is a migrated thread and some comments may be shown as answers.

Async gridview and waiting bar

2 Answers 533 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 30 Nov 2015, 03:49 PM

Hi Folks,

I have an Async Task which uses a LINQ query to fetch rows of data.

public async Task<List<POMastObject>> FetchPOMastsAsync()
        {
            try
            {
                using (var db = new DBContext())
                {
                    return await (from p in db.POMasts.AsNoTracking()
                            join pr in db.Profiles.AsNoTracking() on p.ProfileID equals pr.ID
                            join c in db.CurrencyTypes.AsNoTracking() on p.CurrencyTypeID equals c.ID
                            join w in db.WHMasts.AsNoTracking() on p.WarehouseID equals w.ID
                            join t in db.TermCodeTypes.AsNoTracking() on p.TermCodeTypeID equals t.ID
                            join s in db.POMastStatusTypes.AsNoTracking() on p.StatusID equals s.ID

                            //Ensure that these are dynamic
                            where (_viewfetch.VendMastID == -1 || p.VendorID == _viewfetch.VendMastID) &&
                            (_viewfetch.POMastStatusID == -1 || p.StatusID == _viewfetch.POMastStatusID)

                            orderby p.ID

                            //Put the query results into the bespoke object
                            select new POMastObject
                            {
                                ID = p.ID,
                                OrderNo = p.OrderNo,
                                RaisedDate = p.RaisedDate,
                                RaisedBy = pr.Name,
                                Currency = c.Description,
                                Warehouse = w.Description,
                                Terms = t.Description,
                                LastEditedBy = p.LastEditedBy,
                                LastEditedDate = p.LastEditedDate,
                                Status = s.Name
                            }).ToListAsync();
                }
            }

Which is consumed on the form as follows:

private async void GetSearchResultVendorAsync()
        {
            try
            {              
                radGridViewResult.DataSource = await _poasync.FetchPOMastsAsync();
                radGroupBoxResult.FooterText = "Search Results (" + radGridViewResult.RowCount.ToString() + " Records)";
            }
            catch (Exception ex)
            {
                GlobalErrorHandler.CallingRoutine = MethodBase.GetCurrentMethod().Name;
                GlobalErrorHandler.CallingModule = typeof(frmSearchPO).ToString();
                GlobalErrorHandler.Exception = ex.Message.ToString();
                GlobalErrorHandler.HandleError();
            }
        }

I am trying to have a radWaitngBar wrapped around this to StartWaiting and StopWaiting but so far have been unable to get it to work

I tried the following:

private async void radButtonSearch_Click(object sender, EventArgs e)
        {
            try
            {
                InitMemberVars();
                radWaitingBar1.StartWaiting();

                await Task.Run(() =>
                {
                    GetSearchResultVendorAsync();
                });
                radWaitingBar1.StopWaiting();
            }

Which starts the animation but then falls over with a System.NullReferenceeException in Telerik,WinControls.dll

If I don't use radWaitingBar the grid is populated correctly, and you are still able to drag the radForm around the screen whilst the async data fetch is running.

Any ideas folks?

Thanks

Martin.

2 Answers, 1 is accepted

Sort by
0
Martin
Top achievements
Rank 1
answered on 30 Nov 2015, 04:35 PM

It was solved by altering the code (on the form) that consumes the Task. Thought it may be handy for anyone else doing something similar. The code goes into an async void routine (such as private async void radButtonSearch_Click):

radWaitingBarResult.Visible = true;
radWaitingBarResult.StartWaiting();

var result = await _poasync.FetchPOMastsAsync();
                if (!Equals(result,null))   //<<== This code will not execute till the await finishes
                    radGridViewResult.DataSource = result;

radWaitingBarResult.StopWaiting();
radWaitingBarResult.Visible = false;

0
Hristo
Telerik team
answered on 01 Dec 2015, 02:04 PM
Hello Martin,

Thank you for writing.

I am glad that you managed to find a solution which fits your scenario. Thank you also for sharing it with the community. In case you need additional information or further assistance just let me know.

Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Martin
Top achievements
Rank 1
Answers by
Martin
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or