By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,812 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,812 IT Pros & Developers. It's quick & easy.

Odd "Unhandled Exception" error

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

if
(Process.GetProcessesByName(Process.GetCurrentProc ess().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. 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.
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Paul Steele wrote:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProc ess().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.

Nov 16 '05 #2

P: n/a
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" wrote:
Paul Steele wrote:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProc ess().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.

Nov 16 '05 #3

P: n/a

"John Sun" <js***********@gmail.com> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
Paul Steele wrote:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentProc ess().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...
Nov 16 '05 #4

P: n/a
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" <Al*************@discussions.microsoft.com> wrote in
message news:66**********************************@microsof t.com...
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" wrote:
Paul Steele wrote:
> I often use the following code to check if a program is already
> running:
>
> if
> (Process.GetProcessesByName(Process.GetCurrentProc ess().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.
>
>

Nov 16 '05 #5

P: n/a
Paul Steele wrote:
"John Sun" <js***********@gmail.com> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
Paul Steele wrote:
I often use the following code to check if a program is already running:

if
(Process.GetProcessesByName(Process.GetCurrentP rocess().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.
Nov 16 '05 #6

P: n/a
"Jianwei Sun" <js***********@gmail.com> wrote in message
news:uN**************@tk2msftngp13.phx.gbl...
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.


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

static void Main()
{
try
{
if
(Process.GetProcessesByName(Process.GetCurrentProc ess().ProcessName).Length 1)

{
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...
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.