在单独的线程中运行方法并在保存文件对话框中出错 [英] Run method in separate thread and error with save file dialog

查看:60
本文介绍了在单独的线程中运行方法并在保存文件对话框中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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保存文件对话框没有打开.我需要纠正哪个区域.lz解释一下.谢谢.

when the button1 is clicked then export method is getting called in separate thread and no error raise but save file dialog is not getting error. so please tell me what is my mistake in the code. my approach is wrong to call a method in separate thread. also explain plzz save file dialog is not opening. which area i need to rectify. plzz explain. thanks.

推荐答案

请记住,所有 Winforms 对象都应该从主 UI 线程使用.所以在单独的线程中你必须使用 Invoke/BeginInvoke.如果可以,请在 UI 线程中执行所有Winforms 内容",然后使用所需的所有数据/信息运行单独的线程.

Keep in your mind, that all Winforms object should be used from main UI thread. So in separate thread you MUST use Invoke/BeginInvoke. If you can, do all "Winforms stuff" in UI thread and after that run separate thread with all data/informations which is required.

我认为,更好的方法是:

I think, the better way is:

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

这篇关于在单独的线程中运行方法并在保存文件对话框中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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