views:

177

answers:

5

I am using the Process class in my application, right at the beginning of the static Main method. I am using the Process class to know if another instance of my application is currently running. If my application is already running, I will exit. i am not looking for a way of change this, this has been working for years

But recently this is not working on some computers of our users, I get the error "Common Language Runtime Debugging Services : Application has generated an exception that could not be handled".

I already fullstrust the assembly and the user is an administrator of the system.

any ideas will help Thanks!!

Edit: The WMI Performance Adapter Service is turned ON before the error is thrown and OFF after the error occurred, am i missing some kind of permission?.

this is on Framework 1.1

+1  A: 

Could you add a toplevel try catch (or use the UnhandledException handler) to see what kinf of Exception is thrown? This would be of great help.

Brann
+3  A: 

While I can't speculate why your process crashes, a better solution would be to use a named mutex:

static void Main()
{
  bool mutexWasCreated;
  using (Mutex mutex=new Mutex(true, AppID, out mutexWasCreated))
  {
    // check for single instance
    if (!mutexWasCreated)
      return;

    Application.Run(...);
  }
}
chris
i am not looking for a way of changing this, this has been working for years, i am curiuos why Process can raise a security Exception, Thanks
Oscar Cabrero
@Oscar : how do you know it's a security exception?
Brann
Honestly I agree with Chris. You're doing it the wrong way. Fix the problem at the heart, and you'll not need to worry. You also won't have to vary trusts and permissions to execute it.
Ian
A: 

I am using the Process class to know if another instance of my application is currently running.

.Net provides the StartupNextInstance application event for this purpose.

Hmm... never mind. This looks like it's only available from Visual Basic. I'll leave the answer here in case it's useful to someone else.

Joel Coehoorn
this is only for .net 2.0 and up this app was build in 1.1
Oscar Cabrero
VisualBasic.ApplicationServices seems sort of poorly supported. And if you need Windows Logo certification you should forget about it. See the comments here: http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx
0xA3
+1  A: 

I agree with Brann. It's hard to troubleshoot if you don't know what exception is being thrown. I'd listen to the unhandledexception event and log the issue as the first step in troubleshooting this.

Aaron Daniels
+1  A: 

Hi - I remember having a similar problem with some .net code that I was working on. It was in a citrix environment.

The user had to be a member of the performance group I believe, to be able to run Process.GetProcessesByName()... This isn't desirable really!

I think that we had two work arounds - the first was at the start of the code, and that was to run the code from a VB script, which tested to see whether the code was running already or not.

The second work around was in part of the code which set the window's focus. Here, we just run AppActivate() within a try / catch block - which I guess you might be able to do - if AppActivate fails, then you need to run the application.

Hope this helps!

Mark
this appears a path to the real problem (GetProcessesByName()) thanks!!!
Oscar Cabrero