473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Executing a process with a timeout

I have a bit of "login authentication" code that in the event of a server
error, connection error, etc will take a Looooooong time to timeout. I
wanted to redesign it to call the DAL method in a separate thread (easy
enough) w/ a callback for when it's done. The area I'm having trouble with
is once I determine the timeout period has elapsed, how do I kill the thread
safely?

I asked a threading question here awhile ago and some kind soul pointed me
to Mr' Skeet's great articles. I learned a lot, but apparently not enough
to handle this problem. Jon suggests in his articles that you check a flag
"stopping" to determine if the user has requested the operation be
cancelled, but this requires you to have the opportunity to evaluate the
value of "stopping" - in my case, I make a call to DAL.AuthenticateUser()
and if that method has trouble... well, my execution on the worker thread
sits there waiting for it to return. no chance to check if the timeout
monitor has set "stopping" true or the user has pressed cancel.

I must be thinking about this wrong. As I type this, it seems like my
problem is the main reason people use threads ;0)

I'm trying to do something like this:
User clicks the "Login" button on the form.
Form click event handler calls my AuthMgr.AuthenticateUser(string username)
method
AuthMgr.AuthenticateUser(string username) starts new thread and passes
DAL.AuthenticateUser(string username) as the proc
AuthMgr.AuthenticateUser(string username) starts another thread to monitor
the time elapsed and fire an event (or make a method call) when that happens
at which point I would want to cancel the thread that is servicing the
DAL.AuthenticateUser() method.

problem is, the call to DAL.AuthenticateUser() is hung up on a
DbConnection.Open() call or a DataAdapter.Fill() method and I can't cleanly
kill the thread.

I hope that makes sense to someone out there that has a little time to shed
some light on this problem for me.

Thanks for reading,
Steve

Jun 6 '06 #1
1 4723
On Tue, 6 Jun 2006 10:42:56 -0700, Steve wrote:
I have a bit of "login authentication" code that in the event of a server
error, connection error, etc will take a Looooooong time to timeout. I
wanted to redesign it to call the DAL method in a separate thread (easy
enough) w/ a callback for when it's done. The area I'm having trouble with
is once I determine the timeout period has elapsed, how do I kill the thread
safely?
You don't.
I asked a threading question here awhile ago and some kind soul pointed me
to Mr' Skeet's great articles. I learned a lot, but apparently not enough
to handle this problem. Jon suggests in his articles that you check a flag
"stopping" to determine if the user has requested the operation be
cancelled, but this requires you to have the opportunity to evaluate the
value of "stopping" - in my case, I make a call to DAL.AuthenticateUser()
and if that method has trouble... well, my execution on the worker thread
sits there waiting for it to return. no chance to check if the timeout
monitor has set "stopping" true or the user has pressed cancel.


In an ideal world, your DAL would provide a way to start the authentication
process asynchronously with an optional timeout and a way to cancel the
authentication if it takes too long. Obvioulsly, this is not the case and
you have to do with just the synchronous AuthenticateUser() method which
might take ages to execute if the authentication server is unavailable. In
this case, the only solution to make it asynchronous is to do what you've
already done: start a thread and call AuthenticateUser() in this new thread
(using the thread pool wouldn't be a good idea in this case).

Then you want a way to cancel the operation. Quite simply: you can't since
your DAL doesn't provide you with a way to do that. Of course, you could
somehow kill the thread in which AuthenticateUser() executes but that would
be a bad idea since AuthenticateUser() might have acquired resources that
would not be properly released when the thread is killed causing a resource
leak. What you can do though is to simply set a flag when the operation
times out or when the user clicks Cancel. Then, when AuthenticateUser()
returns (or fails), check the flag. If the flags hasn't been set, this
means that the operation hasn't been canceled, so you can invoke the
callback method. If the flag has been set, this means that the operation
has been canceled so do not invoke the callback method and forget about the
results.

This method doesn't actually cancel anything. It simply forgets about the
operation if the user has chosen to cancel it but the operation keeps
running in the background. This is not really a problem if
AuthenticateUser() is just waiting for a dead server to answer. Of course,
there will be a thread running there for nothing until AuthenticateUser()
finally returns but unless the user starts 1000s of authentications per
seconds, this probably won't affect your system's resources or
performances. If AuthenticateUser() does some processor or bandwidth heavy
processing, then forgetting about it and letting it run in the background
will probably be a pretty bad idea but in this case, the only solution to
your problem will be to modify the DAL so that it supports cancelation.
Jun 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
1
by: Ville Huovinen | last post by:
Platform: Windows 2003 Server (MS SQL Server 2003 SP3) Language: C# Problem: My stored procedures times out randomly, some proces works fine when using them from C#, and some generate...
9
by: Hasani \(remove nospam from address\) | last post by:
I have a website with 2 aspx pages Foo.aspx, and bar.aspx The content of both files is //in 1 file Hello <%=Session.ToString()%> ================
2
by: Cédric Rossé | last post by:
Hello, I'm currently developping a web application and I'm trying to execute a batch file (.cmd) on the server side when the client presses a button on a web page. When I do this on my local...
1
by: Cédric Rossé | last post by:
Hello, I'm currently developping a web application in C# / ASP.net. On a specific form of my application, I have a simple button which calls the following code : Process myProcess; myProcess...
4
by: Siang Hwee | last post by:
Hi. I am facing some problem in Asp.net. I am developing a payroll application currently. This is a web-based application. User need to click on "Process" button in order to process the payroll...
2
by: Alexander van Doormalen | last post by:
I am trying to run an external process (command tool) within a Windows Service with the specified account. When the application launches I get a error dialog with the following message: ...
5
by: Benzi Eilon | last post by:
I have written a C# application which should run as a Windows Service. I must avoid having multiple instances of the application on one machine so I inserted the following code at the beginning of...
1
by: Chrace | last post by:
Hi all, I have a problem with with Thread.Join( Timeout ) where the timeout never occurs. I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.