Hi Narayanan
Not Sure about what exactly you want your background processor to do. I have created the one below which updates the progress bar status with the % in the respective textblock. Let me know if you want it to do something else.
XAML Window Code should be-->
<window x:class="BackgroundWorker.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800">
<stackpanel>
<groupbox>
<groupbox.header>
<textblock text="Device1" foreground="Black">
</textblock>
</groupbox.header>
<stackpanel>
<Button x:Name="Button1" Width="90" Height="30" HorizontalAlignment="Left" Click="Button1_Click" Margin="0 0 0 0"> <Image Source="yes.png"></Image>
</Button>
</stackpanel>
</groupbox>
<groupbox> <groupbox.header> <textblock text="Device2" foreground="Black">
</textblock></groupbox.header>
<stackpanel> <Button x:Name="Button2" Width="90" Height="30" HorizontalAlignment="Left" Click="Button2_Click" Margin="10 0 0 0">
<Image Source="yes.png"></Image>
</Button>
</stackpanel>
</groupbox>
<groupbox>
<groupbox.header>
<textblock text="Device3" foreground="Black" />
</groupbox.header>
<stackpanel>
<Button x:Name="Button3" Width="90" Height="30" HorizontalAlignment="Left" Click="Button3_Click" Margin="10 0 0 0">
<Image Source="yes.png"></Image>
</Button>
</stackpanel>
</groupbox>
<progressbar x:name="ProgressBar">
Margin="0 10 0 0"
Height="23"
Minimum="0"
Maximum="100"
/>
<progressbar x:name="ProgressBar2">
Margin="0 10 0 0"
Height="23"
Minimum="0"
Maximum="100"
/> <textblock x:name="Message">
Margin="0 10 0 0"
/> <textblock x:name="MessageUpdbyBtn1">
Margin="0 20 0 0"
/> <textblock x:name="MessageTemp">
Margin="0 10 0 0"
/> <textblock x:name="MessageUpdbyBtn2">
Margin="0 20 0 0"
/>
</textblock></textblock></textblock></textblock></progressbar></progressbar></stackpanel>
<window>
</window></window>
Write the codebehind as below:- (I have taken the background worker in code behind)
using System.Windows;
using System.ComponentModel;
using System.Threading;
using System.Windows.Media;
using System;
namespace TestBackgroundWorker7338
{
public partial class Window1 : Window
{
private BackgroundWorker _worker;
private BackgroundWorker _worker2;
int _thread1percentageFinished = 0;
int _thread2percentageFinished = 0;
public Window1()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
_worker2 = new BackgroundWorker();
_worker2.WorkerReportsProgress = true;
_worker2.WorkerSupportsCancellation = true;
_worker2.DoWork += (s, args) =>
{
BackgroundWorker worker2 = s as BackgroundWorker;
int numberOfTasks = 1000;
for (int i = 1; i <= numberOfTasks; i++)
{
if (worker2.CancellationPending)
{
args.Cancel = true;
return;
}
Thread.Sleep(5);
float percentageDone = (i / (float)numberOfTasks) * 100f;
worker2.ReportProgress((int)percentageDone);
}
};
_worker2.ProgressChanged += (s, args) =>
{
_thread2percentageFinished = args.ProgressPercentage;
MessageUpdbyBtn1.Text = String.Format("Second thread: {0}, ({1}% finished)", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), _thread2percentageFinished);
};
_worker = new BackgroundWorker();
_worker.WorkerReportsProgress = true;
_worker.WorkerSupportsCancellation = true;
_worker.DoWork += (s, args) =>
{
BackgroundWorker worker = s as BackgroundWorker;
int numberOfTasks = 300;
for (int i = 1; i <= numberOfTasks; i++)
{
if (worker.CancellationPending)
{
args.Cancel = true;
return;
}
Thread.Sleep(10);
float percentageDone = (i / (float)numberOfTasks) * 100f;
worker.ReportProgress((int)percentageDone);
}
};
_worker.ProgressChanged += (s, args) =>
{
_thread1percentageFinished = args.ProgressPercentage;
ProgressBar.Value = _thread1percentageFinished;
Message.Text = _thread1percentageFinished + "% finished";
};
_worker.RunWorkerCompleted += (s, args) =>
{
ProgressBar.Value = 0;
if (_thread1percentageFinished < 100)
{
Message.Text = "stopped at " + _thread1percentageFinished + "%";
}
else
{
Message.Text = "first thread finished";
}
};
_worker2.RunWorkerAsync();
_worker.RunWorkerAsync();
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
_worker2 = new BackgroundWorker();
_worker2.WorkerReportsProgress = true;
_worker2.WorkerSupportsCancellation = true;
_worker2.DoWork += (s, args) =>
{
BackgroundWorker worker2 = s as BackgroundWorker;
int numberOfTasks = 1000;
for (int i = 1; i <= numberOfTasks; i++)
{
if (worker2.CancellationPending)
{
args.Cancel = true;
return;
}
Thread.Sleep(5);
float percentageDone = (i / (float)numberOfTasks) * 100f;
worker2.ReportProgress((int)percentageDone);
}
};
_worker2.ProgressChanged += (s, args) =>
{
_thread2percentageFinished = args.ProgressPercentage;
MessageUpdbyBtn2.Text = String.Format("Second thread: {0}, ({1}% finished)", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), _thread2percentageFinished);
};
_worker = new BackgroundWorker();
_worker.WorkerReportsProgress = true;
_worker.WorkerSupportsCancellation = true;
_worker.DoWork += (s, args) =>
{
BackgroundWorker worker = s as BackgroundWorker;
int numberOfTasks = 300;
for (int i = 1; i <= numberOfTasks; i++)
{
if (worker.CancellationPending)
{
args.Cancel = true;
return;
}
Thread.Sleep(10);
float percentageDone = (i / (float)numberOfTasks) * 100f;
worker.ReportProgress((int)percentageDone);
}
};
_worker.ProgressChanged += (s, args) =>
{
_thread1percentageFinished = args.ProgressPercentage;
ProgressBar2.Value = _thread1percentageFinished;
MessageTemp.Text = _thread1percentageFinished + "% finished";
};
_worker.RunWorkerCompleted += (s, args) =>
{
ProgressBar2.Value = 0;
if (_thread1percentageFinished < 100)
{
MessageTemp.Text = "stopped at " + _thread1percentageFinished + "%";
}
else
{
MessageTemp.Text = "second thread finished";
}
};
_worker2.RunWorkerAsync();
_worker.RunWorkerAsync();
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
}
}
}
Note:- I have left Button3 click event blank you can imitate the similar approach
Hope this helps :)
Happy Coding!!
Prachi