【问题标题】:How to use .net System.Diagnostics::Process OnExited C++?如何使用.net System.Diagnostics::Process OnExited C++?
【发布时间】:2011-06-16 02:05:50
【问题描述】:

谁能举例说明如何在 C++ 中使用 OnExited 事件,请参阅我所指的链接表单 msdn。 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.onexited.aspx 。我真的对这个 C++/CLI 感到困惑。我试图在进程退出后删除一个文件。我让它在 C# 中工作,但更喜欢 C++,因此它可以更容易地为 JNI 包装。

【问题讨论】:

  • 您要将JVM 和CLR 加载到同一个进程中?可怕...

标签: .net events event-handling c++-cli


【解决方案1】:

我不确定真的你想要什么,所以这里是你链接到的页面上的代码从 C# 到 C++/CLI 的字面翻译:

using namespace System;
using namespace System::Diagnostics;

ref class MyProcess : public Process
{
public:
    void Stop()
    {
        this->CloseMainWindow();
        this->Close();
        OnExited();
    } 
};

void myProcess_HasExited(Object^ sender, EventArgs^ e)
{
    Console::WriteLine(L"Process has exited.");
}

int main(array<String^>^ args)
{
    MyProcess^ p = gcnew MyProcess();
    p->StartInfo->FileName = L"notepad.exe";
    p->EnableRaisingEvents = true;
    p->Exited += gcnew EventHandler(myProcess_HasExited);
    p->Start();
    p->WaitForInputIdle();
    p->Stop();
}

【讨论】:

  • 这正是我想要的,但我无法编译。获取错误。 1> test2.cpp 1>test2.cpp(20): error C2628: 'MyProcess' 后跟 'void' 是非法的(你忘记了';'吗?) 1>test2.cpp(30): error C3352: ' myProcess_HasExited' : 指定的函数与委托类型不匹配 'void (System::Object ^,System::EventArgs ^)' ========== Build: 0 成功,1 失败,0 到-date, 0 跳过 ==========
猜你喜欢
  • 2021-06-17
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多