I was making a program that I wanted only 1 copy to be running at a time.
If its multi instance, I can run it over and over.
If its single instance, instead of letting just 1 copy run, whenever a second copy is launched, both it and the first copy CRASH with some fatal exception error.
I'm simply checking off the "Make single instance application" option under the "Windows application framework properties" section.
Why does this make the program crash

Single Instance application crashes with an exeption...?
Pazu Choi
Hi,
Would you please post more details about the fatal exception you got That will help investigating this issue.
Also, it's great if you can try this:
- Click on 'View Application Events' button below the "Make single instance application" check box.
- Add the following code:
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
MsgBox("Second instance attempted")
End Sub
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledExceptionMsgBox(e.Exception.ToString)
End SubTry running your program again and see if you can get the full error message.
Best regards,
chrstdvd
I get the has encountered a problem and needs to close box , with a other msgbox uder that .
Has someone worked how to stop it crashing and pass a command line to the second instance
It sounds like it shouldn`t be very complex , but it is !!!!
Jyoti Ranjan
But before I do I’ve expend on my old problem and answer a couple of points.
1/ handler code was in application.event from net doctor
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e as.vbMicrosoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
MsgBox("StartupNextInstance")
End Sub
I can`t tell you what the error was totally a win32 error, I think in nt.dll
My work around is to make a starter module with :-
Module Module1
Public Sub main()
Dim owned As Boolean
Dim mut As New System.Threading.Mutex(True, "tryagain", owned)
If owned Then
MsgBox("Before")
Application.Run(New Form1)
MsgBox("Exit")
mut.ReleaseMutex()
Else
MsgBox("repeat")
End If
End Sub
End Module
You have to switch off application framework to see the sub main !!!!
Do you get a crach if you enable single instances on a blank form the second instance .
mcfs
I've reviewed the detail in the links you have posted.
However I am not using NOD32 AV software. I have also tried to do a clean boot of XP which would load the absolute minimum of services on startup and I still get the problem. One of the suggestions in the posts detailed above is to put a pause of 15 seconds into the application just before it terminates. This does improve matters although still doesn't resolve them as if you continually double click on the application icon it will crash after a few launches.
I've included some C# code - but the problem is not with the source code but with something underlying environment - IE the .net framework, remoting, XP or hardware.
I'm going to raise this as a support incident with Microsoft. Will post back here as and when I get progress on that front.
Regards
Peter
SingleInstanceApplication.cs>>>>>>>>>>>>>
using Microsoft.VisualBasic.ApplicationServices;
namespace SingleInstanceSample
{
// SingleInstanceApplication.cs
internal class SingleInstanceApplication : WindowsFormsApplicationBase
{
private static SingleInstanceApplication application;
internal static SingleInstanceApplication Application
{
get
{
if (application == null)
{
application = new SingleInstanceApplication();
}
return application;
}
}
// Must call base constructor to ensure correct initial
// WindowsFormsApplicationBase configuration
public SingleInstanceApplication()
{
// This ensures the underlying single-SDI framework is employed,
// and OnStartupNextInstance is fired
this.IsSingleInstance = true;
}
protected override void OnCreateMainForm()
{
this.MainForm = new MainForm();
}
}
}
Program.cs>>>>>>>>>>>>>>>>>>>>>>>>
using System;
using System.Windows.Forms;
namespace SingleInstanceSample
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SingleInstanceApplication.Application.Run(args);
}
}
}
mrobichaud
Hi,
I found this feedback with the same call stack you got: http://connect.microsoft.com/feedback/ViewFeedback.aspx FeedbackID=93618&SiteID=210. The problem seems to be with NOD32 anti virus program (see http://connect.microsoft.com/feedback/Workaround.aspx FeedbackID=93618&SiteID=210). Would you please verify if it solves your issue
You mentioned that you can reproduce the problem with C# code as well. If the issue persists, please simplify the C# code as much as you can to emphasize the issue and submit a feedback through https://connect.microsoft.com/site/sitehome.aspx SiteID=210. Based on the call stack I think this has more to do with remote call than our application framework code.
Thank you for sharing great information, it really helps.
Best regards,
Harald Kjersen
Thanks Huy for your reply.
I've already contacted Panda about this problem and they are looking into it. I will send them your reply as well.
I understand you can't fixed this, but yes, some improvement will be good. Now the application is just crashing and you don't know why. Take note that the same problem also occures with C# applications, so it's not only VB applications.
Kind regards,
Niels
Rajiv Srinivasan-MSFT
Hi Peter,
Do you have the exception type Have you tried the steps suggested above to get the exception type
If the steps above could not get the exception type, are you using remoting with TCP in your program The application framework uses TCP for communication between second instance and first instance. This may lead to exception as well.
It would be great if you can share your code. Feel free to email me as well at Huy.Nguyen at microsoft.com.
Best regards,
Derwood
I have trawled the net and also found that there are other who have similar problems - although it is not such a common problem that the web is littered with similar complaints. With Micahel Weinhardt's assistance we identified that the problem is not with the code, nor the .Net Framework directly but indirectly as a result of something to do with the machine. What the source of the problem is I do not yet know. If I take the compiled executables and move them to another machine they work fine and a single instance of the application runs.
The problem remains and the mutex approach is a PARTIAL workaround which avoids the SingleInstance setting in VB or using the Microsoft.VisualBasic.Application code from a C# app. It is only a partial solution because you have no way of communicating with the existing running instance (EG to tell it to do something when a second instance loads). To add this functionality you then need to create a socket listner and communicate via sockets - however this also introduces security concerns and is the real reason to use the inbuilt single instance support in Microsoft.VisualBasic.Application because MS have done all the hard work on these aspects for you.
If anyone can shed any light on what is causing this problem it would be very much appreciated. More details about it are:
If you run the first instance from explorer then when you lauch a second instance you will get a GPF error indicating that memory is corrupt.
If you do as above but step through the second instance in the VS debugger you will see that the second instance correctly identifies that an existing instance is running and the second instance will terminate normally. The error only occurs in the first instance but occurs as soon as the second instance terminates - no earlier, no later.
If you run the first instance in the VS debugger and the second instance from Explorer then you can catch the 'unhandled' exception in Visual Studio - Actually you rely on VS to catch it because even if you put a try..catch wrapper around all your code in Main() it still doesn't catch the exception and VS reports it as an unhandled exception. The exception details are as follows:
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System"
StackTrace:
at System.Net.UnsafeNclNativeMethods.OSSOCK.WSAGetOverlappedResult(SafeCl
os eSocket socketHandle, IntPtr overlapped, UInt32& bytesTransferred,
Boolean wait, IntPtr ignored)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UI
nt 32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UIn
t32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
lindylars
I installed VS .NET 2005 SP1 Beta, but this made no difference. So I guess the problem is with Framework 2.0. Is there a hotfix available for this problem
Qubic808
Hi,
I've received the report you opened on MS Connect as well.
Enabling subsequent instances to contact the first instance of a single instance application requires remote communication using TCP/IP protocol. The first instance will listen on an available port of localhost. The second instance will obtain this port and try to contact the first instance. Panda antivirus is blocking remoting on your machine (see http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=418569&SiteID=1).
This will affect not only VB Single Instance application but also any programs that use .NET Framework remoting. Can you contact Panda for support as well
AFAIK there is no hot fix for this. We may consider an improvement for VB Application Framework's Single Instance feature to let developer decide not to use remoting (hence subsequent instances will just exit).
Best regards,
Bayer
I am havins similiar problems, I would like to create a single instance application in c# (2005) Attempting to migrate my old vb6 code (or rewrite in c#) I had used this code
If Not App.PrevInstance Then
Dim fLogin As New frmLogin
fLogin.Show vbModal
If Not fLogin.OK Then
'Login Failed so exit app
End
End If
Unload fLogin
frmSplash.Show
frmSplash.Refresh
Set fMainForm = New frmMain
Load fMainForm
Unload frmSplash
fMainForm.Show
Else
MsgBox "Sorry, " & Trim((App.Title)) & " is already running...", vbCritical
End
End If
Can I use a similair construct in c# -- I noted that the vb.net allows this through application properties, alas c# does not.
bighead
Hi dDude,
VB and C# does not contain support for App.PrevInstance by default. To write single instance application in C# you can follow chapter 14 of "Windows Forms 2.0 Programming" book. http://www.sellsbrothers.com/writing/wfbook/ has the chapter in PDF file.
Best regards,
RobGibbens
hi, peter
you can check this link, about vb comumication between instances of your program
http://www.vbaccelerator.com/home/VB/Code/Libraries/Shell_Projects/Registering_File_Associations/article.asp
hope this helps