C#.net中的多个Banckground Worker [英] Multiple Banckground Worker in C#.net
问题描述
朋友们,
我正在C#.net中开发Windows应用程序.我在我的项目中使用Backgroundworker.我知道如何使用Backgroundworker吗?但我不知道如何动态使用(Backgroundworker).所以,请告诉我.
我列出了我的控件:
1)FlowlayoutPanel ---静态
2)分组框-动态
3)图片框(3)-动态
4)BackgroundWorker-动态
任务:
当我单击一个图片框时,Backgroundworker将运行
控件说明:
组合盒DEVICE1
DEVICE2
DEVICE3
图片框:
PictureBox1
PictureBox2
PictureBox3
这些图片框中的内容内置在每个GroupBox中
当我单击Device1中的Picture Box1时-Backgroundworker将运行.
同时单击Device2-中的Picture Box1-Backgroundworker将运行.
而且它还在继续.所以请告诉我如何实现此目标?
在此先感谢.
问候,
Lakshmi Narayanan.S
Hi friends,
I am developing a Windows Application in C#.net. I am using Backgroundworker in my Project.I know how to use Backgroundworker?But i don''t How to use Dynamically(Backgroundworker). So Please tell me .
I list out my Controls:
1)FlowlayoutPanel --- Static
2)Group Box -- Dynamic
3)picture Boxes(3)-- Dynamic
4)BackgroundWorker -- Dynamic
Task:
when I click the One of picture Box ,Backgroundworker will run
Controls description:
Group Box DEVICE1
DEVICE2
DEVICE3
Picture Box:
PictureBox1
PictureBox2
PictureBox3
in these picture boxes are inbuilt in the Each GroupBox
When i Click Picture Box1 in the Device1-- Backgroundworker will run.
at the same time i click Picture Box1 in the Device2-- Backgroundworker will run.
and it ''s going on .So please tell me How to achieve this?
Thanks in Advance.
Regards,
Lakshmi Narayanan.S
推荐答案
你好Narayanan
不确定您希望后台处理器执行什么操作.我在下面创建了一个,用相应文本块中的%更新进度条状态.如果您要它做其他事情,请告诉我.
XAML窗口代码应为->
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)
{
}
}
}
注意:-我将Button3 click事件留为空白,您可以模仿类似的方法
希望这会有所帮助:)
快乐编码!
Prachi
Note:- I have left Button3 click event blank you can imitate the similar approach
Hope this helps :)
Happy Coding!!
Prachi
这篇关于C#.net中的多个Banckground Worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!