在我的程序中,我使用任务并行库来减少进程的时间。我使用这段代码来完成这项工作。
Task[] tasks = new Task[10]
{
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 1 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 2 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 3 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 4 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 5 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 6 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 7 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 8 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 9 , (e.Argument as string[])[1])),
Task.Factory.StartNew(() => DoFiles(fullNum,NotFullNum, 10, (e.Argument as string[])[1]))
};
Task.WaitAll(tasks);
DoFiles() 方法处理一些文件,然后在数据库中的文件中插入单词。每个任务处理一些文件。我使用此代码插入数据库:
SqlBulkCopy sbc = new SqlBulkCopy(clsGlobal.cnTashih);
sbc.DestinationTableName = "tblListTekrari_3";
sbc.BulkCopyTimeout = 0;
sbc.WriteToServer(dtT);
sbc.Close();
当我运行程序时,有时会出现此错误:
Cannot access destination table 'tblListTekrari_3'.
System.Data
The request failed to run because the batch is aborted, this can be caused by abort signal sent from client, or another request is running in the same session, which makes the session busy.
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternal()
at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServer(Int32 columnCount)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table)
at tashih.frmStart.DoFiles(Int32 countFull, Int32 NotCountFull, Int32 part, String ProjectID)
有谁能够帮我?