0

When I use DownloadFileAsync, it seems to 'block' something. I need the program to be able to download more strings while the file is downloading (downloading a file, but user is still able to search a directory for links to download more files).

The UI is not 100% being blocked, but when the user clicks the 'search' button it doesn't work properly, nor do clicks in the DataGridView get handled. The search button however clears the DataGridView as programmed, but the await thing that I wrote to download the directory as a string (asynchronously with DownloadStringTaskAsync) does not work. However, when the download finishes, the search finally goes through and then populates the DataGridView, which seems like very abnormal behavior to me.

When I comment out the DownloadFileAsync, everything is able to perform normally again. I have also tried to comment out the event handlers that I have put in place, but this also does not fix the issue. I am not sure, thanks for any help.

Some code snippets:

Downloading the file:

var bmclient = new WebClient();
bmclient.DownloadFileAsync(new Uri(downloadURL), Path.Combine(Application.StartupPath, originalFileName + ".nexd"));
bmclient.DownloadProgressChanged += (o, e) =>
        {
            int rowIndex = -1;
            DataGridViewRow row = form1.dataGridView2.Rows
                .Cast<DataGridViewRow>()
                .Where(r => r.Cells[0].Value.ToString().Equals(setID))
                .First();
            rowIndex = row.Index;
            MethodInvoker action = () => form1.dataGridView2[2, rowIndex].Value = e.ProgressPercentage.ToString() + "%";
            form1.BeginInvoke(action);
        };

Searching the directory, which is being called by a button on the main form:

public static async Task<string> GetBloodcatSearch(string query)
    {
        var return_data = string.Empty;

        try
        {
            using (var client = new WebClient())
            {
                return return_data = await client.DownloadStringTaskAsync(new Uri("directory/" + query));
            }
        }
        catch (Exception e)
        {
            return null;
        }
    }
Irshad
  • 3,071
  • 5
  • 30
  • 51
desu
  • 113
  • 7
  • 2
    Can you show some code so we have an idea of how you're using DownloadFileAsync, or perhaps how your search works? – maxton Dec 04 '15 at 05:08
  • @Nicholas Tay Please show the code – Noxious Reptile Dec 04 '15 at 05:09
  • 1
    the problem is on the 5th line of `DownloadFileAsync`, did you see it? – Sten Petrov Dec 04 '15 at 05:12
  • I've added some code. (@StenPetrov 5th line of what, I'm not sure what you're referring to.) – desu Dec 04 '15 at 05:15
  • Seems issue with switching back to context so you can use possibly use ConfigureAwait(false) in possible async method. – Akash KC Dec 04 '15 at 05:33
  • 2
    Without a good [mcve] it's impossible to say for sure what your specific problem might be. In the meantime, I will suggest that you subscribe to the `DownloadProgressChanged` event _before_ you start the operation. Also, you need to make sure _all_ access of UI objects occurs in the UI thread; your `DownloadProgressChanged` handler uses e.g. `form1.dataGridView2`, which looks like a UI object to me. Probably that entire method body should be in the invoked anonymous method. – Peter Duniho Dec 04 '15 at 05:34
  • There's a chance your progress event is firing too often, limit calling the UI only if the previous percentage was different from the one you want to update with. Keep that in a local var, don't ask the UI to tell you what is it showing because you'd still be locking it – Sten Petrov Dec 04 '15 at 08:42

0 Answers0