Click here to Skip to main content
15,886,110 members
Home / Discussions / C#
   

C#

 
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 
I'm working on Devexpress Spreadsheet application. My calculation are as below:
1. Data is read from a sheet into two Lists. One list is string type and another is integer type. String type list consists of values of 4 cells in each row.
2. Lists are put into a dictionary. Strings are keys and integers are values.
3. Values of the same keys are summed.
4. The mentioned two lists are cleared and fill by dictionary keys and values.
5. Dictionary keys and values are split into cells in a new sheet.

I want to show progress animation (comes with DevExpress tools) when calculations begin. I used following code:
<pre lang="C#"> private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            IWorkbook workbook = spreadsheetControl.Document;
            Worksheet worksheet = workbook.Worksheets.ActiveWorksheet;
            CellRange range = worksheet.GetDataRange();
            int LastRow = range.BottomRowIndex;
            var keys = new List<string>();
            var values = new List<int>();

            //The line below shows progress animation
            progressPanel1.Visible = true;

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

            if (worksheet.HasData)
            {
                if (workbook.Worksheets.Contains("Summarized") == false)
                {
                    workbook.Worksheets.Add().Name = "Summarized";
                }
                //or
                //workbook.Worksheets.Add("Summarize")
                Worksheet ws_summarized = workbook.Worksheets["Summarized"];
                ws_summarized.Clear(workbook.Worksheets["Summarized"].GetUsedRange());
                keys.Clear();
                values.Clear();

                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["Summarized"].Cells[i, 0].SetValue(rewrite[0]);
                    workbook.Worksheets["Summarized"].Cells[i, 1].SetValue(rewrite[1]);
                    workbook.Worksheets["Summarized"].Cells[i, 2].SetValue(rewrite[2]);
                    workbook.Worksheets["Summarized"].Cells[i, 3].SetValue(rewrite[3]);
                    workbook.Worksheets["Summarized"].Cells[i, 4].SetValue(rewrite[4]);
                }
                for (int i = 0; i < mydic.Count; i++)
                {
                    int text = values[i];

                    workbook.Worksheets["Summarized"].Cells[i, 5].SetValue(text);
                }
            }
            else
            {
                MessageBox.Show("خطای داده ورودی", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

When I run and press the dedicated button, the following runtime error occurs:
C#
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'spreadsheetFormulaBarPanel' accessed from a thread other than the thread it was created on.'

The error in in line
C#
progressPanel1.Visible = true;

I tried to delete that line and check whether it works. This time the following runtime error occurred:
C#
System.InvalidOperationException: 'This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.'

This error occurs in line
C#
backgroundWorker1.RunWorkerAsync();

PLEASE HELP ME.Confused | :confused: Confused | :confused:
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 
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 
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
Alex 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
mveRalf Meier10-Feb-21 20:22 

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.