0

I'm downloading a string from a webpage using System.Net.WebClient.DownloadStringTaskAsync asynchronously.

About 4 out of 5 times, this completes successfully, but occasionally, it hangs, and I will have to restart the program to "reset" it.

Thus I'm thinking of implementing a time-out that automatically cancels the Task after 5 seconds and restarts it:

async void DoTasks()
{
    string output;

    int timeout = 5000;
    WebClient client = new WebClient() { Encoding = Encoding.UTF8 };
    Task<string> task = client.DownloadStringTaskAsync(url);
    if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
    {
        output = task.Result;
    }
    else
    {
        client.CancelAsync();
        DoTasks();
    }
}

This, however, returns a NullReferenceException.

jacobz
  • 2,751
  • 11
  • 31
  • 55

1 Answers1

0

I'm not an expert on await/async, i'm still learning myself but maybe you should do something like this?

using System;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Inline code to test");
            Console.WriteLine(StartTask(true));

            Console.WriteLine("Get actual website to test, write the length of the text only");
            Console.WriteLine(StartTask(false).Length);

            Console.Write("Press any key");
            Console.ReadKey();
        }

        static string StartTask(bool DoTestInsteadOfGetUrl)
        {
            var task = DoTasks(DoTestInsteadOfGetUrl);
            Task.WaitAny(task);

            return task.Result;
        }

        async static Task<string> DoTasks(bool DoTestInsteadOfGetUrl)
        {
            int timeout = 20;
            int count = 1;
            CancellationTokenSource cts;
            string output;                
            do
            {
                cts = new CancellationTokenSource(timeout);
                if (DoTestInsteadOfGetUrl)
                {
                    output = await Task<string>.Run(() => test(count, cts.Token), cts.Token);
                }
                else
                {
                    WebClient client = new WebClient() { Encoding = Encoding.UTF8 };
                    output = await Task<string>.Run(() => client.DownloadStringTaskAsync("http://www.google.com"), cts.Token);
                }

                if (cts.IsCancellationRequested)
                {
                    Console.WriteLine("try #" + count++);
                    timeout += DoTestInsteadOfGetUrl ? 100 : 50;
                }
            } while (cts.IsCancellationRequested);

            return output;
        }

        async static Task<string> test(int count, CancellationToken ct)
        {
            int sleep = 400 - (count * 5);
            await Task.Run(() => Task.Delay(sleep), ct);

            if (!ct.IsCancellationRequested)
            {
                Console.WriteLine("succeed #" + count);
            }
            return sleep.ToString();
        }
    }
}
Fredou
  • 18,946
  • 9
  • 53
  • 107
  • There are many things wrong with this code. For starters, it doesn't include the OP's `WebClient` call, which you'll need to make inside `DoTasks`. But your `DoTasks` is `void`, and doesn't contain the `async` modifier. This currently won't solve the OP's problem. – Yuval Itzchakov Dec 25 '14 at 15:03
  • @YuvalItzchakov, I updated my code... hope it is better – Fredou Dec 25 '14 at 16:20