<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C# Set method timeout using Generics</title>
	<atom:link href="http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Tue, 31 Jan 2012 06:04:50 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: Paresh</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-32738</link>
		<dc:creator>Paresh</dc:creator>
		<pubDate>Sat, 21 Jan 2012 12:18:20 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-32738</guid>
		<description>Hi,

Thanks. This article is very useful.

I am getting compile time error for void return methods:

public static void Limex(Action A, int Timeout, out bool Completed)
{
      Limex(new Func(() =&gt; { A(); return null; }), Timeout, out Completed);
}

I have added above function. Here Func is generating error.  Below is the compile time error message:

Using the generic type &#039;System.Func&#039; requires 10 type arguments

Am I doing anything wrong here?

Can you please look into this?</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Thanks. This article is very useful.</p>
<p>I am getting compile time error for void return methods:</p>
<p>public static void Limex(Action A, int Timeout, out bool Completed)<br />
{<br />
      Limex(new Func(() => { A(); return null; }), Timeout, out Completed);<br />
}</p>
<p>I have added above function. Here Func is generating error.  Below is the compile time error message:</p>
<p>Using the generic type &#8216;System.Func&#8217; requires 10 type arguments</p>
<p>Am I doing anything wrong here?</p>
<p>Can you please look into this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zpreniurry</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-16263</link>
		<dc:creator>zpreniurry</dc:creator>
		<pubDate>Sun, 18 Sep 2011 04:59:20 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-16263</guid>
		<description>Hi! 
Very interesting name by the forum kossovsky.net 
 
This situation is familiar to me. Is ready to help. 
 
The possession of unlimited power will make a despot of almost any man. 
 
