Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know that it isn't really the answer on the question about background worker but maybe it's time to use "new" C# 5.0 feauture called <strong>async/await</strong>? I made some modifications in your code and tested it and all works without errors and freezes:</p> <pre><code> private bool cancelled; private bool started; public MainWindow() { InitializeComponent(); } private async void btnStart_Click(object sender, RoutedEventArgs e) { if (started) return; cancelled = false; started = true; try { await DoAsync(); } catch (Exception exception) { lblProgress.Content = "Error: " + exception.Message; return; } finally { started = false; } lblProgress.Content = cancelled ? "Cancel !" : "Finish !"; } private async Task DoAsync() { using (var sr = new StreamReader("C:\\File 1.txt")) { using (var sw = new StreamWriter("C:\\Out-File 1.txt")) { var fi = new FileInfo("C:\\File 1.txt"); long total = (char) fi.Length; int i = 0; string result; while (!sr.EndOfStream) { result = await sr.ReadLineAsync(); await sw.WriteLineAsync(result); i = i + (char) result.Length; ProgressChanged((int) ((decimal) i/(decimal) total*(decimal) 100)); if (cancelled) return; } } } } private void ProgressChanged(int progress) { this.lblProgress.Content = progress.ToString() + "%"; this.pb.Value = progress; } private void btnCancel_Click(object sender, RoutedEventArgs e) { cancelled = true; } </code></pre> <p>Also my advice about percent calculation is to check if it's changed in each iteration and then rise interface updates.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload