Odd "Unhandled Exception" error

  • Thread starter Thread starter Paul Steele
  • Start date Start date
P

Paul Steele

I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length

This works find on all of my systems, but a student using one of my programs
reported that he was getting an "Unhandled Exception" trying to load my
program. This is even stranger since the code in question is in a try/catch
block. I took a look at the student's system but couldn't find any
immediately obvious that would cause this problem. The fact that it's
happening with two different applications indicates that there is some sort
of problem with his system. I suspect the .NET Framework but a reinstall of
that didn't help. Is there some other system process that would cause this
error to occur? I suspect a reinstall of Windows would solve the problem but
there has to be something a bit less drastic. Any suggestions would be
appreciated.
 
Paul said:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length

This works find on all of my systems, but a student using one of my programs
reported that he was getting an "Unhandled Exception" trying to load my
program.


This is even stranger since the code in question is in a try/catch block.
[How could you be sure on this if you encounter an unhanded exception,
this seems contradictory to me]

I took a look at the student's system but couldn't find any
 
Try this one instead:

// This will be set to true if this process can own the mutex.
bool pobjIOwnMutex = false;

// Try and get ownership of a mutex who's name is known.
System.Threading.Mutex pobjMutex = new System.Threading.Mutex(true,
"MyMutex", out
pobjIOwnMutex);

// If the mutex is owned by this process, then run the application.
if (pobjIOwnMutex)
{
// Run the application.
Application.Run(new frmMain());
}
else
{
//exit application
}

This works always.

Regards Alexander
John Sun said:
Paul said:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length

This works find on all of my systems, but a student using one of my programs
reported that he was getting an "Unhandled Exception" trying to load my
program.


This is even stranger since the code in question is in a try/catch block.
[How could you be sure on this if you encounter an unhanded exception,
this seems contradictory to me]

I took a look at the student's system but couldn't find any
immediately obvious that would cause this problem. The fact that it's
happening with two different applications indicates that there is some sort
of problem with his system. I suspect the .NET Framework but a reinstall of
that didn't help. Is there some other system process that would cause this
error to occur? I suspect a reinstall of Windows would solve the problem but
there has to be something a bit less drastic. Any suggestions would be
appreciated.
 
John Sun said:
Paul said:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length

This works find on all of my systems, but a student using one of my
programs reported that he was getting an "Unhandled Exception" trying to
load my program.


This is even stranger since the code in question is in a try/catch block.
[How could you be sure on this if you encounter an unhanded exception,
this seems contradictory to me]

I don't disagree, but if I comment out the code I no longer get the error.
In either program. It's very weird...
 
This may or may not work. Unfortunately just solving this problem won't be
sufficient. I didn't want to go into all the gory details but even if I
comment out the code causing the initial error, I get another strange error
later on in the program. I only had the student's system for 30 minutes so I
didn't get a chance to narrow down the cause of the second error (which
incidentally works fine on other students' systems). I forgot to make note
of the exception error, but it was something about not being able to start a
process (I use quite a few threads in the application). I suspect the second
error is related to the first error. Something very weird is going on...

Alexander Wehrli said:
Try this one instead:

// This will be set to true if this process can own the mutex.
bool pobjIOwnMutex = false;

// Try and get ownership of a mutex who's name is known.
System.Threading.Mutex pobjMutex = new System.Threading.Mutex(true,
"MyMutex", out
pobjIOwnMutex);

// If the mutex is owned by this process, then run the application.
if (pobjIOwnMutex)
{
// Run the application.
Application.Run(new frmMain());
}
else
{
//exit application
}

This works always.

Regards Alexander
John Sun said:
Paul said:
I often use the following code to check if a program is already
running:

if
(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length
1) ...

This works find on all of my systems, but a student using one of my
programs
reported that he was getting an "Unhandled Exception" trying to load my
program.


This is even stranger since the code in question is in a try/catch
block.
[How could you be sure on this if you encounter an unhanded exception,
this seems contradictory to me]

I took a look at the student's system but couldn't find any
immediately obvious that would cause this problem. The fact that it's
happening with two different applications indicates that there is some
sort
of problem with his system. I suspect the .NET Framework but a
reinstall of
that didn't help. Is there some other system process that would cause
this
error to occur? I suspect a reinstall of Windows would solve the
problem but
there has to be something a bit less drastic. Any suggestions would be
appreciated.
 
Paul said:
Paul said:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length
1) ...

This works find on all of my systems, but a student using one of my
programs reported that he was getting an "Unhandled Exception" trying to
load my program.


This is even stranger since the code in question is in a try/catch block.
[How could you be sure on this if you encounter an unhanded exception,
this seems contradictory to me]


I don't disagree, but if I comment out the code I no longer get the error.
In either program. It's very weird...
Hi, Paul

Won't it be more helpful if you could post more complete code, how did
you implement the try .. catch ..., which line of code you comment out
before and after you got an exception.
 
Jianwei Sun said:
Hi, Paul

Won't it be more helpful if you could post more complete code, how did you
implement the try .. catch ..., which line of code you comment out before
and after you got an exception.

Not sure that it will help, but here it is:

static void Main()
{
try
{
if
(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length
{
MessageBox.Show("The program is already running!");
}
else
{
Application.Run(new ExamProctorForm());
}
}
catch
{
MessageBox.Show("An error has occured!");
}
}

I change the if statement to "if (false)" and I don't get the exception...
 
Back
Top