我嘗試啓動多線程,但我不能它返回給我錯誤:跨線程操作無效:'listBox1'線程被創建用來控制來自另一線程的外部訪問。如何解決「跨線程操作無效」?
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:\Source\Addresses.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();
}
你學習常用的東西,我絕對喜歡約SO。他們已經偷偷吃了一些好吃的東西,而不是他們。 – Spence 2010-06-15 14:20:00