SocketException&打开已运行的单实例应用程序的新实例时发生IOException

时间:2015-01-22 02:39:33

标签: c++ winforms clr

我正在开发一个CLR C ++项目,它在运行单一实例时可以正常工作。 但是当我调试从visual studio运行程序然后从项目调试文件夹运行已编译的可执行文件时,我得到SocketIO例外,应用程序不会崩溃并且它会保持正常运行。

我使用以下类启用单个实例:

initializer.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.IOException:

Additional information: The read operation failed, see inner exception.

If there is a handler for this exception, the program may be safely continued.

导致这些异常的原因以及如何处理它们?

1 个答案:

答案 0 :(得分:-1)

我认为你的visual studio调试器仍在运行,基本上没有正常终止,可能是你仍然有一个在后台运行的线程正在打开套接字。