Click here to Skip to main content
15,606,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Scenario : a process write a textfile and in meantime another with the progress bar show current situation. At the end a messagebox show Done

What's wrong ? The code doesn't show the progress situation, and only at the end
I can see progress bar at 100% and the Messagebox.

Why this problem ?

This is my code

What I have tried:

<Button Command="{Binding MyCommand2}" Content="Click Me" Margin="10,10,670,321"/>
 <ProgressBar HorizontalAlignment="Left" Height="17" Margin="172,200,0,0" VerticalAlignment="Top" Width="421"
           Maximum="20" Value="{Binding Prog_bar_value}"/>

public int ncount = 0;

        private int _prog_bar_value;
        public int Prog_bar_value
            get { return _prog_bar_value; }
                _prog_bar_value = value;

private async void startButton_Click()
    await WriteTextFile(); // The 2 methods must be separated.
    await Progress();

    MessageBox.Show("Done");  //here we're on the UI thread.

async Task Progress()
    await Task.Run(() =>
        Prog_bar_value = ncount;

async Task WriteTextFile()
    await Task.Run(Exec_WriteTextFile);

void Exec_WriteTextFile()
    using (StreamWriter sw = File.CreateText(@"C:\temp\Abc.txt"))
        for (int i = 0; i < 20; i++)
            sw.WriteLine("Values {0} Time {1} ", i.ToString(),
            ncount = i;
            Thread.Sleep(500); //this is only for create a short pause

Thanks in advance
Comments 19-Feb-22 11:56am    
This should help you
antobaro 20-Feb-22 15:06pm    
Thank you ! ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900