<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.NET Tips and Tricks &#187; network</title>
	<atom:link href="http://kossovsky.net/index.php/tag/network/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Sat, 25 Dec 2010 08:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>C# Set method timeout using Generics</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 18:30:17 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[execution]]></category>
		<category><![CDATA[file ext]]></category>
		<category><![CDATA[func]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[httpwebrequest]]></category>
		<category><![CDATA[ins]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[lt]]></category>
		<category><![CDATA[milliseconds]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[quick solution]]></category>
		<category><![CDATA[remote computer]]></category>
		<category><![CDATA[return result]]></category>
		<category><![CDATA[synchronous]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[unc]]></category>
		<category><![CDATA[unc path]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=687</guid>
		<description><![CDATA[I&#8217;m pretty sure all of you know the WebRequest and it&#8217;s derived class HttpWebRequest. And what a marvelous property both of them have &#8211; the TimeOut. Yesterday I had to write some app that reads files located on some remote computer. As I knew already this ins&#8217;t such a good practice, because your code can [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (18 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty sure all of you know the WebRequest and it&#8217;s derived class HttpWebRequest.<br />
And what a marvelous property both of them have &#8211; the TimeOut.</p>
<p><span id="more-687"></span></p>
<p>Yesterday I had to write some app that reads files located on some remote computer.<br />
As I knew already this ins&#8217;t such a good practice, because your code can just hang/freeze for seconds waiting for that UNC path to become available or just checking it&#8217;s existence.</p>
<p>Still, I had to provide some quick solution and &#8220;hoping for the best&#8221; wasn&#8217;t good enough.<br />
So, this is what I came up with.</p>
<pre class="c-sharp" name="code">
        public static T Limex&lt;T&gt;(Func&lt;T&gt; F, int Timeout, out bool Completed)
        {
            T result = default(T);
            Thread thread = new Thread(() =&gt; result = F());
            thread.Start();
            Completed = thread.Join(Timeout);
            if (!Completed) thread.Abort();
            return result;
        }

        // Overloaded method, for cases when we don't
        // need to know if the method was terminated
        public static T Limex&lt;T&gt;(Func&lt;T&gt; F, int Timeout)
        {
            bool Completed;
            return Limex(F, Timeout, out Completed);
        }
</pre>
<p>The usage is very simple, just pass any method (declared or anonymous) and the desired Timeout in milliseconds to the Limex. </p>
<p>Example :</p>
<pre class="c-sharp" name="code">
bool Completed;
string Content = Limex(() => File.ReadAllText(@"\\unc\dir\file.ext")
                       ,100 // milliseconds
                       ,out Completed);

if (Completed)
   // Do something
else
  // Do something else
</pre>
<p>Comments and suggestions for improvement are welcome and will be gratefully appreciated</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (18 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-how-to-limit-method-execution-time%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

