Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8129359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:17:03+00:00 2026-06-06T08:17:03+00:00

Per a client’s request I have written a communication class that inherits from webclient.

  • 0

Per a client’s request I have written a communication class that inherits from webclient. They want to be able to “Pass in a custom windows form as a progress bar” rather than that I created an interface that implements 3 properties. Anyway the problem I am having is the app starts and i click the start button so to speak and the first time i do this the ui thread is frozen, after a few seconds it unfreezes and the progress bar and data begin coming down.

After this initial freeze though , any subsequent presses of the start button work perfectly and do not block the thread any ideas?

Here are the relevant pieces of code from form 1

private void btnSubmit_Click(object sender, EventArgs e)
    {
        txtResponse.Text = "";
        progressForm = new ProgressFormTest();
        myCommunication = new CommunicationClient(progressForm);
        myCommunication.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_RequestComplete);
        // myCommunication.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
        myCommunication.Timeout += new EventHandler(wc_TimeOut);
        myCommunication.Cancelled += new EventHandler(myCommunication_Cancelled);


        progressForm.Show();
        myCommunication.TimeoutValue = (int)numConnectionTimeout.Value * 1000;

        myCommunication.DownloadStringAsync(new Uri(txtUrl.Text));


    }

Here is the communication class

    public class CommunicationClient:WebClient
    {
    private int step = 1000;
    private long bytesReceived;
    private long bytesSent;
    private Status status;
    private System.Timers.Timer _timer;
    private IProgress myProgressForm;

    /// <summary>
    /// Sets the timeout value in milliseconds
    /// </summary>
    public int TimeoutValue { get; set; }
    public int TimeElapsed { get; set; }
    public long BytesReceived
    {
        get
        {
            return bytesReceived;
        }
    }
    public long BytesSent
    {
        get
        {
            return bytesSent;
        }
    }

    public event EventHandler Timeout;
    public event EventHandler Cancelled;

    public CommunicationClient(IProgress progressForm)
    {
        myProgressForm = progressForm;
        _timer = new System.Timers.Timer(step);
        _timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    }





    protected override void OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
    {
        _timer.Stop();
        if (status == Status.Completed)
        {
            myProgressForm.PercentComplete = 100;
            base.OnDownloadStringCompleted(e);
        }
    }





    protected override void OnDownloadProgressChanged(DownloadProgressChangedEventArgs e)
    {
        bytesReceived = e.BytesReceived;
        myProgressForm.BytesReceived = bytesReceived;
        if (e.TotalBytesToReceive == -1)
            myProgressForm.PercentComplete = calculateFakePercentage();
        else
            myProgressForm.PercentComplete = e.ProgressPercentage;
        base.OnDownloadProgressChanged(e);
    }

    protected virtual void OnTimeout(EventArgs e)
    {
        if (Timeout != null)
        {
            CancelAsync();
            this.Dispose();
            Timeout(this, e);
        }
    }

    protected virtual void OnCancelled(EventArgs e)
    {
        if (Cancelled != null)
        {
            this.Dispose();
            Cancelled(this, e);
        }
    }

    /// <summary>
    /// Cancels a pending asynchronous operation and raises the Cancelled Event
    /// </summary>
    /// <param name="Event">Set to true to raise the event</param>
    public void  CancelAsync(bool Event)
    {
        CancelAsync();
        if (Event)
        {
            status = Status.Cancelled;
            OnCancelled(new EventArgs());
        }
    }

    private void initialize()
    {
        status = Status.Completed;
        TimeElapsed = 0;
        _timer.Start();
    }



    new public void DownloadStringAsync(Uri url)
    {
        initialize();
        base.DownloadStringAsync(url);
    }

    private void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        TimeElapsed += step;

        if (TimeElapsed >= TimeoutValue)
        {
            _timer.Stop();
            status = Status.Timeout;
            OnTimeout(e);
        }
    }    

    //used when the website in question doesnt provide the content length
    private int calculateFakePercentage()
    {
        return  (int)bytesReceived * 100 / 60000 ;
    }        
}

And here is the simple Interface IProgressForm

public interface IProgress
{
    int PercentComplete
    {
        get;
        set;
    }
    long BytesReceived
    {
        get;
        set;
    }
    long BytesSent
    {
        get;
        set;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:17:04+00:00Added an answer on June 6, 2026 at 8:17 am

    Fixed it , I set the Proxy property from the webclient class to null and that seemed to fix it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server that receives various xml messages from clients (one thread per
I want to add time in my textfield and for that as per client's
As per client requirement i want to send crash Report when App get crash.
Per a great answer from another question I have begun mounting global resources (css/js/images)
Given: 1 database per client (business customer) 5000 clients Clients have between 2 to
I'm working on SaaS application that uses the one DB per client model. It
Here's my trouble: I have a udp class that allow me to send and
I have an application that has multiple releases for different clients. For each client
i have a list of billable services per client and i'm trying to build
I have a bit of a unique challenge today. I have a client that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.