Click here to Skip to main content
15,904,024 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to access GUI from another thread? PinPopular
Luc Pattyn11-Feb-21 11:29
sitebuilderLuc Pattyn11-Feb-21 11:29 
GeneralRe: How to access GUI from another thread? Pin
Eddy Vluggen11-Feb-21 14:01
professionalEddy Vluggen11-Feb-21 14:01 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 18:18
Alex Dunlop11-Feb-21 18:18 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 19:39
Alex Dunlop11-Feb-21 19:39 
AnswerMessage Closed Pin
11-Feb-21 14:18
professionalEddy Vluggen11-Feb-21 14:18 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 18:16
Alex Dunlop11-Feb-21 18:16 
QuestionJsonConvert.DeserializeObject Pin
Member 1449252210-Feb-21 21:43
Member 1449252210-Feb-21 21:43 
AnswerRe: JsonConvert.DeserializeObject Pin
OriginalGriff10-Feb-21 22:05
mveOriginalGriff10-Feb-21 22:05 
AnswerRe: JsonConvert.DeserializeObject Pin
Member 1449252211-Feb-21 4:13
Member 1449252211-Feb-21 4:13 
AnswerRe: JsonConvert.DeserializeObject Pin
James Curran11-Feb-21 23:58
James Curran11-Feb-21 23:58 
GeneralRe: JsonConvert.DeserializeObject Pin
Member 1449252212-Feb-21 7:08
Member 1449252212-Feb-21 7:08 
QuestionBackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 4:13
Alex 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
Alex 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
Alex 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
Alex 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:
C#
       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
Alex Dunlop12-Feb-21 6:03 
AnswerRe: BackGroundWorker gives runtime error Pin
Ralf Meier10-Feb-21 4:48
mveRalf Meier10-Feb-21 4:48 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 4:57
Alex Dunlop10-Feb-21 4:57 
GeneralRe: BackGroundWorker gives runtime error Pin
Alex Dunlop10-Feb-21 7:47
Alex 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
Alex Dunlop10-Feb-21 16:49 

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.