使用保存文件对话框在单独的线程中运行方法并出错

时间:2011-04-06 08:10:27

标签: c# .net winforms multithreading savefiledialog

private void button1_Click(object sender, EventArgs e)
    {
        new System.Threading.Thread(delegate()
        {
          Export();
        }).Start();

    }

    private void Export()
    {
        int rowcount = ((System.Data.DataTable)this.dgResult.DataSource).Rows.Count;
        System.Data.DataTable dt = (System.Data.DataTable)this.dgResult.DataSource;
        if (rowcount > 0)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(delegate()
                {
                    svDialog.Filter = "Excel|*.xls";
                    svDialog.Title = "Save an Excel File";
                    svDialog.ShowDialog();
                    if (svDialog.FileName != "")
                    {
                        Business.ExportToExcel.ExcelFromDataTable(dt, svDialog.FileName);
                        MessageBox.Show("Export completed");
                    }
                }));
            }
            else
            {
                svDialog.Filter = "Excel|*.xls";
                svDialog.Title = "Save an Excel File";
                svDialog.ShowDialog();
                if (svDialog.FileName != "")
                {
                    Business.ExportToExcel.ExcelFromDataTable(dt, svDialog.FileName);
                    MessageBox.Show("Export completed");
                }
            }
        }
        else
        {
            MessageBox.Show("No data found");
        }
    }

当单击button1时,导出方法将在单独的线程中调用,并且没有错误引发但是保存文件对话框没有出错。所以请告诉我代码中的错误是什么。我在单独的线程中调用方法是错误的。还解释了plzz保存文件对话框没有打开。我需要纠正哪个方面。 plzz解释。感谢。

1 个答案:

答案 0 :(得分:1)

请记住,应该从主UI线程中使用所有Winforms对象。所以在单独的线程中你必须使用Invoke / BeginInvoke。如果可以的话,在UI线程中执行所有“Winforms stuff”,之后运行单独的线程,其中包含所需的所有数据/信息。

我认为,更好的方法是:

private void button1_Click(object sender, EventArgs e) {
    this.Export();
}
private void Export() {
    System.Data.DataTable dt = (System.Data.DataTable)this.dgResult.DataSource;
    if ( dt.Rows.Count > 0 ) {
        // initialize save file dialog
        DialogResult rslt = this.svDialog.ShowDialog(this);
        if ( rslt == DialogResult.OK ) {
            string filePath = this.svDialog.FileName;
            // QueueUserWorkItem runs target delegate in separate thread
            ThreadPool.QueueUserWorkItem( (_state)=> this.Export(dt, filePath) );
        }
    }
    else {
        // ... some other code ....
    }
}
private void Export(DataTable data, string filePath) {
    Exception thrownException = null;
    try { Business.ExportToExcel.ExcelFromDataTable(dt, filePath); }
    catch( Exception exc ) { thrownException = exc; }

    if ( null == thrownException ) { MsgBox("Export completed."); }
    else { MsgBox("Error: " + thrownException.Message); }
}
private void MsgBox(string text) {
    if (this.InvokeRequired) {
        Action<string> dlg = this.MsgBox;
        this.Invoke( dlg, text );
    }
    else {
        MessageBox.Show(this, text);
    }
}