什么是做Windows.Forms的后台任务的最简单的方法是什么? [英] What is the simplest way to do background tasks in Windows.Forms?

查看:130
本文介绍了什么是做Windows.Forms的后台任务的最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后台任务是东西,包括网络I / O,磁盘I / O,或其他长时间运行的任务,可能会或可能不会发生在网络上。它会经常与code,更新的图形用户界面,它需要在另一个线程,GUI线程。

Background tasks being stuff that involves network I/O, disk I/O, or other long-running tasks that may or may not take place over a network. It will often intermix with code that updates the GUI, which needs to be running on another thread, the GUI thread.

简单的意思,在打开一个Form.cs文件时,源$ C ​​$ c是容易或更容易阅读比以前。在实践中,在源$ C ​​$ c中的流动仍必须依次与问候它执行对在其中code为执行,而不管哪个线程的顺序读出。所有配套面料必须是可重复使用的和隐藏起来的地方,而不是包含在每个表单中。

Simple meaning that when opening a Form.cs file, the source code is as easy or easier to read than before. In practice, the flow of the source code must still read sequentially with regards to the order in which the code is executed, regardless of which thread it executes on. All the supporting fabric must be reusable and hidden away somewhere, rather than being included in each Form.

谷歌搜索MSDN:发现,通过微软官方认可的解决方案是System.ComponentModel.BackgroundWorker,这属于(非常!)简短的第二点

Googling MSDN: found that the solution officially sanctioned by Microsoft is the System.ComponentModel.BackgroundWorker, which falls (very!) short on the second point.

(还有在System.Windows.Threading.Dispatcher一个官方认可的Silverlight / XAML / 3.5解决方案模式。)

推荐答案

这是我来了迄今最简单的想法。它可能完全取消犹太,我有codeD难以置信的接近零Windows.Forms的应用程序。

This is the simplest idea I've come up with so far. It may be entirely un-kosher, I've coded incredibly close to zero Windows.Forms applications.

它涉及到一个帮手里面有两个主要方法,背景()和前景()。无论这些需要在一个lambda EX pression的形式指定一个委托。背景()开始在后台线程任何给定的委托,并立即返回。前景()发送任何给定的委托回到使用Form.BeginInvoke(GUI线程),并立即返回。

It involves a helper which has two main methods, Background() and Foreground(). Either of these take a delegate specified in the form of a lambda expression. Background() starts any given delegate on a background thread and returns immediately. Foreground() sends any given delegate "back" to the GUI thread using Form.BeginInvoke() and returns immediately.

在以下是如何使用这种设计图案的$ C $℃实施例,前提是该助手已经实现

In the following is a code example of how to use this design pattern, provided that the helper is already implemented.

public class Form1 : Form {
    protected ProgressBar progressBar1;
    protected Button button1;

    protected BackgroundHelper helper = new BackgroundHelper();

    public void button1_Click(...) {
    	// Execute code in the background.
    	helper.Background(() => {
    		for (int i = 0; i <= 100; i++) {
    			// Continually report progress to user.
    			helper.Foreground<int>(i, j => {
    				progressBar1.Value = j;
    			});
    			// Simulate doing I/O or whatever.
    			Thread.Sleep(25);
    		}
    	});
    }
}

这保持了code顺序整齐,提供共享在一个好地方变量,并允许跨越两个线程的循环。

This keeps the code neatly sequential, provides shared variables in a good place, and allows loops that span the two threads.

要澄清的助手完成,

  • 构造函数启动一个后台线程在队列中等待。
  • 在这两个背景()和前景()立即返回。
  • 在后台()入队code。使用内部队列在后台线程中运行。
  • 前景()不一样,用的BeginInvoke首先创建助手GUI线程上。

编辑:实施:
HTTP://$c$c.google.com/p/backgrounder/

Implementation:
http://code.google.com/p/backgrounder/

这篇关于什么是做Windows.Forms的后台任务的最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