English 中文(简体)
在Windows完成背景任务的最简单方式是什么。 形式?
原标题:What is the simplest way to do background tasks in Windows.Forms?
  • 时间:2009-10-06 10:46:43
  •  标签:

涉及网络I/O、软盘I/O或其他长期任务,可能或不可能在网络上执行。 它往往与更新全球倡议的法典相配合,后者需要再 running,即全球倡议。

简单的含义是,在开设表格C时,源代码比以前容易阅读或容易阅读。 在实践中,源代码的流通仍然必须依次与实施该守则的先后次序一并阅读,而不论该守则已执行。 所有的辅助结构必须加以改造,在某个地方被掩盖,而不是列入每一种形式。

GooglingMSDN:发现Microsoft正式认可的解决办法是该系统,Component Model.BackgroundWorker,该办法在第二点(每!)很短。

(还有一个正式认可的银灯/XAML/3.5解决方案模式,见www.o.org.Threading.Displer.)

最佳回答

这是我迄今提出的最简单的想法。 它可能完全是无孔不入的,I ve编码令人难以置信地接近零窗。 表格应用。

它涉及一个有两个主要方法的助手,即背景和理由。 两人都以伊斯兰教表达方式指定代表。 背景:任何指定代表都立即在背景和返回的基础上开始。 Foreground () sent any given delegation “back” to the GUI thread using Form.BeginInvoke() and Return Immediate.

下面是关于如何使用这一设计模式的典型例子,条件是帮助者已经执行。

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);
            }
        });
    }
}

这使得《法典》保持了严格的顺序,提供了良好的共同变量,并允许跨越两条线的通道。

澄清帮助者所做的工作,

  • The constructor starts a background thread that waits on a queue.
  • Both Background() and Foreground() returns immediately.
  • Background() enqueues code to run in the background thread using the internal queue.
  • Foreground() does the same, with BeginInvoke on the GUI thread that first created the helper.

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

问题回答

如果你真的不像背景工作者,那么你就可以为背景行动创建自己的基级,正如我所做的那样:here

您仍可使用<代码>。 背景工作人。 它不需要作为形式的组成部分。 你可以很容易地把它归结为可以再用每一种形式的一个类别。

然而,这与在需要时为背景任务设立工人没有什么不同。

请解释一下你为什么要说<代码>。 履历:

在大多数情况下,它需要2至3条额外的法典。

例如,在你问答I/O的问题之后,我最近(或所有年份的100次)写了一幅同步的档案扫描仪。 它像你所希望的那样,通过在事件中立即出现新的事例和hoo。

我将这一功能提升到能够实施跨阅读安全的班级。 班级冲撞事件,向打电话者通报最新情况和填写情况。

你们说,“来源法必须依次结合实施该守则的顺序来解读”,但认识到,表面上是相互平行的。 因此,它愿意将守则结构分开。

这表明,可再使用的法典被单独分类......

Public Class FileScanner
    Public Event Scan_Complete(sender As Object)
    Public Event Scan_Update(sender As Object, filename As String)
    Public Event Scan_Error(sender As Object, ex As Exception)
    Private Delegate Sub del_ScanComplete()

    Sub New(syncObject As Control, path String)
        Me.SynchronizeObject = syncObject
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ScanFilesAsync), path)
    End Sub

    Private Sub ScanFilesAsync(ByVal state As Object)

          scan files here

          call the method to raise the Complete event
        ScanComplete()

    End Sub

    Private Sub ScanComplete()

        If SynchronizeObject.InvokeRequired Then
              we cant raise event on a different thread than our caller
              so lets invoke our method on the same caller thread
            Dim d As New del_ScanComplete(AddressOf ScanComplete)
            SynchronizeObject.Invoke(d)
        Else
              no synchronize needed, tell the caller we are done
            RaiseEvent Complete(Me)
        End If

    End Sub

End Class




相关问题
热门标签