2

So I have this Progress Bar that I'm changing the value of after each sleep. Is there any ways that the Progress Bar visually updates live rather than just sitting at 0% and jumping to 100% when the execution is over?

private Client getConnectionPDU()

    {

        // Client à retourné
        PrimS.Telnet.Client client = null;

        // Ouverture de la connection
        try
        {
            // --- Ouverture de la connexion
            client = new Client(rackToIP[selectedRack], addrPort, new System.Threading.CancellationToken());
            console.Text +=  client.Read();
            Thread.Sleep(sleep);
            progressBar.Value = 20;

            // --- Username
            client.WriteLine(username + enter);
            Thread.Sleep(sleep);
            progressBar.Value = 40;

            console.Text += "\n" + client.Read();
            Thread.Sleep(sleep);
            progressBar.Value = 60;

            // --- Password
            client.WriteLine(password + enter);
            Thread.Sleep(sleep);
            progressBar.Value = 80;
            console.Text += "\n" + client.Read();
            Thread.Sleep(sleep);
            progressBar.Value = 100; 

        }
        catch (Exception e)
        {
            console.Text += "\n" + e.Message;
            client = null;
        }

        // Scroll to end
        scroller.ScrollToEnd();

        return client;
    }
  • Your code is blocking the UI thread and not giving it a chance to actually draw the progress bar at its new size. Consider using `Async` / `Await` to avoid that problem. Replace `Thread.Sleep` with `await Task.Delay`. You would also have to make `getConnectionPDU` return a `Task`, and mark it as `async`. This might go several levels deep depending on what calls this function. – Bradley Uffner Jun 14 '16 at 18:38
  • Possible duplicate of [How to correctly implement a BackgroundWorker with ProgressBar updates?](http://stackoverflow.com/questions/19334583/how-to-correctly-implement-a-backgroundworker-with-progressbar-updates) – Stewbob Jun 14 '16 at 18:49
  • I'd say it isn't a duplicate of that question, since he isn't specifically asking about a background worker, and it can be solved many different ways. – Bradley Uffner Jun 14 '16 at 18:50

2 Answers2

1

Your code is blocking the UI thread, never giving it a chance to redraw the progress bar at its new size until the function returns. You should consider using async / await to avoid the blocking.

You will need to make a few changes for this to work:

  • Change all theThread.Sleep calls to await Task.Delay.
  • Change getConnectionPDU to return a Task<Client>.
  • Mark getConnectionPDU as async.

Assuming your code gets called from a button click, the result would look something like this (not tested, may contain errors):

private async void Button1_click(object sender, EventArgs e)
{
    var TheClient = await getConnectionPDU();
}

private async Task<Client> getConnectionPDU()
{

    // Client à retourné
    PrimS.Telnet.Client client = null;

    // Ouverture de la connection
    try
    {
        // --- Ouverture de la connexion
        client = new Client(rackToIP[selectedRack], addrPort, new System.Threading.CancellationToken());
        console.Text +=  client.Read();
        await Task.Delay(sleep);
        progressBar.Value = 20;

        // --- Username
        client.WriteLine(username + enter);
        await Task.Delay(sleep);
        progressBar.Value = 40;

        console.Text += "\n" + client.Read();
        await Task.Delay(sleep);
        progressBar.Value = 60;

        // --- Password
        client.WriteLine(password + enter);
        await Task.Delay(sleep);
        progressBar.Value = 80;
        console.Text += "\n" + client.Read();
        await Task.Delay(sleep);
        progressBar.Value = 100; 

    }
    catch (Exception e)
    {
        console.Text += "\n" + e.Message;
        client = null;
    }

    // Scroll to end
    scroller.ScrollToEnd();

    return client;
}
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
1

I would recommend using a backgroundWorker thread. Here is a similar question for what you are trying to do: How to correctly implement a BackgroundWorker with ProgressBar updates?

Community
  • 1
  • 1
Russell
  • 29
  • 4