【问题标题】:SocketException & IOException when opening new instances of an already running single instance application打开已经运行的单实例应用程序的新实例时出现 SocketException 和 IOException
【发布时间】:2015-01-22 02:39:33
【问题描述】:

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

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

初始化器.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.

是什么导致了这些异常以及如何处理它们?

【问题讨论】:

    标签: c++ winforms clr


    【解决方案1】:

    我认为您的 Visual Studio 调试器仍在运行,基本上没有正常终止,这可能是您可能仍然有一个线程在后台运行,它使套接字保持打开状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      相关资源
      最近更新 更多