Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, add another class</p> <pre><code>class BackupTask { public string MachineName { get; set; } public string Username { get; set; } public string Password { get; set; } public string CurrentFile { get; set; } public int OverallProgress { get; set; } } </code></pre> <p>Then</p> <pre><code>private List&lt;BackupTask&gt; task_definitions = new List&lt;BackupTask&gt;(); //this is needed to parallel foreach private List&lt;BackupTask&gt; tasks = new List&lt;BackupTask&gt;(); //this is what you will use to report progress of your copying </code></pre> <p>and suppose your backgroundworker is named 'bw`</p> <p><code>private BackgroundWorker bw;</code></p> <p>Add this to your XAML file (before main Grid)</p> <pre><code>&lt;Window.Resources&gt; &lt;DataTemplate x:Key="BackupTask"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding MachineName}" Margin="2,4,2,4"/&gt; &lt;TextBlock Text="{Binding Username}" Margin="2,4,2,4" /&gt; &lt;TextBlock Text="{Binding Password}" Margin="2,4,2,4"/&gt; &lt;TextBlock Text="{Binding CurrentFile}" Margin="2,4,2,4"/&gt; &lt;TextBlock Text="{Binding OverallProgress}" Margin="2,4,2,4"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; </code></pre> <p>in your listview set <code>ItemTemplate="{StaticResource ResourceKey=BackupTask}</code></p> <p>So in the window constructor</p> <pre><code>bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); </code></pre> <p>Your buttons</p> <pre><code>private void btn_AddToQueue_Click(object sender, RoutedEventArgs e) { BackupTask bt = new BackupTask(); bt.MachineName = //load these bt.Username = // from your bt.Password = //textboxes bt.CurrentFile = ""; bt.OverallProgress = 0; AddOrModifyTask(bt); listView1.ItemsSource = tasks; } private void btn_StartBackup_Click(object sender, RoutedEventArgs e) { if (!bw.IsBusy) bw.RunWorkerAsync(); } </code></pre> <p>Of cource you will also need</p> <pre><code>void AddOrModifyTask(BackupTask t) { bool found = false; foreach (BackupTask bt in tasks) { if (bt.MachineName == t.MachineName) { found = true; bt.CurrentFile = t.CurrentFile; bt.OverallProgress = t.OverallProgress; } } if (!found) tasks.Add(t); } </code></pre> <p>Your progressbar events</p> <pre><code>void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { lock (tasks) { AddOrModifyTask((BackupTask)e.UserState); } listView1.ItemsSource = tasks; listView1.Items.Refresh(); } void bw_DoWork(object sender, DoWorkEventArgs e) { task_definitions = tasks; Parallel.ForEach(task_definitions, task =&gt; { string computer = task.MachineName; string user = task.Username; string pass = task.Password; //your old Do_Work code }); } </code></pre> <p>Instead of passing background worker as parameter pass associated BackupTask object and always use bw</p> <p>Quick working example is <a href="http://www.theoutsourcing.pl/parallelprogress.zip" rel="nofollow">here</a></p>
    singulars
    1. This table or related slice is empty.
    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. 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.
    3. 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