Application hanging up when reboot

  • Thread starter Thread starter Mullin Yu
  • Start date Start date
M

Mullin Yu

hi,

i write a windows application (exe file) which mainly loop at the sql
server to do some operations.

but, when i restart the computer without really close it first, a dialog
always pops out to ask me quit the application. it's very annoying as it may
make my computer can't be rebooted remotely or schedulely that should be no
user intervention.

any ideas? and how to debug?

thanks!

regards,
mullin
 
I'll assume that your looping is occurring in a worker thread (if it isn't
it should be). You need to signal your worker thread to exit the loop so
the process will exit. Let's say your worker thread function looks
something like this:

void WorkerThread()
{
while (true)
{
// do sql stuff...
}
}

Currently there is no way for the thread that executes this function to
terminate in a normal way since the 'while' clause will always evaluate to
'true'. One way to solve this problem is to replace the 'true' with a
member variable from your main form class that can signal the thread to
exit the loop.

void WorkerThread()
{
while (!this.finished)
{
// do sql stuff...
}
}

In your form constructor, this.finished would be initialized to false since
our thread is not finished yet. We also need to add a syncronization
object to our form to syncronize access to this.finsihed:

private object syncObject;

and initialize it in the constructor:

this.syncObject = new object();

Now, in the form closing event, signal the worker thread that the app is
exiting:

lock (this.syncObject)
{
this.finished = true;
}

Now the while loop will end, your worker thread will exit, and the process
will terminate.

hth

-Joel

--------------------
 
Now that I think about it, it might be preferable to use a ManualResetEvent
instead of the bool to signal the worker thread to exit. It also
eliminates the need for the syncObject. Here's the modified code:

// private class member;
ManualResetEvent finished;

// in constructor
this.finished = new ManualResetEvent(false); // set to non-signaled

// worker thread function
void WorkerThread()
{
while (!this.finished.WaitOne(0, false)) // loop while the event is
non-signaled
{
// do sql stuff...
}
}

// in form close event handler
this.finished.Set(); // signal the event

--------------------
X-Tomcat-ID: 743412820
References: <#[email protected]>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: (e-mail address removed) (Joel Hendrix [MSFT])
Organization: Microsoft
Date: Thu, 01 Apr 2004 18:43:54 GMT
Subject: RE: Application hanging up when reboot
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Lines: 67
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:234474
NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122

I'll assume that your looping is occurring in a worker thread (if it isn't
it should be). You need to signal your worker thread to exit the loop so
the process will exit. Let's say your worker thread function looks
something like this:

void WorkerThread()
{
while (true)
{
// do sql stuff...
}
}

Currently there is no way for the thread that executes this function to
terminate in a normal way since the 'while' clause will always evaluate to
'true'. One way to solve this problem is to replace the 'true' with a
member variable from your main form class that can signal the thread to
exit the loop.

void WorkerThread()
{
while (!this.finished)
{
// do sql stuff...
}
}

In your form constructor, this.finished would be initialized to false since
our thread is not finished yet. We also need to add a syncronization
object to our form to syncronize access to this.finsihed:

private object syncObject;

and initialize it in the constructor:

this.syncObject = new object();

Now, in the form closing event, signal the worker thread that the app is
exiting:

lock (this.syncObject)
{
this.finished = true;
}

Now the while loop will end, your worker thread will exit, and the process
will terminate.

hth

-Joel

--------------------
From: "Mullin Yu" <[email protected]>
Subject: Application hanging up when reboot
Date: Fri, 26 Mar 2004 09:50:40 +0800
Lines: 18
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 202.130.116.106
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP
0
8.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:232418
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

hi,

i write a windows application (exe file) which mainly loop at the sql
server to do some operations.

but, when i restart the computer without really close it first, a dialog
always pops out to ask me quit the application. it's very annoying as it may
make my computer can't be rebooted remotely or schedulely that should be no
user intervention.

any ideas? and how to debug?

thanks!

regards,
mullin
 
Back
Top