几天前,我开始在我的 WPF 应用程序中使用任务来实现并行化。我的应用程序需要每 5 秒执行一次工作。这项工作必须由 4 个任务并行化。除此之外,还需要实现后台工作程序,以避免在按任务执行工作时冻结 UI。我找到了很多例子来理解任务是如何工作的。但是,我找不到任何简单的示例来理解任务如何与计时器、后台工作人员和锁一起工作。我根据自己的理解写了一个简单的例子。请给我一些关于我是否正确执行的建议。这样,我将对 WPF 中的多任务处理有更好的理解。我期待着您的回复。
namespace timer_and_thread
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer TimerObject;
Task[] tasks;
readonly object _countLock = new object();
int[] Summ = new int[10];
int Index = 0;
int ThreadsCounter = 0;
public MainWindow()
{
InitializeComponent();
TimerObject = new DispatcherTimer();
TimerObject.Tick += new EventHandler(timer_Elapsed);
TimerObject.Interval = new TimeSpan(0, 0, 5);
}
// call the method every 5 seconds
private void timer_Elapsed(object sender, EventArgs e)
{
TimerObject.Stop();
// 4 - is the tasks' count
ThreadsCounter = 4;
Index = 0;
tasks = new Task[4];
tasks[0] = Task.Factory.StartNew(() => DoSomeLongWork());
tasks[1] = Task.Factory.StartNew(() => DoSomeLongWork());
tasks[2] = Task.Factory.StartNew(() => DoSomeLongWork());
tasks[3] = Task.Factory.StartNew(() => DoSomeLongWork());
// wait untill all tasks complete
while (ThreadsCounter != 0) ;
TimerObject.Start();
}
private void DoSomeLongWork()
{
while (Index < Summ.Length)
{
// lock the global variable from accessing by multiple threads at a time
int localIndex = Interlocked.Increment(ref Index) - 1;
//I wrote rundom number generation just a an example of doing some calculation and getting some result. It can also be some long calculation.
Random rnd = new Random();
int someResult = rnd.Next(1, 100000);
// lock the global variable (Summ) to give it the result of calculation
lock (_countLock)
{
Summ[localIndex] = someResult;
}
}
Interlocked.Decrement(ref ThreadsCounter);
return;
}
// button by which I start the application working
private void Start_Button_Click_1(object sender, RoutedEventArgs e)
{
TimerObject.Start();
}
}
}
我还有两个问题:
我可以使用任务而不是后台工作人员吗?
正如我所拥有的那样,使用锁定是为了防止线程访问全局变量。但是,要通过线程访问 UI 元素,我应该使用 Dispatcher。正确的?
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
label1.Content = "Some text";
}));