473,783 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Authenticat eUser()
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.Authent icateUser(strin g username)
method
AuthMgr.Authent icateUser(strin g username) starts new thread and passes
DAL.Authenticat eUser(string username) as the proc
AuthMgr.Authent icateUser(strin g 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.Authenticat eUser() method.

problem is, the call to DAL.Authenticat eUser() is hung up on a
DbConnection.Op en() call or a DataAdapter.Fil l() 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 4744
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.Authenticat eUser()
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 AuthenticateUse r() 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 AuthenticateUse r() 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 AuthenticateUse r() executes but that would
be a bad idea since AuthenticateUse r() 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 AuthenticateUse r()
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
AuthenticateUse r() is just waiting for a dead server to answer. Of course,
there will be a thread running there for nothing until AuthenticateUse r()
finally returns but unless the user starts 1000s of authentications per
seconds, this probably won't affect your system's resources or
performances. If AuthenticateUse r() 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
4893
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) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
1
12208
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 SqlException which states that the server has timed out. The problem isn't in server, neither in stored procedures because they work fine in Sql Server's enterprise manager, but when calling from code they are slow and take about 60 seconds before...
9
2778
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
18057
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 machine (where I'm developping my application), no problem occurs. But when I put the web application on my Windows server 2003 server, I always get the following error message. Can someone help me ? Here is the config of my webconfig file :
1
1129
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 = new Process(); myProcess.StartInfo.UseShellExecute = false;
4
2735
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 every time. Now the problem is, if the processing time is less than 3600 seconds, everything will work fine. but when the processing time is more than 3600 seconds. A page cannot be displayed message will be shown on the browser. Any one know why?...
2
6443
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: Application popup: <COMMAND TOOL> - Application Error : The application failed to initialize properly (0xc0000142). Click on OK to terminate the application. I use the following code to start the process (not relevant code is stripped):
5
4491
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 the Main() function: // if this is not the first instance of this application, exit // immediately. Allow only one instance in the system if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
1
3307
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 gets a problem and the way this is handled on AS400 is by hanging. If I was to connect directly this would mean my main process would hang as well, so I've spawned a worker thread and used a Thread.Join( Timeout ) to make sure it always returns with...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.