【发布时间】:2013-11-19 10:33:41
【问题描述】:
我使用 C# 开发了一个 Windows 窗体应用程序。
应用程序将读取 Excel 文件,将该值存储到数据表中,执行一些处理并将处理后的信息写入新的 Excel 文件。
写入 Error saving file 或 System.OutOfMemoryException 或 ContextSwitchDeadlock was detected
还显示以下错误(在 Visual Studio 窗口内):
The CLR has been unable to transition from COM context 0x592b88 to COM context 0x592cf8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
worksheetToWrite.Cells["A" + Iteration1].Value = sMailID2.Trim();
worksheetToWrite.Cells["B" + Iteration1].Value = intCurrentBatchSize.ToString();
if (sFull1.Length <= 4000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, sFull1.Length);
worksheetToWrite.Cells["D" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["E" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["F" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 4000 && sFull1.Length <= 8000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, sFull1.Length - 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["F" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 8000 && sFull1.Length <= 12000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, sFull1.Length - 8000);
worksheetToWrite.Cells["F" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 12000 && sFull1.Length <= 16000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, sFull1.Length - 12000);
worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 16000 && sFull1.Length <= 20000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, sFull1.Length - 16000);
worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 20000 && sFull1.Length <= 24000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, sFull1.Length - 20000);
worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 24000 && sFull1.Length <= 28000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, sFull1.Length - 24000);
worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 28000 && sFull1.Length <= 32000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, 4000);
worksheetToWrite.Cells["J" + Iteration1].Value = sFull1.Substring(28000, sFull1.Length - 28000);
worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 32000 && sFull1.Length <= 36000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, 4000);
worksheetToWrite.Cells["J" + Iteration1].Value = sFull1.Substring(28000, 4000);
worksheetToWrite.Cells["K" + Iteration1].Value = sFull1.Substring(32000, sFull1.Length - 32000);
worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 36000 && sFull1.Length <= 40000)
{
worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, 4000);
worksheetToWrite.Cells["J" + Iteration1].Value = sFull1.Substring(28000, 4000);
worksheetToWrite.Cells["K" + Iteration1].Value = sFull1.Substring(32000, 4000);
worksheetToWrite.Cells["L" + Iteration1].Value = sFull1.Substring(36000, sFull1.Length - 36000);
}
Iteration1 表示行。我在每个单元格中保存 4000 个字符,但第一行中的单元格以及每行的第一列和第二列中的单元格除外。
谁能帮我解决。
【问题讨论】:
-
你的数据表是固定行吗?
-
没有。我从 Excel 中读取了多达 3500 行并将其存储到数据表中。阅读没有问题。该错误仅在写入时发生。
-
现在我也得到 System.OutOfMemoryException
-
你能发布完整的错误信息吗?
System.OutOfMemoryException在哪一行? -
你为什么写
string.Empty;?你不能省略那些代码行吗?