【问题标题】:Translate MSDN example from C# to C++/CLI将 MSDN 示例从 C# 转换为 C++/CLI
【发布时间】: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


【解决方案1】:

这是我的快速尝试:

private void dependency_OnChange(
   System::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 = 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 =
            gcnew OnChangeEventHandler(this, &Form1::dependency_OnChange);

        cli::array<System::Object^>^ args = gcnew cli::array<System::Object^>(2);
        args[0] = sender;
        args[1] = 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 = safe_cast<SqlDependency^>(sender);

    dependency->OnChange -= gcnew OnChangeEventHandler(this, &Form1::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.ToString();

}

【讨论】:

    【解决方案2】:

    编译 C# 示例源代码,然后使用 Reflector 将程序集反编译为 MC++(托管 C++)。

    【讨论】:

    • MC++ != C++/CLI。忘记曾经存在的 C++ 托管扩展。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2015-04-15
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多