Run the method in a separate thread and error using the save file dialog

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

when button1 is pressed, then the export method is called in a separate thread and no error occurs, but saving the file dialog does not cause an error. so please tell me what is my error in the code. my approach is wrong to call a method in a separate thread. also explain that the plzz file save dialog does not open. in which area do I need to rectify the situation. Plzz explain. thank.

0
source share
1 answer

, Winforms . Invoke/BeginInvoke. , "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);
    }
}
+1

All Articles