2
Answers

Background worker with input

alec

alec

13y
2.1k
1
I have a function 

public void DataTableToExcel (DataTable DT)
        {
            Excel.Application ExcelApp = new Excel.Application();
            ExcelApp.Workbooks.Add();
            ExcelApp.Workbooks[1].Worksheets.Add();
            Excel.Worksheet wSheet = ExcelApp.Workbooks[1].Worksheets[1];


            for (int i = 1; i < DT.Columns.Count + 1; i++)
            {
                wSheet.UsedRange[1, i] = DT.Columns[i - 1].ColumnName.ToString();
            }


            wSheet.get_Range(ExcelColumnLetter(0) + "1", ExcelColumnLetter(DT.Columns.Count) + "1").Font.Bold = true;


            for (int i = 0; i < DT.Rows.Count; i++)
            {
                for (int j = 0; j < DT.Columns.Count; j++)
                {
                    wSheet.UsedRange[i + 2, j + 1] = DT.Rows[i][j].ToString();
                }
            }
            ExcelApp.Visible = true;
        }

and I want it as a backgroundworker as I want it in a different thread and to report its progress. 

How do I make a backgroundworker with an input?
Answers (2)
0
mibuyt
NA 512 0 19y
Well, Normally what i did for parent and child. Parent - .show() Child - .showdialog() So child definetely can close properly by calling this.Close() But to close parent, execute Application.Exit() Hope it helps :)
0
doug 0
NA 14 0 19y
Thank you for your reply. I am currently not setting the Owner propery, but good to know. A couple of observations: 1.I am using the ShowDialog() method as opposed to the Show() method because I want them to do something in the form or destroy it. 2.The maximize and minimize buttons work fine no problem with them. 3.I am using serial communications very heavily and all of the forms are in while loops for getting data out of serial port (RS232). I created a very simple form fired from a button on the parent form. The close 'X' button worked until I added a while loop similar to the ones in my other child forms. I am using the App.DoEvents method in this loop to catch other events that are firing, but does not seem to work on the close button. Odd how it works with the min/max buttons. Wondering how that event would get killed just because of the while loop? Add this simple loop to one of your methods in the five forms you created and see if you have the same results. while(key_pressed !=0) //wait until user hit a key on the keyboard then go on Application.DoEvents(); //would think this would catch the close 'X' button
0
spgilmore
NA 591 0 19y
If implemented, it will first fire the FormClosing event. If the eventargs.Cancel method is not executed there, then it will go on to close and fire the FormClosed event. As for closing the child forms, how do you create them? I have a project with two forms. The first form has a button that creates 5 instances of the second form. private void button1_Click(object sender, EventArgs e) { for (int j = 0; j < 5; j++) { Form2 frm = new Form2(); frm.Owner = this; frm.Show(); } } If I close the main form, the 5 children are destroyed and the application terminates. If I remove this line: frmOwner= this; ... then the children will not be destroyed when I close the main form and the application continues to run. Check the owner property of your child forms. If they are not owned, they will not be destroyed automatically.