【发布时间】:2011-07-31 11:06:32
【问题描述】:
这是来自 Microsoft(MSDN) (build sql dependency application) 的代码示例,请你帮我把这段代码从 c# 翻译成 C++/CLI,我一直在尝试,但我不太擅长 c++。
private void dependency_OnChange(
object sender, SqlNotificationEventArgs e)
{
// This event will occur on a thread pool thread.
// Updating the UI from a worker thread is not permitted.
// The following code checks to see if it is safe to
// update the UI.
ISynchronizeInvoke i = (ISynchronizeInvoke)this;
// If InvokeRequired returns True, the code
// is executing on a worker thread.
if (i.InvokeRequired)
{
// Create a delegate to perform the thread switch.
OnChangeEventHandler tempDelegate =
new OnChangeEventHandler(dependency_OnChange);
object[] args = { sender, e };
// Marshal the data from the worker thread
// to the UI thread.
i.BeginInvoke(tempDelegate, args);
return;
}
// Remove the handler, since it is only good
// for a single notification.
SqlDependency dependency =
(SqlDependency)sender;
dependency.OnChange -= dependency_OnChange;
// At this point, the code is executing on the
// UI thread, so it is safe to update the UI.
++changeCount;
label1.Text = changeCount;
}
【问题讨论】:
-
这是一段更大的代码的一部分;我不建议您将其全部发布,但不可能将其单独转换并使其有意义!
-
纯 C++ 还是 C++/CLI?如果不使用额外的库,这在简单的 C++ 中是不可能的。
-
您能否提供您获取此代码的链接
-
是的,进入 c++/cli,这里是 microsoft msdn.microsoft.com/en-us/library/a52dhwx7%28v=vs.80%29.aspx的原始链接
-
为什么不直接在 C# 中编译它并从 C++/CLI 中引用该程序集?
标签: c# c++-cli code-translation