Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

c# - How can solve "Cross-thread operation not valid"?

i try to start multi Thread but i can not it returns to me error: Cross-thread operation not valid: 'listBox1' thread was created to control outside access from another thread was.

MyCodes:

 
  public DataTable dTable;
        public DataTable dtRowsCount;
        Thread t1;
        ThreadStart ts1;
  void ExcelToSql()
        {
           // SelectDataFromExcel();
            ts1 = new ThreadStart(SelectDataFromExcel);
            t1 = new Thread(ts1);
            t1.Start();
        }
   void SelectDataFromExcel()
        {
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:SourceAddresses.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
            OleDbConnection excelConnection = new OleDbConnection(connectionString);
                      string[] Sheets = new string[] { "Sayfa1"};
            excelConnection.Open(); // This code will open excel file.            
            OleDbCommand dbCommand;
            OleDbDataAdapter dataAdapter;
          //  progressBar1.Minimum = 1;

            foreach (var sheet in Sheets)
            {
                dbCommand = new OleDbCommand("select * From[" + sheet + "$]", excelConnection);
                //progressBar1.Maximum = CountRowsExcel(sheet).Rows.Count;
               // progressBar2.Value = i + 1;
                System.Threading.Thread.Sleep(1000);
                **listBox1.Items.Add("Tablo ismi: "+sheet.ToUpper()+"Sat?r Adeti: "+CountRowsExcel(sheet).Rows.Count.ToString()+" ");**
                dataAdapter = new OleDbDataAdapter(dbCommand);
                dTable = new DataTable();
                dataAdapter.Fill(dTable);
                dTable.TableName = sheet.ToUpper();
                dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
                ArrangedDataList(dTable);
                FillSqlTable(dTable, dTable.TableName);
            }

            excelConnection.Close();
            excelConnection.Dispose();
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The thread in background is not allowed to access the UI components.

In multithreaded forms, I include code like this:

    private delegate void InvokeAction();
    private void DoUI(InvokeAction call) {
        if (IsDisposed) {
            return;
        }
        if (InvokeRequired) {
            try {
                Invoke(call);
            } catch (InvalidOperationException) {
                // Handle error
            }
        } else {
            call();
        }
    }

Then from my background thread I can code like this:

    // Stuff in other thread
    myVar = GetFromDb();
    DoUI(() => { 
       // Access UI components here 
       listBox1.Items.Add(myVar);
    });
    // More other thread 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...