Translate MSDN example from C# to C++/CLI

大城市里の小女人 提交于 2019-12-24 09:58:55

问题


this is a code sample from Microsoft(MSDN) (build sql dependency application) , could you please help me translate this code from c# into C++/CLI, I've been trying I, but I'm not really good in 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;

}

回答1:


Here's my quick attempt:

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:


Compile the C# sample source code, then use Reflector to decompile the assembly to MC++ (managed C++).



来源:https://stackoverflow.com/questions/5577644/translate-msdn-example-from-c-sharp-to-c-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!