1

So I made a software that can download strings from url. Here is my code:

private void button1_Click(object sender, EventArgs e)
        {

    if (radioButton1.Checked)
                {

                    var accounts = new WebClient().DownloadString("https://pastebin.pl/view/raw/sample").Split('\n');
                    textBox1.Text = accounts[new Random().Next(0, accounts.Length)];
                }

How can I make custom progress bar made by text, such that only text will show for progressing the download? example:

When the download is 10% I want to put on text progressbar (Status: requesting database)

when it comes up to 50% I want to put on text progressbar (Status: getting information)

when it comes 100% (Status: completed)

My Full Code

My Ui

Gracia
  • 13
  • 6
  • I think you should use an `async` method for that, see: https://stackoverflow.com/questions/13240915/converting-a-webclient-method-to-async-await – Luuk Jan 12 '20 at 10:59
  • i dont understand that you provide example, can you please make a example code including my code? i'm newbie in c# – Gracia Jan 12 '20 at 11:11
  • See example given in the answer by @divyang4481, it only needs some changes to do what you wish to do, SO is not a writing code service, you have to do some things yourself! – Luuk Jan 12 '20 at 13:40
  • yeah his code working fine 100% im newbie for c# sorry for asking too much – Gracia Jan 12 '20 at 13:52

2 Answers2

1

you should use async method to download

   private void button1_Click(object sender, EventArgs e)
    {
        if (radioButton1.Checked)
        {
            Download(new Uri("https://pastebin.pl/view/raw/sample"));
        }
        else if (radioButton2.Checked)
        {
            Download(new Uri("https://pastebin.pl/view/raw/sample2"));
        }
        else if (radioButton3.Checked)
        {
            Download(new Uri("https://pastebin.pl/view/raw/sample4"));
        }
        else
        {
            MessageBox.Show(this, "select radio btn");
        }
    }

    private void  Download(Uri uri)
    {
        Thread thread = new Thread(() =>
        {
            WebClient client = new WebClient();
            client.DownloadProgressChanged += Client_DownloadProgressChanged1;
            client.DownloadStringCompleted += Client_DownloadStringCompleted;
            client.DownloadStringAsync(uri);
        });
        thread.Start();
    }

    private void Client_DownloadProgressChanged1(object sender, DownloadProgressChangedEventArgs e)
    {
        this.BeginInvoke((MethodInvoker)delegate {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;
            if(percentage >=10 && percentage <50)
            { 
              textBox1.Text ="message for 10%";   
            }
            else if if(percentage >=50 && percentage <100)
            {
                    textBox1.Text ="message for 50%";
            }
            else
            {
              textBox1.Text ="completed";
            }
            // you can use to show to calculated % of download
        });
    }



    private  void Client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {


        BeginInvoke((MethodInvoker)delegate
        {
            var accounts = e.Result.Split('\n');
            textBox1.Text = accounts[new Random().Next(0,accounts.Length)];
        });
    }
divyang4481
  • 1,584
  • 16
  • 32
  • in this method, my pattern is broken, my pattern is get a string from url 1 by 1 and randomize, not all string that I want to get, can you fix your method can you include my pattern – Gracia Jan 12 '20 at 12:32
  • This doesn’t answer the OP’s question- namely how do you print out the progress of the download – auburg Jan 12 '20 at 12:34
  • i want to do is download strings from url 1 by 1 and random strings then when downloading the 1 string from url in put in textbox1 in label1 it should my status whether is completed or downloading – Gracia Jan 12 '20 at 12:35
  • you should provide your sample data that you are downloading – divyang4481 Jan 12 '20 at 12:43
  • i provide it now, recheck my post i have sample pic – Gracia Jan 12 '20 at 12:47
  • your code is 100% work now, i have one more question how can i control the DownloadProgress, instead of totalbytes i want to do is text status like logs Example: when comes to 10% in progress my text status will be Status: blahblahblah instead totalbytes or percentage when it comes up to 50% it will be Status: blahblahblah when it comes to 100% it will be Status: blahblahblah – Gracia Jan 12 '20 at 13:48
  • in that code i have commentted to calculate percentage value where you can place logic about if 10% then something blalball.. – divyang4481 Jan 12 '20 at 13:58
  • but why i need progressBar toolbox? i just want is calculate of percentage of downloading and put som logic – Gracia Jan 12 '20 at 14:11
  • no you do not need progressbar. you update you textbox there – divyang4481 Jan 12 '20 at 14:12
  • sorry i don't really get it, im really newbie in coding i can't put a logic too, this is the last time that i need your help, can you please provide an example, i'm sorry for too much asking for your help. – Gracia Jan 12 '20 at 14:17
0

WebClient has been deprecated in favor of HttpClient, but here goes.

You should use Progress<T> to report back progress.

You can define an extension method like:

public static class WebClientExtensions
{
    public static async Task<string> DownloadStringTaskAsync(
        this WebClient webClient,
        string address,
        IProgress<int> progress)
    {
        try
        {
            webClient.DownloadProgressChanged += downloadProgressChanged;
            return await webClient.DownloadStringTaskAsync(address);
        }
        finally
        {
            webClient.DownloadProgressChanged -= downloadProgressChanged;
        }

        void downloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progress.Report(e.ProgressPercentage);
        }
    }
}

and use it like this:

private async void button1_Click(object sender, EventArgs e)
{
    if (radioButton1.Checked)
    {
        var text = await new WebClient()
            .DownloadStringTaskAsync(
                "https://pastebin.pl/view/raw/sample",
                new Progress<int>(p => /* report progress */));
        var accounts = text.Split('\n');
        textBox1.Text = accounts[new Random().Next(0, accounts.Length)];
    }
}
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59