Перевести MSDN пример с C # на C ++ / CLI - PullRequest
1 голос
/ 07 апреля 2011

это пример кода от Microsoft (MSDN) (сборка приложения зависимостей sql) , не могли бы вы помочь мне перевести этот код из 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;

}

Ответы [ 2 ]

3 голосов
/ 08 апреля 2011

Вот моя быстрая попытка:

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();

}
1 голос
/ 08 апреля 2011

Скомпилируйте пример исходного кода C #, затем используйте Reflector , чтобы декомпилировать сборку в MC ++ (управляемый C ++).

...