gpt4 book ai didi

c# Win Form - CLR 无法从 COM 上下文转换

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:40 26 4
gpt4 key购买 nike

SQL版本:SQL Server 2008 R2标准版
应用 : .Net 3.5 (Windows Form)

这是我运行代码后收到的错误

The CLR has been unable to transition from COM context 0xe88270 to COM context 0xe88328 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.

以下代码有时会产生上述错误。很可能在插入 24,000 多条记录后出现错误。

代码的目标
编写代码以插入虚拟条目来测试我的应用程序

Random rnd = new Random();

string Data = "";

for (int i = 0; i < 2000000; i++)
{
Data = "Insert Into Table1(Field1)values('" + rnd.Next(0, 200000000) + "');" + Environment.NewLine +
"Insert Into Table1(Field1)values('" + rnd.Next(0, 200000000) + "');" + Environment.NewLine +
"Insert Into Table1(Field1)values('" + rnd.Next(0, 200000000) + "');" + Environment.NewLine +
"Insert Into Table1(Field1)values('" + rnd.Next(0, 200000000) + "');" + Environment.NewLine +
"Insert Into Table1(Field1)values('" + rnd.Next(0, 200000000) + "');" + Environment.NewLine +
"Insert Into Table1(Field1)values('" + rnd.Next(0, 200000000) + "');";
ExecuteQuery(Data);//Error is displayed here
}

“执行查询”的代码

SqlConnection Conn = new SqlConnection("Connection String");

if (Conn == null)
{
Conn = new SqlConnection(Program.ConnString);
}
if (Conn.State == ConnectionState.Closed)
{
Conn.Open();
}
else
{
Conn.Close();
Conn.Open();
}

SqlCommand cmd = new SqlCommand();
cmd.Connection = Conn;
cmd.CommandTimeout = 600000;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
Conn.Close();

注意 :- 我在查询中编写了多个完全相同的插入语句,因为它会减少编号。 SQL 必须处理的查询数

问题
我如何优化我的代码以防止错误发生?

最佳答案

"The CLR has been unable to transition from COM context … to COM context … 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."

从你的问题中你的代码如何工作的细节并不完全清楚,所以我假设你的应用程序不是多线程的,即执行 2×6 百万数据库 INSERTs 的循环正在运行应用程序的主 (UI) 线程。这可能需要一段时间(> 60 秒),因此您的 UI 将在此期间卡住(即使其无响应)。这是因为当您的(阻塞)循环仍在运行时,Windows 窗体永远没有机会运行并对用户输入使用react。我敢打赌这就是导致您引用警告的原因。

改用参数化 SQL 命令。

您可以做的第一件简单的事情就是将您的 SqlCommand 变成一个参数化命令。然后,您将使用相同的 SQL 文本发出命令;只有单独提供的参数会有所不同:

private void InsertRandomNumbers(int[] randomNumbers)
{
const string commandText = "INSERT INTO dbo.Table1 (Field1) VALUES (@field1);"
using (var connection = new SqlConnection(connectionString)
using (var command = new SqlCommand(commandText, connection))
{
var field1Parameter = new SqlDataParameter("@field1", SqlDbType.Int);
command.Parameters.Add(field1Parameter);
connection.Open();
foreach (int randomNumber in randomNumbers)
{
field1Parameter.Value = randomNumber;
/* int rowsAffected = */ command.ExecuteNonQuery();
}
connection.Close();
}
}

请注意 commandText 是如何定义为常量的。这是一个很好的迹象,表明 SQL Server 也将始终将其识别为相同的命令——对于参数化命令,实际参数值是单独提供的——SQL Server 只会编译和优化语句一次(并将编译后的语句放入其缓存中所以它可以在以后重复使用)而不是一遍又一遍地做同样的事情。仅此一项就可以节省大量时间。

长时间运行的操作应该是异步的,这样您的 UI 就不会卡住。

您可以做的另一件事是将数据库代码转移到后台线程,这样您的应用程序的 UI 就不会卡住。

假设您的数据库INSERT 循环当前由按钮doWorkButton 触发。所以 for 循环在按钮 Click 事件处理程序中:

private void doWorkButton_Click(object sender, EventArgs e)
{

for (i = 0; i < 2000000; i++)
{
Data = …
ExecuteQuery(Data);
// note: I leave it as an exercise to you to combine the
// above suggestion (parameterized queries) with this one.
}
}

我们做的第一件事是通过三种方式更改您的 ExecuteQuery 方法:

  1. 使其异步。它的返回类型将是 Task 而不是 void,并且它的声明使用 async 关键字进行扩充。

  2. 将其重命名为 ExecuteNonQueryAsync 以反射(reflect)两件事:它现在将是异步的,并且它不执行查询。我们实际上并不期望从数据库 INSERT 中取回结果。

  3. 我们重写方法主体以使用 ADO.NET 异步方法而不是同步方法。

这就是它最终可能的样子:

private async Task ExecuteNonQueryAsync(string commandText)
{
using (var connection = new SqlConnection(connectionString)
using (var command = new SqlCommand(commandText, connection))
{
connection.Open();
/* int rowsAffected = */ await command.ExecuteNonQueryAsync();
connection.Close();
}
}

就是这样。现在我们需要修改处理程序方法以使其也异步:

private async void doWorkButton_Click(object sender, EventArgs e)
{
try
{

for (i = 0; i < 2000000; i++) { … }
{
Data = …
await ExecuteNonQueryAsync(Data);
}
}
catch
{
… // do not let any exceptions escape this handler method
}
}

这应该处理 UI 卡住业务。

使用 SqlBulkCopy 可以更高效地完成数百万个 INSERT 语句。

SQL Server 具有用于批量插入数据的绝妙选项。使用此功能通常会比执行您自己的 INSERT 语句批处理获得更好的性能。

private async void doWorkButton_Click(object sender, EventArgs e)
{
// Prepare the data to be loaded into your database table.
// Note, this could be done more efficiently.
var dataTable = new DataTable();
{
dataTable.Columns.Add("Field1", typeof(int));
var rnd = new Random();
for (int i = 0; i < 12000000; ++i)
{
dataTable.Rows.Add(rnd.Next(0, 2000000));
}
}

using (var connection = new SqlConnection(connectionString))
using (var bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName = "dbo.Table1";
try
{
// This will perform a bulk insert into the table
// mentioned above, using the data passed in as a parameter.
await bulkCopy.WriteToServerAsync(dataTable);
}
catch
{
Console.WriteLine(ex.Message);
}
}
}

我现在无法测试,但我希望这能让你开始。如果您想进一步改进基于 SqlBulkCopy 的解决方案,我建议您创建一个 IDataReader 的自定义实现,它创建包含动态随机数据的“行” ,然后将其实例传递给 SqlBulkCopy 而不是预填充的数据表。这意味着您不必在内存中保留一个巨大的数据表,而一次只保留一个数据行。

关于c# Win Form - CLR 无法从 COM 上下文转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40692213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com