473,320 Members | 1,953 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.

Setting timeout for methods

Hi,
I want to implement a method which will poll database for messages.
But i want to provide a method that will specify a timeout until which
it will be polled and after that it will throw an exception. I tried
the following code.

Here say for example GetString in class test is a method for which i
want to provide timeout. Class1 contains the console application's main
method

using System;
using System.Threading;

using System;
using System.Threading;

namespace ConsoleApplication6
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
test t1 = new test();

try
{

string s = t1.GetString( 100, new TimeSpan( 1000 ) );

Console.WriteLine( s );
}
catch( Exception ex )
{
Console.WriteLine( "handled " + ex.Message );
}
//
// TODO: Add code to start application here
//
}
}

class test
{
public string GetString( int count )
{
string s = "";
for( int i = 0; i < count; i++ )
Console.WriteLine(i);

return s;
}

public string GetString( int count, TimeSpan timeout )
{
Timer tmr = new Timer( new TimerCallback( Timedout ), null,
(long)timeout.TotalMilliseconds, Timeout.Infinite );

return GetString( count );
}

void Timedout( object state )
{
throw new ApplicationException("Time out" );
}
}

}
The problem with the above code is control does not come to catch block
in the main method. Where is the exception thrown? When you run the
sample you get the message exception in the console window but the
method still continues. I want to the stop the method execution when
the exception occurs. Can somebody help?
Thanks & Regards,
Arun Prakash

Nov 16 '05 #1
1 3335

ArunPrakash wrote:
Hi,
I want to implement a method which will poll database for messages.
But i want to provide a method that will specify a timeout until which it will be polled and after that it will throw an exception. I tried
the following code.

Here say for example GetString in class test is a method for which i
want to provide timeout. Class1 contains the console application's main method

using System;
using System.Threading;

using System;
using System.Threading;

namespace ConsoleApplication6
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
test t1 = new test();

try
{

string s = t1.GetString( 100, new TimeSpan( 1000 ) );

Console.WriteLine( s );
}
catch( Exception ex )
{
Console.WriteLine( "handled " + ex.Message );
}
//
// TODO: Add code to start application here
//
}
}

class test
{
public string GetString( int count )
{
string s = "";
for( int i = 0; i < count; i++ )
Console.WriteLine(i);

return s;
}

public string GetString( int count, TimeSpan timeout )
{
Timer tmr = new Timer( new TimerCallback( Timedout ), null,
(long)timeout.TotalMilliseconds, Timeout.Infinite );

return GetString( count );
}

void Timedout( object state )
{
throw new ApplicationException("Time out" );
}
}

}
The problem with the above code is control does not come to catch block in the main method. Where is the exception thrown? When you run the
sample you get the message exception in the console window but the
method still continues. I want to the stop the method execution when
the exception occurs. Can somebody help?
Thanks & Regards,
Arun Prakash


This is not ideal, but you could create a timeout
handler (needs to be a class probably) that knows
which thread times out (pass Thread.Current when
you create the timeout handler.) In the handler,
instead of throwing an exception, call the Abort()
method on the thread.

Jose Solorzano

Nov 16 '05 #2

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

Similar topics

11
by: Frank Rizzo | last post by:
Hello, My c# based windows service takes a while to dispose. I have to release bunch of resources all over the place and unfortunately it can take 20-40 seconds before I can cleanly exit. ...
1
by: Scott Walters | last post by:
Hi, I have an asp.net webapp that uses some javascript in a hidden frame on the browser to poll the server every 30 seconds, using an http post. I also want my sessions to timeout in the...
4
by: UJ | last post by:
I have a page where the user can upload a video file. As you can guess, this may take a while. Is there a way I can change the session timeout for just this one page? I would also want to change...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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.