Buy-buy!</description>
		<content:encoded><![CDATA[<p>Hi!<br />
Very interesting name by the forum kossovsky.net </p>
<p>This situation is familiar to me. Is ready to help. </p>
<p>The possession of unlimited power will make a despot of almost any man. </p>
<p>Buy-buy!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xander</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-5065</link>
		<dc:creator>Xander</dc:creator>
		<pubDate>Wed, 24 Nov 2010 16:30:55 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-5065</guid>
		<description>Hi, David
You can overload it with something like this : 
 
        public static void Limex(Action A, int Timeout, out bool Completed)
        {
            Limex(new Func(() =&gt; { A();  return null; }), Timeout, out Completed);
        }
 

and call it as following :
 
        static void Test()
        {
                Thread.Sleep(5000);
        }

        Limex(Test, 100, out Completed);
</description>
		<content:encoded><![CDATA[<p>Hi, David<br />
You can overload it with something like this : </p>
<p>        public static void Limex(Action A, int Timeout, out bool Completed)<br />
        {<br />
            Limex(new Func(() =&gt; { A();  return null; }), Timeout, out Completed);<br />
        }</p>
<p>and call it as following :</p>
<p>        static void Test()<br />
        {<br />
                Thread.Sleep(5000);<br />
        }</p>
<p>        Limex(Test, 100, out Completed);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-5056</link>
		<dc:creator>David</dc:creator>
		<pubDate>Tue, 23 Nov 2010 19:50:40 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-5056</guid>
		<description>What about for calling void methods?</description>
		<content:encoded><![CDATA[<p>What about for calling void methods?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JOISPWISORM</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-4324</link>
		<dc:creator>JOISPWISORM</dc:creator>
		<pubDate>Mon, 16 Aug 2010 12:09:26 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-4324</guid>
		<description>i&#039;m new... promise to post around more time after time!</description>
		<content:encoded><![CDATA[<p>i&#8217;m new&#8230; promise to post around more time after time!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nohorse</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-3317</link>
		<dc:creator>nohorse</dc:creator>
		<pubDate>Fri, 19 Feb 2010 01:08:37 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-3317</guid>
		<description>Don&#039;t forget to eventually cleanup the async call:

 private static T InvokeWithTimeout(Func operation)
    {
      T result = default(T);
      if (timeout == 0)
      {
        result = operation.Invoke();
      }
      else
      {
        var async = operation.BeginInvoke(AsyncCleanup,operation);
        if (async.AsyncWaitHandle.WaitOne(timeout))
        {
          result = operation.EndInvoke(async);
        }
      }
      return result;
    }

    private static void AsyncCleanup(IAsyncResult result)
    {
      var operation = result.AsyncState as Func;
      if (operation != null)
      {
        operation.EndInvoke(result);
      }
    }</description>
		<content:encoded><![CDATA[<p>Don&#8217;t forget to eventually cleanup the async call:</p>
<p> private static T InvokeWithTimeout(Func operation)<br />
    {<br />
      T result = default(T);<br />
      if (timeout == 0)<br />
      {<br />
        result = operation.Invoke();<br />
      }<br />
      else<br />
      {<br />
        var async = operation.BeginInvoke(AsyncCleanup,operation);<br />
        if (async.AsyncWaitHandle.WaitOne(timeout))<br />
        {<br />
          result = operation.EndInvoke(async);<br />
        }<br />
      }<br />
      return result;<br />
    }</p>
<p>    private static void AsyncCleanup(IAsyncResult result)<br />
    {<br />
      var operation = result.AsyncState as Func;<br />
      if (operation != null)<br />
      {<br />
        operation.EndInvoke(result);<br />
      }<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xander</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-125</link>
		<dc:creator>Xander</dc:creator>
		<pubDate>Thu, 16 Jul 2009 20:23:05 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-125</guid>
		<description>&lt;a href=&quot;#comment-124&quot; rel=&quot;nofollow&quot;&gt;@Russ &lt;/a&gt;, I&#039;ll be sure to check your solution in a runtime environment.

Thanks for a great discussion and i hope to see you in my future posts.</description>
		<content:encoded><![CDATA[<p><a href="#comment-124" rel="nofollow">@Russ </a>, I&#8217;ll be sure to check your solution in a runtime environment.</p>
<p>Thanks for a great discussion and i hope to see you in my future posts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Russ</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-124</link>
		<dc:creator>Russ</dc:creator>
		<pubDate>Thu, 16 Jul 2009 17:21:54 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-124</guid>
		<description>As I said in my second comment, you can terminate if you capture the current thread, e.g.

        Thread t = null;
        Action action = () =&gt;
        {
            t = Thread.CurrentThread;
            F();
        };

Then you call BeginInvoke on action instead of F. The action delegate will execute on the threadpool, so in that context Thread.CurrentThread will be your threadpool thread. If WaitOne returns false, you call t.Abort(). This is nasty for reasons I mentioned earlier, with the added problem that it gradually wrecks the threadpool.

I&#039;m not necessarily arguing that my way is better, just an alternative. Fundamentally, there is no first-class language support for what you&#039;re trying to do, so any solution is going to be hacky in some respect. If your solution is working for you, don&#039;t sweat it - just be aware of the risks, particularly with Thread.Abort().</description>
		<content:encoded><![CDATA[<p>As I said in my second comment, you can terminate if you capture the current thread, e.g.</p>
<p>        Thread t = null;<br />
        Action action = () =&gt;<br />
        {<br />
            t = Thread.CurrentThread;<br />
            F();<br />
        };</p>
<p>Then you call BeginInvoke on action instead of F. The action delegate will execute on the threadpool, so in that context Thread.CurrentThread will be your threadpool thread. If WaitOne returns false, you call t.Abort(). This is nasty for reasons I mentioned earlier, with the added problem that it gradually wrecks the threadpool.</p>
<p>I&#8217;m not necessarily arguing that my way is better, just an alternative. Fundamentally, there is no first-class language support for what you&#8217;re trying to do, so any solution is going to be hacky in some respect. If your solution is working for you, don&#8217;t sweat it &#8211; just be aware of the risks, particularly with Thread.Abort().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xander</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-123</link>
		<dc:creator>Xander</dc:creator>
		<pubDate>Thu, 16 Jul 2009 17:01:45 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-123</guid>
		<description>&lt;a href=&quot;#comment-120&quot; rel=&quot;nofollow&quot;&gt;@Russ &lt;/a&gt; 
Just to visualize my previous reply :

&lt;pre class=&quot;c-sharp&quot; name=&quot;code&quot;&gt;
        // This is your solution
        public static T SafeLimex&lt;T&gt;(Func&lt;T&gt; F, int Timeout, out bool Completed)
        {
            var iar = F.BeginInvoke(null, new object());
            if (iar.AsyncWaitHandle.WaitOne(Timeout))
            {
                Completed = true;
                return F.EndInvoke(iar);
            }
            
            Completed = false;
            return default(T);
        }
&lt;/pre&gt;

Now let&#039;s try to execute the next code :

&lt;pre class=&quot;c-sharp&quot; name=&quot;code&quot;&gt;
            for (int n = 0; n &lt; 100; n++)
            {
                var r = SafeLimex(() =&gt; { 
                    Console.WriteLine(&quot;\n\rThread : &quot; + Thread.CurrentThread.ManagedThreadId); 
                    Thread.Sleep(100); 
                    for (int i = 0; i &lt; 1000; i++) { Console.Write(i); } 
                    return &quot;result&quot;; 
                }, 120, out Finished);
                Console.WriteLine(&quot;\n\r&quot; + &quot;Result : &quot; + r + &quot;, Finished : &quot; + Finished);
            }
&lt;/pre&gt;

You will get something like this :

Thread : 7
0123456789101....454647484950...561
Result : , Finished : False
5625635645.....................................601
Thread : 11
602603604.......................................698
Result : , Finished : False
699...................................................999</description>
		<content:encoded><![CDATA[<p><a href="#comment-120" rel="nofollow">@Russ </a><br />
Just to visualize my previous reply :</p>
<pre class="c-sharp" name="code">
        // This is your solution
        public static T SafeLimex&lt;T&gt;(Func&lt;T&gt; F, int Timeout, out bool Completed)
        {
            var iar = F.BeginInvoke(null, new object());
            if (iar.AsyncWaitHandle.WaitOne(Timeout))
            {
                Completed = true;
                return F.EndInvoke(iar);
            }

            Completed = false;
            return default(T);
        }
</pre>
<p>Now let&#8217;s try to execute the next code :</p>
<pre class="c-sharp" name="code">
            for (int n = 0; n &lt; 100; n++)
            {
                var r = SafeLimex(() =&gt; {
                    Console.WriteLine("\n\rThread : " + Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(100);
                    for (int i = 0; i &lt; 1000; i++) { Console.Write(i); }
                    return "result";
                }, 120, out Finished);
                Console.WriteLine("\n\r" + "Result : " + r + ", Finished : " + Finished);
            }
</pre>
<p>You will get something like this :</p>
<p>Thread : 7<br />
0123456789101&#8230;.454647484950&#8230;561<br />
Result : , Finished : False<br />
5625635645&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.601<br />
Thread : 11<br />
602603604&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;698<br />
Result : , Finished : False<br />
699&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;999</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xander</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/comment-page-1/#comment-121</link>
		<dc:creator>Xander</dc:creator>
		<pubDate>Thu, 16 Jul 2009 16:34:11 +0000</pubDate>
		<guid isPermaLink="false">http://kossovsky.net/?p=687#comment-121</guid>
		<description>&lt;a href=&quot;#comment-120&quot; rel=&quot;nofollow&quot;&gt;@Russ &lt;/a&gt; 
Ok, i agree that this is a much safer way, but correct me if i am wrong - your solution won&#039;t allow to terminate the thread after the timeout and it will continue running without releasing the queue.

Let&#039;s say you are executing some loop with 100ms timeout - you will get the result after 100ms (null or not), but the loop will continue executing till it stops (which is very bad and dangerous)</description>
		<content:encoded><![CDATA[<p><a href="#comment-120" rel="nofollow">@Russ </a><br />
Ok, i agree that this is a much safer way, but correct me if i am wrong &#8211; your solution won&#8217;t allow to terminate the thread after the timeout and it will continue running without releasing the queue.</p>
<p>Let&#8217;s say you are executing some loop with 100ms timeout &#8211; you will get the result after 100ms (null or not), but the loop will continue executing till it stops (which is very bad and dangerous)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

