Re: Setting timeout for methods

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Jose Solorzano (jhsolorz_at_hotmail.com)
Date: 12/28/04


Date: 28 Dec 2004 10:01:22 -0800


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



Relevant Pages

  • Setting timeout for methods
    ... it will be polled and after that it will throw an exception. ... want to provide timeout. ... Class1 contains the console application's main ... public string GetString(int count, TimeSpan timeout) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Exceptions take too long
    ... exception class in order to notify that the timeout for acquiring the mutex ... Rewritten w/out exception, ... ULONG getError ... virtual BOOL Unlock ...
    (microsoft.public.windowsce.embedded.vc)
  • RE: Error codes for sql server?
    ... //For admin killing process ... //For timeout ... What you will find is the same exception is thrown each time: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Set application timeout to more than 3 minutes
    ... I didnt mention that I already set the SQL server query timeout to 600 ... Here is the detailed exception: ... Server Error in '/YZA_Remote' Application. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Timeout expired. The timeout period elapsed prior to completion of the operation or the server
    ... Ther are two types of timeout exception that could be raised by SqlClient objects: ... SqlCommand's Timeout decides how long time a command is given for the application that uses the SQlCommand to wait for SQL Server to finish the operation invoced by the SqlCommand. ... System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) ...
    (microsoft.public.dotnet.framework.adonet)