【发布时间】:2015-01-22 02:39:33
【问题描述】:
我正在开发一个 CLR C++ 项目,它在运行单个实例时可以正常工作。
但是,当我从 Visual Studio 调试运行程序,然后从项目调试文件夹运行编译后的可执行文件时,我得到 Socket 和 IO 异常,应用程序不会崩溃,它会继续正常运行。
我正在使用以下类来启用单个实例:
初始化器.h
#pragma once
namespace Project2
{
public ref class SingleApplication: public Microsoft::VisualBasic::ApplicationServices::WindowsFormsApplicationBase
{
protected:
virtual void OnCreateMainForm() override;
protected:
~SingleApplication ()
{
}
public:
SingleApplication (void);
System::Void StartNextInstance (System::Object ^sender, Microsoft::VisualBasic::ApplicationServices::StartupNextInstanceEventArgs ^e);
};
}
initializer.cpp
#include "initializer.h"
#include "MyForm.h"
using namespace Project2;
using namespace System;
using namespace System::Windows::Forms;
using namespace Microsoft::VisualBasic::ApplicationServices;
SingleApplication::SingleApplication (void)
{
this->IsSingleInstance = true;
this->EnableVisualStyles = true;
this->StartupNextInstance += gcnew
StartupNextInstanceEventHandler (this, &SingleApplication::StartNextInstance);
}
Void SingleApplication::StartNextInstance (Object ^sender, StartupNextInstanceEventArgs ^e)
{
MyForm ^form = safe_cast<MyForm ^> (this->MainForm);
if (form->IsMinimizedToSystemTray()) form->MakeVisible();
else if (form->WindowState == FormWindowState::Minimized)
{
form->Show();
form->WindowState = FormWindowState::Normal;
}
else form->BringToFront();
form->Focus();
}
void SingleApplication::OnCreateMainForm()
{
this->MainForm = gcnew MyForm();
}
入口点:
[STAThread]
int main (array<String ^> ^argv)
{
SingleApplication ^MyApplication = gcnew SingleApplication();
MyApplication->Run (argv);
return 0;
}
调试日志:
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll
启用异常中断后,我得到了以下详细信息:
1.SocketException:
Additional information: An existing connection was forcibly closed by the remote host
If there is a handler for this exception, the program may be safely continued.
2.IO异常:
Additional information: The read operation failed, see inner exception.
If there is a handler for this exception, the program may be safely continued.
是什么导致了这些异常以及如何处理它们?
【问题讨论】: