async await timeut

Andy75

Member
Joined
May 29, 2020
Messages
18
Programming Experience
1-3
Hi all,
I have a method which calls a process sometimes longer than usual.
Here code:

C#:
 private async void LoadAll()
        {
            try
            {
                await Task.Run(() =>
                {
                    details = new Docs().LoadProduct();
                    // process very slow, about 25/30 seconds
                });

                foreach (var d in details)
                {
                   // at this point, details is null
                }
            }
            catch (Exception ex)
            {
                // Error shows timeout error
                MessageBox.Show("ERROR: " + ex.Message);
            }
            
        }


Development side works fast and the problem is not there. On the production side, being the largest db, it takes a few more seconds and an error comes out.
The method times out and I find null. DB is SQL SERVER in LAN.
How can I do?
Thanks
A.
 
The normal approach is to use a cancellation token. But your LoadProduct() would also need to know about the cancellation token and check it periodically.

Update: Forgot to put in the documentation link:
 
Last edited:
Solution
Can't work out if you're asking "how do I make `await ...` time out?" or "what do i do if I get a timeout execption while awaiting an async operation" ?
 
Back
Top Bottom