joe bloe

joe bloe

  • NA
  • 10
  • 0

Synchronous WebClient

May 26 2009 8:24 PM
I have a calculator application that performs various download functions to get values for the calculations.  I paid someone to develop this for me and  the developer is now history.  Recently I discovered an error message when the application is run concurrently.  "WebClient does not support concurrent I/O operations."

I am not a developer, but I understand the gist of what's happening.  Either I need to use an asynchronous WebClient method or find a way to schedule the synchronous process better. 

I copied a snippet of the code from my .cs file.  Altogether there are 5 instances of the WebClient "client".  I copied one of those for space sake. 

My question is how would I code this asynchronously or is there a way to pause a synchronous instance of WebClient until previous instances are completed? I don't mind the delay, I just don't want the error message to pop up for the end user.
  Thanks to anyone who can help. 

Code Snippet:

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using proquest.Data;
using proquest.Entities;

public static WebClient client = new WebClient();

    #endregion

///<Comment>There is unrelated code here.  I skipped until next instance of WebClient///</Comment>

public static double TreasuryValue(RateType type)
    {
        string xmlurl = "http://www.ustreas.gov/offices/domestic-finance/debt-management/interest-rate/yield.xml";

        string filepath = HttpContext.Current.Server.MapPath("~/documents/yeild.xml");

        if (File.Exists(filepath) && hoursSinceLastUpdate(filepath) > 24)
        {
            try
            {
                client.DownloadFile(xmlurl, filepath);
            }
            catch
            {
            }
        }
        else if (!File.Exists(filepath))
        {
            try
            {
                client.DownloadFile(xmlurl, filepath);
            }
            catch
            {
            }
        }

Answers (6)