Click here to Skip to main content
14,845,649 members
Home / Discussions / C#
   

C#

 
GeneralRe: JsonConvert.DeserializeObject Pin
Member 1449252212-Feb-21 7:08
MemberMember 1449252212-Feb-21 7:08 
QuestionBackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 4:13
MemberAlex Dunlop10-Feb-21 4:13 
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 
I finally could use Invoke to avoid cross-threading. But, UI still freezes and non-responsive during UI calculations.
My final code:
       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();
    }
    private void FillCell(string _sheetName)
    {
        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);
            }
        }
        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]);
            }
        }
        foreach (var item in mydic.Keys)
        {
            keys.Add(item);
        }
        foreach (var item in mydic.Values)
        {
            values.Add(item);
        }

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

            workbook.Worksheets[_sheetName].Cells[i, 0].SetValue(rewrite[0]);
            workbook.Worksheets[_sheetName].Cells[i, 1].SetValue(rewrite[1]);
            workbook.Worksheets[_sheetName].Cells[i, 2].SetValue(rewrite[2]);
            workbook.Worksheets[_sheetName].Cells[i, 3].SetValue(rewrite[3]);
            workbook.Worksheets[_sheetName].Cells[i, 4].SetValue(rewrite[4]);
        }
        for (int i = 0; i < mydic.Count; i++)
        {
            int text = values[i];

            workbook.Worksheets[_sheetName].Cells[i, 5].SetValue(text);
        }
    }

    private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {

        IWorkbook workbook = spreadsheetControl.Document;
        Worksheet worksheet = workbook.Worksheets["DataSet1"];

        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"); });
                spreadsheetBarController1.Control.Invoke((Action)delegate { FillCell("Summarized"); });
            }
        }
        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)
    {
        progressPanel1.Visible = true;
    }
}

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 
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 

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.