Can sort the Excel tables in multi-thread?

1

My codes as follow:

Parallel.ForEach(listSheets, (xlWorkSheet1) =>
{
  //  Excel.Worksheet xlWorkSheet1 = (Excel.Worksheet)excelbk.Worksheets["Sheet1"];
  xlRange = (Excel.Range)xlWorkSheet1.Cells[xlWorkSheet1.Rows.Count, 1];
  lock (xlRange)
  {
     //   nRows = (long)xlRange.get_End(Excel.XlDirection.xlUp).Row;
     nRows = xlWorkSheet1.UsedRange.Cells.Rows.Count;
     xlRange = (Excel.Range)xlWorkSheet1.Rows["5:" + nRows, Type.Missing];
     xlRange.Sort(xlRange.Columns[clnum1, Type.Missing], 
                  Excel.XlSortOrder.xlAscending,
                  xlRange.Columns[clnum2, Type.Missing], 
                  Type.Missing, 
                  Excel.XlSortOrder.xlAscending,
                  Type.Missing, 
                  Excel.XlSortOrder.xlAscending,
                  Excel.XlYesNoGuess.xlNo, 
                  Type.Missing, 
                  Type.Missing,
                  Excel.XlSortOrientation.xlSortColumns,
                  Excel.XlSortMethod.xlStroke,
                  Excel.XlSortDataOption.xlSortTextAsNumbers,
                  Excel.XlSortDataOption.xlSortNormal,
                  Excel.XlSortDataOption.xlSortNormal);

     Console.WriteLine("Sheet{0} have been sorted", xlWorkSheet1.Name);
  }
}

listSheets is defined as a List,and when i debug it,it shows error that the COM exception that problem have been run yet.

System.Runtime.InteropServices.COMException Application is busy (RPC_E_CALL_REJECTED 0x80010001) Call was rejected by callee (RPC_E_SERVERCALL_RETRYLATER 0x8001010A)

c#
multithreading
excel
asked on Stack Overflow Nov 19, 2012 by startewho • edited Nov 20, 2012 by Dante May Code

2 Answers

0

Excel does not support multi-threaded interop calls. Internally, it is multi-threaded. For instance, you can enable multi-threaded recalculations in the options page, for a significant performance boost. However, you cannot peform multi-threaded interop, I'm afraid.

answered on Stack Overflow Nov 19, 2012 by code4life
0

As code4life says: Excel does not support multi-threaded interop calls. And since Excel's Sort method is already multi-threaded in the latest versions there is no point in trying to multi-thread something thats already multi-threaded.

answered on Stack Overflow Nov 19, 2012 by Charles Williams

User contributions licensed under CC BY-SA 3.0