Click here to Skip to main content
14,841,473 members
Home / Discussions / C#
   

C#

 
AnswerRe: BackGroundWorker gives runtime error Pin
OriginalGriff10-Feb-21 4:36
mveOriginalGriff10-Feb-21 4:36 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 4:44
MemberAlex Dunlop10-Feb-21 4:44 
GeneralRe: BackGroundWorker gives runtime error Pin
OriginalGriff10-Feb-21 5:00
mveOriginalGriff10-Feb-21 5:00 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 5:03
MemberAlex Dunlop10-Feb-21 5:03 
GeneralRe: BackGroundWorker gives runtime error Pin
OriginalGriff10-Feb-21 5:43
mveOriginalGriff10-Feb-21 5:43 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop12-Feb-21 1:44
MemberAlex Dunlop12-Feb-21 1:44 
GeneralRe: BackGroundWorker gives runtime error Pin
OriginalGriff12-Feb-21 2:11
mveOriginalGriff12-Feb-21 2:11 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop12-Feb-21 6:03
MemberAlex Dunlop12-Feb-21 6:03 
I made some changes in my code. I used datatable for filling spreadsheet. I tried to pass UI related works (filling cells) to ProgressChanged event.
But it still freezes when filling spreadsheet.
private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
       {
           if (!backgroundWorker1.IsBusy)
           {
               backgroundWorker1.RunWorkerAsync();
           }
       }
       private void CreateSheet(string _sheetName)
       {
           IWorkbook workbook = spreadsheetControl.Document;

           if (workbook.Worksheets.Contains(_sheetName) == false)
           {
               workbook.Worksheets.Add().Name = _sheetName;
           }
               //or
               //workbook.Worksheets.Add("Summarize")
       }
       private void ClearSheet(string _sheetName)
       {
           IWorkbook workbook = spreadsheetControl.Document;
           var keys = new List<string>();
           var values = new List<int>();

           Worksheet ws_summarized = workbook.Worksheets[_sheetName];
           ws_summarized.Clear(workbook.Worksheets[_sheetName].GetUsedRange());
           keys.Clear();
           values.Clear();
       }
       DataTable my_table = new DataTable();
       DataTable my_table2 = new DataTable();
       private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
       {
           IWorkbook workbook = spreadsheetControl.Document;
           Worksheet worksheet = workbook.Worksheets["DataSet1"];
           CellRange range = worksheet.GetDataRange();
           int LastRow = range.BottomRowIndex;
           var keys = new List<string>();
           var values = new List<int>();

           for (int i = 0; i < LastRow + 1; i++)
           {
               if (worksheet.Cells[i, 10].DisplayText == "خاتمه یافته")
               {
                   keys.Add(string.Join(",", worksheet.Cells[i, 28].DisplayText, worksheet.Cells[i, 0].DisplayText, worksheet.Cells[i, 9].DisplayText,
                   worksheet.Cells[i, 15].DisplayText, worksheet.Cells[i, 31].DisplayText));
                   values.Add((int)worksheet.Cells[i, 32].Value.NumericValue);
               }
           }
           if (worksheet.HasData)
           {
               if (spreadsheetBarController1.Control.InvokeRequired)
               {
                   //***Instead of (Action), we can use (MethodInvoker)***
                   spreadsheetBarController1.Control.Invoke((Action)delegate { CreateSheet("Summarized"); });
                   spreadsheetBarController1.Control.Invoke((MethodInvoker)delegate { ClearSheet("Summarized"); });
               }

               var mydic = new Dictionary<string, int>();
               for (int i = 0; i < keys.Count; i++)
               {
                   if (mydic.ContainsKey(keys[i]))
                   {
                       mydic[keys[i]] += values[i];
                   }
                   else
                   {
                       mydic.Add(keys[i], values[i]);
                   }
               }
               keys.Clear();
               values.Clear();
               foreach (var item in mydic.Keys)
               {
                   keys.Add(item);
               }
               foreach (var item in mydic.Values)
               {
                   values.Add(item);
               }

               my_table.Columns.Add("A");
               my_table.Columns.Add("B");
               my_table.Columns.Add("C");
               my_table.Columns.Add("D");
               my_table.Columns.Add("E");

               for (int i = 0; i < mydic.Count; i++)
               {
                   string text = keys[i];
                   string[] rewrite = text.Split(',');

                   my_table.Rows.Add(rewrite[0], rewrite[1], rewrite[2], rewrite[3], rewrite[4]);
               }

               my_table2.Columns.Add("F");
               for (int i = 0; i < mydic.Count; i++)
               {
                   int text = values[i];

                   my_table2.Rows.Add(text);
               }
               backgroundWorker1.ReportProgress(100);
           }
           else
           {
               MessageBox.Show("خطای داده ورودی", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }

       private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
       {
           progressPanel1.Visible = false;
       }

       private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
       {
           IWorkbook workbook = spreadsheetControl.Document;
           Worksheet worksheet = workbook.Worksheets["DataSet1"];
           CellRange range = worksheet.GetDataRange();
           int LastRow = range.BottomRowIndex;

           for (int i = 0; i < my_table.Rows.Count; i++)
           {
               workbook.Worksheets["Summarized"].Cells[i, 0].SetValue(my_table.Rows[i]["A"].ToString());
               workbook.Worksheets["Summarized"].Cells[i, 1].SetValue(my_table.Rows[i]["B"].ToString());
               workbook.Worksheets["Summarized"].Cells[i, 2].SetValue(my_table.Rows[i]["C"].ToString());
               workbook.Worksheets["Summarized"].Cells[i, 3].SetValue(my_table.Rows[i]["D"].ToString());
               workbook.Worksheets["Summarized"].Cells[i, 4].SetValue(my_table.Rows[i]["E"].ToString());
           }
       }

AnswerRe: BackGroundWorker gives runtime error Pin
Ralf Meier10-Feb-21 4:48
professionalRalf Meier10-Feb-21 4:48 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 4:57
MemberAlex Dunlop10-Feb-21 4:57 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 7:47
MemberAlex Dunlop10-Feb-21 7:47 
GeneralRe: BackGroundWorker gives runtime error Pin
Luc Pattyn10-Feb-21 9:26
sitebuilderLuc Pattyn10-Feb-21 9:26 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 16:49
MemberAlex Dunlop10-Feb-21 16:49 
GeneralRe: BackGroundWorker gives runtime error Pin
Luc Pattyn10-Feb-21 17:22
sitebuilderLuc Pattyn10-Feb-21 17:22 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop13-Feb-21 8:24
MemberAlex Dunlop13-Feb-21 8:24 
GeneralRe: BackGroundWorker gives runtime error Pin
Luc Pattyn13-Feb-21 8:28
sitebuilderLuc Pattyn13-Feb-21 8:28 
AnswerRe: BackGroundWorker gives runtime error Pin
Ralf Meier10-Feb-21 20:22
professionalRalf Meier10-Feb-21 20:22 
AnswerRe: BackGroundWorker gives runtime error Pin
Eddy Vluggen11-Feb-21 14:21
professionalEddy Vluggen11-Feb-21 14:21 
QuestionMonitor data using C# and ESP8266 Pin
pinout_19-Feb-21 1:56
Memberpinout_19-Feb-21 1:56 
AnswerRe: Monitor data using C# and ESP8266 Pin
OriginalGriff9-Feb-21 2:20
mveOriginalGriff9-Feb-21 2:20 
GeneralRe: Monitor data using C# and ESP8266 Pin
pinout_19-Feb-21 2:31
Memberpinout_19-Feb-21 2:31 
AnswerRe: Monitor data using C# and ESP8266 Pin
Gerry Schmitz9-Feb-21 7:54
mveGerry Schmitz9-Feb-21 7:54 
QuestionSerialize File from one format(AMI ) to another(BAI2) format Pin
Sasikalav8-Feb-21 19:31
MemberSasikalav8-Feb-21 19:31 
AnswerRe: Serialize File from one format(AMI ) to another(BAI2) format Pin
OriginalGriff8-Feb-21 20:04
mveOriginalGriff8-Feb-21 20:04 
QuestionExistence of “Add” Function causes WCF to Fail Pin
Bernhard Hiller8-Feb-21 0:44
MemberBernhard Hiller8-Feb-21 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.