<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6433679371380083688</id><updated>2024-09-25T05:39:20.928+01:00</updated><category term="C#"/><category term="Silverlight"/><category term="net"/><category term=".net"/><category term="Process"/><category term="Class"/><category term="ASP.Net"/><category term="C"/><category term="client"/><category term="control"/><category term="design"/><category term="development"/><category term="framework"/><category term="implement"/><category term="monitor"/><category term="pattern"/><category term="problem"/><category term="Exception"/><category term="Thread"/><category term="XAML"/><category term="application"/><category term="as2"/><category term="assembly"/><category term="dynamic"/><category term="event"/><category term="interface"/><category term="interview"/><category term="lock"/><category term="runtime"/><category term="server"/><category term="sharp"/><category term="type"/><category term="AppDomain"/><category term="CLR"/><category term="Domain"/><category term="Enter"/><category term="Game"/><category term="IDisposable"/><category term="IIS"/><category term="MVVM"/><category term="ProcessStartInfo"/><category term="Silverlight 2"/><category term="String"/><category term="Tween"/><category term="Visual Studio"/><category term="WCF"/><category term="WP7"/><category term="Windows Phone"/><category term="actionscript"/><category term="as"/><category term="async"/><category term="binding"/><category term="collector"/><category term="concurrent"/><category term="context"/><category term="error"/><category term="execution"/><category term="exit"/><category term="explorer"/><category term="expression"/><category term="factory"/><category term="finally"/><category term="garbage"/><category term="generic"/><category term="idea"/><category term="internet"/><category term="item"/><category term="leak"/><category term="list"/><category term="load"/><category term="lock(this)"/><category term="log"/><category term="memory"/><category term="message"/><category term="method"/><category term="module"/><category term="net 4.0"/><category term="object"/><category term="online"/><category term="parse"/><category term="periodically"/><category term="project"/><category term="question"/><category term="reflection"/><category term="remote"/><category term="safe"/><category term="screen"/><category term="show"/><category term="software"/><category term="start"/><category term="stop"/><category term="syncronization"/><category term="syncronize"/><category term="web"/><category term="windows"/><category term="11"/><category term="3-rd party"/><category term="8.1"/><category term="=="/><category term="AJAX"/><category term="Activator"/><category term="Always"/><category term="AppPool"/><category term="Application Pool"/><category term="Architecture"/><category term="Auto"/><category term="C# 6.0"/><category term="Call"/><category term="ChildWIndow"/><category term="Class View"/><category term="Component"/><category term="Conditional"/><category term="ConsoleHoster"/><category term="Convert"/><category term="ConvertBack"/><category term="Converter"/><category term="Create"/><category term="CreateNoWindow"/><category term="DataContractSerializer"/><category term="DataReceivedEventHandler"/><category term="DateTime"/><category term="DateTime.Now"/><category term="DateTime.UtcNow"/><category term="Diagnostics"/><category term="Disposable"/><category term="Dispose"/><category term="Dispose()"/><category term="EF"/><category term="EF 4.0"/><category term="Entity"/><category term="Equals"/><category term="Exist"/><category term="Explicit"/><category term="Found"/><category term="GC"/><category term="GetGenericArguments"/><category term="GetGenericTypeDefinition"/><category term="IE"/><category term="IE11"/><category term="IPAddress"/><category term="IValueConverter"/><category term="InProc"/><category term="Instance"/><category term="InvalidCastException"/><category term="LayoutKind"/><category term="Legends"/><category term="LegendsOfNoobia"/><category term="LoadControl"/><category term="MEF"/><category term="MORPG"/><category term="MakeGenericType"/><category term="Map control"/><category term="MarshalByRefObject"/><category term="Microsoft"/><category term="Multiple"/><category term="NetOnly"/><category term="NewLine"/><category term="Noobia"/><category term="Not"/><category term="NotFound"/><category term="Now"/><category term="OutputDataReceived"/><category term="P-Invoke"/><category term="RPG"/><category term="RedirectStandardOutput"/><category term="RegEx"/><category term="Role"/><category term="STAThread"/><category term="Service"/><category term="Session"/><category term="Singleton"/><category term="State"/><category term="StructLayout"/><category term="StructLayoutAttribute"/><category term="Studio"/><category term="Synchronize"/><category term="TcpClient"/><category term="Tetris+"/><category term="TryEnter"/><category term="UnhandledException"/><category term="Unit Test"/><category term="UpdateMode"/><category term="UpdatePanel"/><category term="UseShellExecute"/><category term="UtcNow"/><category term="VS"/><category term="VS.Net"/><category term="ViewModel"/><category term="Visual"/><category term="WP8"/><category term="WebBrowser"/><category term="Win8"/><category term="XNA"/><category term="absolute"/><category term="abstract"/><category term="animation"/><category term="app"/><category term="applicaiton"/><category term="approach"/><category term="argument"/><category term="as3"/><category term="aspnet_wp.exe"/><category term="assistance"/><category term="audit"/><category term="await"/><category term="backward"/><category term="c++"/><category term="care"/><category term="change"/><category term="character"/><category term="chat"/><category term="child"/><category term="cleanup"/><category term="collect"/><category term="collection"/><category term="common"/><category term="company"/><category term="compare"/><category term="comparison"/><category term="compatibility"/><category term="compatible"/><category term="concurrency"/><category term="conditions"/><category term="connection"/><category term="console"/><category term="console-based"/><category term="constructor"/><category term="content"/><category term="continuously"/><category term="contract"/><category term="copy"/><category term="crash"/><category term="crashing"/><category term="csharp"/><category term="custom"/><category term="customize"/><category term="cut"/><category term="data"/><category term="databind"/><category term="deadlock"/><category term="debug"/><category term="debugger"/><category term="delegate"/><category term="derived type"/><category term="deserialize"/><category term="dialog"/><category term="distribute"/><category term="distributed"/><category term="dual"/><category term="enum"/><category term="enumeration"/><category term="err"/><category term="exclude"/><category term="external"/><category term="field"/><category term="fight"/><category term="file"/><category term="file system"/><category term="flash"/><category term="folder"/><category term="force"/><category term="format"/><category term="forward"/><category term="full"/><category term="generics"/><category term="group"/><category term="handle"/><category term="handler"/><category term="heal"/><category term="hide"/><category term="history"/><category term="host"/><category term="html"/><category term="identify"/><category term="iframe"/><category term="implementation"/><category term="import"/><category term="inheritance"/><category term="init"/><category term="initialization"/><category term="initialize"/><category term="inline"/><category term="innerHTML"/><category term="invalid cast"/><category term="invest"/><category term="investement"/><category term="invester"/><category term="invocaton"/><category term="invoke"/><category term="ip"/><category term="iterate"/><category term="iteration"/><category term="iterator"/><category term="javascript"/><category term="keyword"/><category term="loader"/><category term="long"/><category term="manage"/><category term="math"/><category term="mathematical"/><category term="menu"/><category term="messaging"/><category term="methodinfo"/><category term="model"/><category term="multicast"/><category term="multicastdelegate"/><category term="multiplayer"/><category term="name"/><category term="nameof"/><category term="native"/><category term="need"/><category term="of"/><category term="offset"/><category term="operator"/><category term="optimize"/><category term="oriented"/><category term="output"/><category term="page"/><category term="parallel"/><category term="param"/><category term="parameter"/><category term="parameters"/><category term="parent"/><category term="parser"/><category term="paste"/><category term="path"/><category term="phone 7"/><category term="polymorphysm"/><category term="position"/><category term="prefix"/><category term="prepare"/><category term="property"/><category term="propertyInfo"/><category term="proxy"/><category term="questions"/><category term="random"/><category term="read"/><category term="realise"/><category term="recycle"/><category term="ref"/><category term="refactor"/><category term="reference"/><category term="regular"/><category term="relative"/><category term="remoting"/><category term="remove"/><category term="repeat"/><category term="robust"/><category term="runas"/><category term="runas.exe"/><category term="say"/><category term="serialize"/><category term="share"/><category term="short"/><category term="shout"/><category term="signature"/><category term="site"/><category term="socket"/><category term="socket server"/><category term="static"/><category term="storage"/><category term="strategy"/><category term="stream"/><category term="strong"/><category term="support"/><category term="synchronization"/><category term="system"/><category term="tab"/><category term="table"/><category term="tag"/><category term="task"/><category term="tcp"/><category term="tell"/><category term="terminate"/><category term="testing"/><category term="thread safe"/><category term="toolkit"/><category term="tracking"/><category term="try"/><category term="unhandled"/><category term="union"/><category term="user"/><category term="ut"/><category term="view"/><category term="view-model"/><category term="virtual"/><category term="visibility"/><category term="visible"/><category term="weak"/><category term="win8.1"/><title type='text'>Development ideas</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>60</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-69583979438537719</id><published>2017-06-20T22:39:00.002+01:00</published><updated>2017-06-20T22:39:19.483+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="async"/><category scheme="http://www.blogger.com/atom/ns#" term="await"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="execution"/><category scheme="http://www.blogger.com/atom/ns#" term="init"/><category scheme="http://www.blogger.com/atom/ns#" term="initialization"/><category scheme="http://www.blogger.com/atom/ns#" term="initialize"/><category scheme="http://www.blogger.com/atom/ns#" term="load"/><category scheme="http://www.blogger.com/atom/ns#" term="parallel"/><title type='text'>How to initialize instances asyncronously</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Here is another problem I&#39;ve been thinking about quite a while, before found a solution I like.&lt;br /&gt;
async/await keywords revolutionized the writing of asynchronous code in C#. It is as simple as marking a method with async keyword and using await in it to wait for asynchronous operations to complete in non-blocking manner.This gets trickier with async initialization methods. Think about a class, instances of which need to be initialized once only, before those can be used. In a non-async scenario one example would be to call the initialization method from the constructor, as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;SomeDirectory&lt;/span&gt;
{
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; Object state;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;SomeDirectory&lt;/span&gt;()
  {
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.Initialize();
  }

  &lt;span style=&quot;color: #888888;&quot;&gt;// This method expects the state to be initialized before execution&lt;/span&gt;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;DoSomething&lt;/span&gt;()
  {
    &lt;span style=&quot;color: #888888;&quot;&gt;// some logic&lt;/span&gt;
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;Initialize&lt;/span&gt;()
  {
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.state = &lt;span style=&quot;color: #888888;&quot;&gt;// state retrieval logic&lt;/span&gt;
  }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
This gets complicated, when the initialization method becomes asynchronous. As constructors are non-async methods, the following transformation can lead to a situation, where instances of the SomeDirectory class aren&#39;t yet initialized after constructor, which can lead to some unexpected side-effects later, when the DoSomething method is called. The below sample code emphasizes the situation:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;SomeDirectory&lt;/span&gt;
{
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; Object state;

&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; SomeDirectory()
&amp;nbsp; {
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.InitializeAsync();
&amp;nbsp; }

&amp;nbsp; &lt;span style=&quot;color: #888888;&quot;&gt;// This method expects the state to be initialized before execution&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;async&lt;/span&gt; Task DoSomethingAsync()
&amp;nbsp; {
&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #888888;&quot;&gt;// some logic&lt;/span&gt;
&amp;nbsp; }

&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;async&lt;/span&gt; Task InitializeAsync()
&amp;nbsp; {
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.state = &lt;span style=&quot;color: #888888;&quot;&gt;// state retrieval logic&lt;/span&gt;
&amp;nbsp; }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
A simple solution to this would be to force the constructor to wait for the initialization to complete:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; SomeDirectory()
&amp;nbsp; {
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.InitializeAsync().GetAwaiter().GetResult();
&amp;nbsp; }
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;b&gt;&lt;/b&gt;&lt;i&gt;&lt;/i&gt;&lt;u&gt;&lt;/u&gt;&lt;sub&gt;&lt;/sub&gt;&lt;sup&gt;&lt;/sup&gt;&lt;strike&gt;&lt;br /&gt;
&lt;/strike&gt;&lt;/div&gt;&lt;div&gt;This, however, is not a good technique, as it will introduce delay in the caller thread, which can harm in many situations (like in server code, where the actual DoSomethingAsync api is not being called immediately).&lt;/div&gt;&lt;br /&gt;
The third approach is calling initialization on every public API. Of course with a simple &quot;run-only-once&quot; optimization in it. Something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;SomeDirectory&lt;/span&gt;
{
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;readonly&lt;/span&gt; Object stateLock = &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;new&lt;/span&gt; Object();
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; Object state;

&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; SomeDirectory()
&amp;nbsp; {
&amp;nbsp; }

&amp;nbsp; &lt;span style=&quot;color: #888888;&quot;&gt;// This method expects the state to be initialized before execution&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;async&lt;/span&gt; Task DoSomethingAsync()
&amp;nbsp; {
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.InitializeAsync();

&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #888888;&quot;&gt;// some logic&lt;/span&gt;
&amp;nbsp; }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
The problem with this approach is that it will slow down the very first call to DoSomethingAsync() method. Sometimes - it&#39;s something you don&#39;t want to have.&lt;br /&gt;
So here comes the final approach, which I&#39;m using nowdays:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;SomeDirectory&lt;/span&gt;
{
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; Object state;
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;readonly&lt;/span&gt; Task initializationTask;

&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; SomeDirectory()
&amp;nbsp; {
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.initializationTask = &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.InitializeAsync();
&amp;nbsp; }

&amp;nbsp; &lt;span style=&quot;color: #888888;&quot;&gt;// This method expects the state to be initialized before execution&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;async&lt;/span&gt; Task DoSomethingAsync()
&amp;nbsp; {
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.initializationTask;

&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #888888;&quot;&gt;// some logic&lt;/span&gt;
&amp;nbsp; }

&amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;async&lt;/span&gt; Task InitializeAsync()
&amp;nbsp; {
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;.state = &lt;span style=&quot;color: #888888;&quot;&gt;// state retrieval logic&lt;/span&gt;
&amp;nbsp; }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
This approach will allow the execution of the initialization start as soon as the object is constructed. Yet, it won&#39;t loose the track of its execution.&lt;br /&gt;
&lt;br /&gt;
Hope this will help some of you.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/69583979438537719/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/69583979438537719' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/69583979438537719'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/69583979438537719'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2017/06/how-to-initialize-instances.html' title='How to initialize instances asyncronously'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-6852040415302096108</id><published>2016-07-22T17:56:00.001+01:00</published><updated>2016-07-29T18:33:47.986+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Class"/><category scheme="http://www.blogger.com/atom/ns#" term="Disposable"/><category scheme="http://www.blogger.com/atom/ns#" term="factory"/><category scheme="http://www.blogger.com/atom/ns#" term="generics"/><category scheme="http://www.blogger.com/atom/ns#" term="IDisposable"/><category scheme="http://www.blogger.com/atom/ns#" term="interface"/><category scheme="http://www.blogger.com/atom/ns#" term="method"/><category scheme="http://www.blogger.com/atom/ns#" term="problem"/><category scheme="http://www.blogger.com/atom/ns#" term="safe"/><title type='text'>Unknown disposables: Disposing of non-disposable interface instances</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
In coding, it&#39;s a good practice to use factory methods (or some other creational design pattern) to abstract away creation of class instances. In most of the cases, the&amp;nbsp;&quot;creator&quot;&amp;nbsp;would return not the actual type being created, but the abstraction - the interface.&lt;/div&gt;
&lt;div&gt;
To emphasize the problem, let&#39;s consider the below example:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBP0L0rWZ8EDuFk39glAIAvoPFTqpbKxd5Wu9uYfn9NX7KzoV8jm4BEy1Ph2bgrbEhDZIUhmrplea5KBMDFSCQZm26h_r3GTYZyLN9ZNcL7aN3mEyuug6cKb4uI5-qw3sUQRjbHcj-TdFj/s1600/Class+Diagram.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;160&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBP0L0rWZ8EDuFk39glAIAvoPFTqpbKxd5Wu9uYfn9NX7KzoV8jm4BEy1Ph2bgrbEhDZIUhmrplea5KBMDFSCQZm26h_r3GTYZyLN9ZNcL7aN3mEyuug6cKb4uI5-qw3sUQRjbHcj-TdFj/s400/Class+Diagram.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
For simplicity, let&#39;s assume that we&#39;re dealing with a factory method, and the interface the factory method returns is called &quot;IContract&quot;. Note, that IContract does not implement IDisposable. There are two different implementations of IContract - Impl and DisposableImpl. So here comes the interesting part, the DisposableImpl implementation does implement IDisposable.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
So now, if one uses the factory method &quot;Create()&quot; to instantiate IContract, it may get a disposable instance back, and won&#39;t ever even know about it:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
public class ContractFactory&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public IContract Create()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new DisposableImpl();&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Obviously, the responsibility of disposing of the disposable instance is on the caller. A lot of people won&#39;t even notice the problem and just use the IContract and never worry about freeing up resources as early as possible. Yes, GC will eventually take care of the unnecessary resources, but that&#39;ll happen later than the actual variable becomes unnecessary - during finalization.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
So one pattern a client can use is the following:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
{&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; IContract contract = null;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; try {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; contract = new ContractFactory().Create();&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// all the usage of the contract instance happens here&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; finally&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDisposable disposableContract = contract as IDisposable;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (disposableContract != null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; disposableContract.Dispose();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This will ensure safety in case of both disposable and non-disposable implementations.&lt;/div&gt;
&lt;div&gt;
Another way to go, would be to have a wrapper class, encapsulating the disposal logic and being a disposable itself, as follows:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
public sealed class DisposableInterface&amp;lt;T&amp;gt; : IDisposable where T : class&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private readonly T interfaceInstance;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public T Instance&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { return this.interfaceInstance; }&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public DisposableInterface(T instance)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (instance == null)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentNullException(&quot;instance&quot;);&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.interfaceInstance = instance;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // A better implementation would be the usage of the Dispose pattern here.&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Not doing that here to keep the sample simple.&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Dispose()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IDisposable&amp;nbsp;disposableInstance = this.Instance as IDisposable;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (disposableInstance != null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; disposableInstance.Dispose();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now, the caller could use the IContract as follows:&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
{&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; using (DisposableInterface&amp;lt;IContract&amp;gt; contract = new DisposableInterface&amp;lt;IContract&amp;gt;(new ContractFactory().Create()))&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IContract instance = contract.Instance;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // all the interaction now happens with the &quot;instance&quot; reference&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
As you can see, the second example is cleaner and nicer.&lt;/div&gt;
&lt;div&gt;
Now, as you&#39;re familiar with the problem, doesn&#39;t it raise a natural question: &quot;should this become a pattern and be used all over the place with factory methods, as one can&#39;t know whether the actual implementation the factory methods returns will be disposable or not?&quot;....&lt;/div&gt;
&lt;div&gt;
Think about this and leave any comments you may have.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/6852040415302096108/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/6852040415302096108' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/6852040415302096108'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/6852040415302096108'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2016/07/unknown-disposables-disposing-of-non.html' title='Unknown disposables: Disposing of non-disposable interface instances'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBP0L0rWZ8EDuFk39glAIAvoPFTqpbKxd5Wu9uYfn9NX7KzoV8jm4BEy1Ph2bgrbEhDZIUhmrplea5KBMDFSCQZm26h_r3GTYZyLN9ZNcL7aN3mEyuug6cKb4uI5-qw3sUQRjbHcj-TdFj/s72-c/Class+Diagram.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-2553283233412902805</id><published>2015-06-17T18:53:00.001+01:00</published><updated>2015-10-16T19:25:34.430+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Class"/><category scheme="http://www.blogger.com/atom/ns#" term="native"/><category scheme="http://www.blogger.com/atom/ns#" term="NetOnly"/><category scheme="http://www.blogger.com/atom/ns#" term="P-Invoke"/><category scheme="http://www.blogger.com/atom/ns#" term="Process"/><category scheme="http://www.blogger.com/atom/ns#" term="ProcessStartInfo"/><category scheme="http://www.blogger.com/atom/ns#" term="runas"/><category scheme="http://www.blogger.com/atom/ns#" term="runas.exe"/><category scheme="http://www.blogger.com/atom/ns#" term="start"/><title type='text'>&quot;RunAs /NetOnly&quot; functionality from C# code</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Only recently I&#39;ve learnt about the &quot;/netonly&quot; flag availability for &quot;runas&quot; command. I was really happy to find it, but the issue came later.&lt;br /&gt;
It turns out that the &lt;b&gt;runas.exe&lt;/b&gt; command can&#39;t be automated, as it accepts passwords only through a prompt. So there is no way to pass in credentials without the prompt.&lt;br /&gt;
&lt;br /&gt;
That made me seek for a&amp;nbsp;custom solution for the problem. What it turned out is that the .Net Process API doesn&#39;t expose any functionality to control the process creation the way, which &quot;/netonly&quot; flag&amp;nbsp;would&amp;nbsp;results in.&lt;br /&gt;
&lt;br /&gt;
Long story short, I came up with the following solution:&lt;br /&gt;
The &lt;b&gt;CreateProcessWithLogonW&lt;/b&gt; Win32 API, which is being called internally by runas.exe, has a number of parameters indicating how exactly the process should be created. One of these parameters is &quot;dwLogonFlags&quot;. The set of values it can take has one called &quot;LOGON_NETCREDENTIALS_ONLY&quot;. From MSDN documentation for that value: &quot;Log on, but use the specified credentials on the network only. The new process uses the same token as the caller, but the system creates a new logon session within LSA, and the process uses the specified credentials as the default credentials.&quot;. This is exactly what I need. After few hours of unsuccessful search for a C# code sample, which would create a process with this flag, I ended up creating my own.&lt;br /&gt;
The sample code is available for&amp;nbsp;download at: &lt;a href=&quot;http://1drv.ms/1LeqKnD&quot;&gt;http://1drv.ms/1LeqKnD&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Hope you find this useful !&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/2553283233412902805/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/2553283233412902805' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/2553283233412902805'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/2553283233412902805'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2015/06/runas-netonly-functionality-from-c-code.html' title='&quot;RunAs /NetOnly&quot; functionality from C# code'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-7590132017488211152</id><published>2015-04-09T17:30:00.003+01:00</published><updated>2015-04-25T08:20:42.834+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="backward"/><category scheme="http://www.blogger.com/atom/ns#" term="change"/><category scheme="http://www.blogger.com/atom/ns#" term="compatibility"/><category scheme="http://www.blogger.com/atom/ns#" term="compatible"/><category scheme="http://www.blogger.com/atom/ns#" term="design"/><category scheme="http://www.blogger.com/atom/ns#" term="factory"/><category scheme="http://www.blogger.com/atom/ns#" term="file system"/><category scheme="http://www.blogger.com/atom/ns#" term="forward"/><category scheme="http://www.blogger.com/atom/ns#" term="pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="robust"/><category scheme="http://www.blogger.com/atom/ns#" term="storage"/><category scheme="http://www.blogger.com/atom/ns#" term="strategy"/><title type='text'>The Compatibility Pattern: Designing adaptive classes</title><content type='html'>&lt;div style=&quot;text-align: left&quot; dir=&quot;ltr&quot; trbidi=&quot;on&quot;&gt;Any non-deprecated system, soon or late, faces challenges. Some of these challenges are quite common. The two following items represent just a fraction of those:&lt;br&gt;&lt;/div&gt; &lt;div style=&quot;text-align: left&quot; dir=&quot;ltr&quot; trbidi=&quot;on&quot;&gt; &lt;ul&gt; &lt;li&gt;existing file storage doesn&#39;t satisfy current/coming space requirements  &lt;li&gt;the data model need to be modified/extended to comply with new requirements&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;Today I’m going to describe a design pattern, which can be applied in a variety of situations (including the two above) and eliminate the pain of code re-write.&lt;/p&gt; &lt;p&gt;So, for the sake of simplicity let’s concentrate on a single scenario. Let’s assume we have an existing system – which is operational. Let’s also assume that “EntityA” is an entity in the data model of the system, and the records of that entity are processed by some business logic: “LogicA”.&amp;nbsp; So far the system used to operate just fine. Now – new requirements come in – which introduce changes to “EntityA” and the “LogicA” as well. Specifically, new “EntityA” records should be treated differently than the “old/current” records. For the sake of simplicity, let’s limit the changes in “EntityA” by just one new field “Field2”. So here what “EntityA” will look like:&lt;/p&gt; &lt;p align=&quot;center&quot;&gt;&lt;a href=&quot;http://lh3.googleusercontent.com/-2_ouSSNfQCE/VTtAO3AIpzI/AAAAAAAAaag/MnoRnXQyHDU/s1600-h/image2.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://lh3.googleusercontent.com/-JdyC9M5WXCs/VTtAPrdhAJI/AAAAAAAAaao/1KbcDZ_CvUk/image_thumb.png?imgmax=800&quot; width=&quot;244&quot; height=&quot;89&quot;&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;where “Field1” is an existing field on the entity. Note, that I intentionally don’t mention the types here as its irrelevant in this context.&lt;/p&gt; &lt;p&gt;Also, let’s assume that “LogicA” is already encapsulated in a class with the same name, as follows:&lt;/p&gt; &lt;p align=&quot;center&quot;&gt;&lt;a href=&quot;http://lh3.googleusercontent.com/-DKbSray-sKs/VTtAQUQbzuI/AAAAAAAAaaw/jNOmGfh6ut4/s1600-h/image5.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://lh3.googleusercontent.com/-HTvduuYbYGw/VTtARGjZGSI/AAAAAAAAaa4/94gKbuK4Ln0/image_thumb1.png?imgmax=800&quot; width=&quot;244&quot; height=&quot;70&quot;&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Now, back to the new requirements, which affect “LogicA”. The simplest way to handle the change would be to modify the “LogicA.ProcessData” method. However that won’t be maintainable. Think about any future changes – affecting the same logic. After several such changes it’ll be really a mess and it won’t be easy to figure out how to change something&amp;nbsp; to impact only specific version of entities. So the simplest and design-wise proper solution would be to introduce new LogicA_New handler for the new entities and put the decision logic of instantiating the proper logic in some factory class. The following diagram depicts that case:&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;http://lh3.googleusercontent.com/-SJWBkpxgfMo/VTtARoiBdmI/AAAAAAAAabA/Dr3ChHLT0iA/s1600-h/image22.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://lh3.googleusercontent.com/-x25Wp8kdcTM/VTtASJEFA-I/AAAAAAAAabE/Lu_FCWHp3B4/image_thumb8.png?imgmax=800&quot; width=&quot;417&quot; height=&quot;147&quot;&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Note, that the abstraction of the handling logic into separate classes is an implementation of the Strategy design pattern.&lt;/p&gt; &lt;p&gt;This model allows future expansion of the system by just defining a new strategy/logic and updating the factory method, to return the proper handler type. Handling logic deprecation is quite straight forward as well, and can be handled in the reverse order: the factory method is updated to remove the obsolete case and the appropriate logic handler is removed.&lt;/p&gt; &lt;p&gt;Hope this will save you some time in the future.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/7590132017488211152/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/7590132017488211152' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/7590132017488211152'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/7590132017488211152'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2015/04/the-compatibility-pattern-designing.html' title='The Compatibility Pattern: Designing adaptive classes'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.googleusercontent.com/-JdyC9M5WXCs/VTtAPrdhAJI/AAAAAAAAaao/1KbcDZ_CvUk/s72-c/image_thumb.png?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-8897796361762217455</id><published>2015-04-01T07:03:00.001+01:00</published><updated>2015-04-01T07:04:52.311+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="development"/><category scheme="http://www.blogger.com/atom/ns#" term="interview"/><category scheme="http://www.blogger.com/atom/ns#" term="math"/><category scheme="http://www.blogger.com/atom/ns#" term="mathematical"/><category scheme="http://www.blogger.com/atom/ns#" term="prepare"/><category scheme="http://www.blogger.com/atom/ns#" term="problem"/><category scheme="http://www.blogger.com/atom/ns#" term="Process"/><category scheme="http://www.blogger.com/atom/ns#" term="question"/><category scheme="http://www.blogger.com/atom/ns#" term="questions"/><category scheme="http://www.blogger.com/atom/ns#" term="software"/><title type='text'>The interview process</title><content type='html'>&lt;p&gt;Looking back - there were many times I’ve went through an interview process for different software development positions in different companies. To be more precise – there were around 20 interviews I’ve went through. Frankly saying, it doesn’t matter what kind of company you’re interviewing for – 60% of the technical questions will be very similar (of course, there are rare exceptions to this rule). And if you’ve gone through at least 5 interview processes, you probably know what to look for - how to prepare for those questions.&lt;/p&gt; &lt;p&gt;Why ? Because most of those questions you’ll be asked will be just formality and well known mathematical problems. It’s just a matter of filling in a checkbox whether you’ve answered a question or not – and the chances you’d be ever dealing with that kind of problem during your day to day work – are almost 0! To answer these kind of questions you’d need to just have walked over any similar problem in the past. Most of those share very similar solutions.&lt;/p&gt; &lt;p&gt;These 60% are the bad part of the interview process and I call – THE INTERVIEW TAX. Because you have to learn those anyway even if you won’t be dealing with anything close to those questions.&lt;/p&gt; &lt;p&gt;Now, let’s talk about the other 40%. Those divide into three categories:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Thinking questions  &lt;li&gt;Practice questions  &lt;li&gt;Design questions&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;1. Thinking questions&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;These may easily be unsolvable mathematical problems as well. And the interviewer is asking you the question and also ask to think loudly. This is important as what he/she wants to observe – is how you think. Or – do you think at all. I know it’s funny, but I’ve seen people just having no ideas on how to solve a problem.&lt;/p&gt; &lt;p&gt;On the other hand, the problem can be really simple as well – very trivial – but the discussion around it will be like building a knowledge graph: you say something, you get another question on what you said, you answer again, and again get a new question – which slowly takes you away from the original topic. These discussions can take hours (luckily the interviewer will have a schedule).&lt;/p&gt; &lt;p&gt;&lt;strong&gt;2. Practice questions&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The questions here are simple coding questions in your preferred programming language (unless the company has strict requirements about the coding language). In most of the cases – even a pseudo-code will be fine. These questions are aimed to verify your coding skills – like properly handling validation, corner cases. In the end you most probably will be given also a task to write unit-tests for your code. The interviewer may have noticed a mistake in the code, and sometimes want you to catch it – so be careful to do so. If you’ll write a code and the tests you’ll write won’t handle it – that’ll be reflected negatively for you.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;3. Design questions&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;This is my preferred topic. Unfortunately these are quite rare questions during the interview process you get asked. I believe those are the most valuable ones as those will be giving answers on how well you’ll be doing your work – if you’ll be hired.&lt;/p&gt; &lt;p&gt;Anyway – here you may be given some real-world problems – and you’d have to solve it – by describing a system from a high level. The system can be very small or very big – depending on the responsibilities you’re being considered for. But even on the smallest systems (a simple example is an elevator or an alarm clock) some important skills can be seen.&lt;/p&gt; &lt;p&gt;I hope this writing will be a good overview for one who is looking forward for an interview and also for those who host interviews – to improve their existing processes, if they fall in the 60% category.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/8897796361762217455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/8897796361762217455' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/8897796361762217455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/8897796361762217455'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2015/04/the-interview-process.html' title='The interview process'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-152541954245322243</id><published>2015-01-10T06:00:00.001+00:00</published><updated>2015-01-10T06:00:18.457+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C# 6.0"/><category scheme="http://www.blogger.com/atom/ns#" term="format"/><category scheme="http://www.blogger.com/atom/ns#" term="inline"/><category scheme="http://www.blogger.com/atom/ns#" term="nameof"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><title type='text'>C# 6.0: The great changes</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
It&#39;s now more than 10 years I&#39;m writing code in C# and I enjoy every part of it. It gets better with every version, and there is no exception in the coming version: C# 6.0.&lt;br /&gt;
Everybody who had ever dealt with MVVM pattern,&amp;nbsp;will be&amp;nbsp;well familiar with the &lt;strong&gt;INotifyPropertyChange&lt;/strong&gt; interface. The tedious part of the implementation were the constants (I used to have constants always) you had to define per each publicly exposed property - to store the name of the property.&lt;br /&gt;
I recently had a post about how to retrieve the property names dynamically using reflection (&lt;a href=&quot;http://mkartak.blogspot.com/2014/04/improving-c-getting-reflectioninformati.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;), but still it&#39;s not everyone would consider a good option to go with. C# 6.0&amp;nbsp;mitigates this pain with a new keyword called &quot;&lt;strong&gt;nameof&lt;/strong&gt;&quot;.&lt;br /&gt;
To get the name of a property you&#39;d simply write: &lt;strong&gt;nameof&lt;/strong&gt;(PropName), and this would return &quot;PropName&quot;. Really cool !&lt;br /&gt;
&lt;br /&gt;
And now the other thing I am happy about. Remember the String.Format(&quot;Name: {0}&quot;, record.Name)&amp;nbsp;&amp;nbsp;way of doing things ? There were many issues I&#39;ve seen where updating the format one forgets to add additional parameter, or adds one, but forgets to add the &#39;{X}&#39; placeholder for it - which would cause a runtime exception. C# 6.0 changes the mechanism completely - as now instead of the placeholders you&#39;ll just refer to the values within the format string. That&#39;s much more readable and safe:&lt;br /&gt;
&lt;em&gt;String.Format(&quot;Name: &lt;strong&gt;\{record.Name}&lt;/strong&gt;&quot;)&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
And there is much more...&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/152541954245322243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/152541954245322243' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/152541954245322243'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/152541954245322243'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2015/01/c-60-great-changes.html' title='C# 6.0: The great changes'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-2085090771983548729</id><published>2014-12-23T17:20:00.000+00:00</published><updated>2015-05-17T07:05:21.843+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="DataContractSerializer"/><category scheme="http://www.blogger.com/atom/ns#" term="Exception"/><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><title type='text'>WCF: Identifing late exceptions in the pipeline</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi.&lt;br /&gt;
Recently I’ve run into&amp;nbsp; a strange WCF service issue. The logs in the service method were indicating a successful completion of the call, but the call was still failing and the clients were getting exceptions.&lt;br /&gt;
&amp;nbsp;The exception message was: “&lt;i&gt;An error occurred while receiving the HTTP response to &lt;/i&gt;&lt;strong&gt;&amp;lt;serviceEndpoint&amp;gt;&lt;/strong&gt;&lt;i&gt;. This could be due to the service endpoint binding not using the HTTP protocol. This &lt;/i&gt;&lt;i&gt;could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).“.&lt;/i&gt;&lt;br /&gt;
The service API which was called had a return type (not void), and the type was serializable. So the only explanation I could come up with was that something wrong goes with serialization. THAT WAS IT!&lt;br /&gt;
Luckily it’s possible to simulate the same serialization what WCF does behind the scene, and verify the assumptions. So I did it: created a test method within the unit-test project, called the API being called by the client to get properly built result object, and then tried to serialize it:&lt;br /&gt;
&lt;blockquote&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;TestMethod&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;public&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;void&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; GetDataUsingDataContract_SerializesResults()&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;{&lt;/span&gt;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;Service1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; serviceInstance = &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;Service1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&amp;nbsp; CompositeType&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; argument = &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;CompositeType&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&amp;nbsp; CompositeType&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; result = serviceInstance.GetDataUsingDataContract(argument);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&amp;nbsp; using&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; (&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;MemoryStream&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; stream = &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;MemoryStream&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;())&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;br /&gt;&amp;nbsp; {&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;DataContractSerializer&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; serializer = &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;DataContractSerializer&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: consolas; font-size: x-small;&quot;&gt;typeof&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: consolas; font-size: x-small;&quot;&gt;CompositeType&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: consolas; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; serializer.WriteObject(stream, result);&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Note: you’ll need to add a reference to System.Runtime.Serialization.dll.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my case the “serializer.WriteObject(stream, result);” call threw with the detailed exception.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
You can download the full sample project: &lt;a href=&quot;http://1drv.ms/1sJljCd&quot; title=&quot;http://1drv.ms/1sJljCd&quot;&gt;http://1drv.ms/1sJljCd&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hope this helps.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/2085090771983548729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/2085090771983548729' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/2085090771983548729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/2085090771983548729'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2014/12/wcf-exception-after-successful.html' title='WCF: Identifing late exceptions in the pipeline'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-8329477646739599842</id><published>2014-09-06T07:14:00.001+01:00</published><updated>2014-09-06T07:14:22.677+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Map control"/><category scheme="http://www.blogger.com/atom/ns#" term="problem"/><category scheme="http://www.blogger.com/atom/ns#" term="toolkit"/><category scheme="http://www.blogger.com/atom/ns#" term="Windows Phone"/><category scheme="http://www.blogger.com/atom/ns#" term="WP8"/><category scheme="http://www.blogger.com/atom/ns#" term="XAML"/><title type='text'>Windows Phone 8: The Map control</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Recently I&#39;ve been&amp;nbsp;hitting an issue with integrating the Map control from the Windows Phone Toolkit into the UI. The problem was&amp;nbsp;related to&amp;nbsp;centering map to&amp;nbsp;the desired location. I was using MVVM pattern for the app, so I had a proper public property in the data context, which I was trying to set as a binding source for the Center property of the Map control.&lt;br /&gt;
&lt;br /&gt;
As I was just interested in centering map based on the property value, and not vice-versa, I&#39;ve had explicitly specified the Mode property for the binding to be &quot;One Way&quot;. And that was the problem.&lt;br /&gt;
It turns out, that for the Center property to work properly, you have to have the binding mode set to &quot;Two Way&quot;.&lt;br /&gt;
&lt;br /&gt;
So the final property and the XAML should be similar to the following to work:&lt;br /&gt;
&lt;br /&gt;
The following property is the one defined in the data context:&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;public&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;GeoCoordinate&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; MapCenterLocation&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;{&lt;br /&gt;
&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;            &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&lt;/span&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;                &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;return&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;this&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;.mapCenterLocation;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;            &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;set&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;                &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;if&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; (&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;value&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; != &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;this&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;.mapCenterLocation)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;                   &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;this&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;.NotifyPropertyChanging(MapCenterLocationPropertyName);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;                   &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;.mapCenterLocation = &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;value&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;                   &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;.NotifyPropertyChanged(MapCenterLocationPropertyName);&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
and the appropriate XAML for the map control should be:&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;maps&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;Map&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt; x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;Name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;=&quot;map&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;HorizontalAlignment&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;=&quot;Stretch&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;VerticalAlignment&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;=&quot;Stretch&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;Center&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;=&quot;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;Binding&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt; Path&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;=MapCenterLocation,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: x-small;&quot;&gt; &lt;strong&gt;Mode&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;strong&gt;=TwoWay&lt;/strong&gt;}&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
As mentioned above - the &quot;Mode=TwoWay&quot; is critical for the Center property binding to work. Otherwise, the map will never be centered like the binding is not there.&lt;br /&gt;
Interestingly, you don&#39;t even get an error/warning for that.&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/8329477646739599842/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/8329477646739599842' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/8329477646739599842'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/8329477646739599842'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2014/09/windows-phone-8-map-control.html' title='Windows Phone 8: The Map control'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-567681814608715205</id><published>2014-04-10T17:51:00.000+01:00</published><updated>2014-06-16T17:04:18.417+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="dynamic"/><category scheme="http://www.blogger.com/atom/ns#" term="idea"/><category scheme="http://www.blogger.com/atom/ns#" term="name"/><category scheme="http://www.blogger.com/atom/ns#" term="property"/><category scheme="http://www.blogger.com/atom/ns#" term="propertyInfo"/><category scheme="http://www.blogger.com/atom/ns#" term="reflection"/><title type='text'>Dynamic Reflection in C#</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi,&lt;br /&gt;
&lt;br /&gt;
More and more I started to feel the need for dynamic reflection - reflection working not based on subject names (properties, methods, fields ,...) but delegates.&lt;br /&gt;
So got time to submit and idea on Microsoft Connect&amp;nbsp;about it.&amp;nbsp;The following sample shows what the idea is about:&lt;br /&gt;
internal class Sample&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;public int PropA { get; set; }&lt;br /&gt;
&amp;nbsp;public void SomeMethod()&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;PropertyInfo info = GetPropertyInfo(() =&amp;gt; this.PropA);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;// Some other code, using the &quot;info&quot; variable &lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;public static PropertyInfo GetPropertyInfo(Func&amp;lt;object&amp;gt; property)&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;// This is the method I&#39;m asking about. &lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
If you like it, please go and vote for it at: &lt;a href=&quot;http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5755035-ability-to-get-propertyinfo-dynamically-passing-th&quot;&gt;http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5755035-ability-to-get-propertyinfo-dynamically-passing-th&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Update&lt;/strong&gt;:&lt;br /&gt;
It turns out what I&#39;ve asked in this post is possible using Expression trees. The following is the working example on how to achieve it:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static class PropertyExtensions&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static PropertyInfo GetPropertyInfo&amp;lt;TSource, TProperty&amp;gt;(this TSource instance, Expression&amp;lt;Func&amp;lt;TSource, TProperty&amp;gt;&amp;gt; propExtractor)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MemberExpression mexp = propExtractor.Body as MemberExpression;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (mexp == null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentException(&quot;Invalid&quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PropertyInfo result = mexp.Member as PropertyInfo;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (result == null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentException(&quot;Invalid expression&quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return result;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/567681814608715205/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/567681814608715205' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/567681814608715205'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/567681814608715205'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2014/04/improving-c-getting-reflectioninformati.html' title='Dynamic Reflection in C#'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-5722523543339935544</id><published>2014-03-11T23:52:00.001+00:00</published><updated>2014-03-11T23:52:10.764+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="cleanup"/><category scheme="http://www.blogger.com/atom/ns#" term="debug"/><category scheme="http://www.blogger.com/atom/ns#" term="debugger"/><category scheme="http://www.blogger.com/atom/ns#" term="execution"/><category scheme="http://www.blogger.com/atom/ns#" term="finally"/><category scheme="http://www.blogger.com/atom/ns#" term="runtime"/><category scheme="http://www.blogger.com/atom/ns#" term="stop"/><category scheme="http://www.blogger.com/atom/ns#" term="try"/><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio"/><title type='text'>Breaking the debugger session like an exception was thrown</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today I cam across a situation, which made me think about an new feature in Visual Studio Debugger.&lt;br /&gt;
But first about the scenario: I&#39;ve been debugging a component test, and wanted to stop the debugger. But the same time - I couldn&#39;t do that, as that won&#39;t take the execution through the normal cleanup flow, defined in a finally block.&lt;br /&gt;
Then I thought about the feature I&#39;ve started with, and here is the link to it: &lt;a href=&quot;http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5624050-stopping-debug-session-to-go-through-the-defined&quot;&gt;http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5624050-stopping-debug-session-to-go-through-the-defined&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
So please read it and vote for it !&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/5722523543339935544/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/5722523543339935544' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5722523543339935544'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5722523543339935544'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2014/03/breaking-debugger-session-like.html' title='Breaking the debugger session like an exception was thrown'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-6176370602996578231</id><published>2014-02-20T06:44:00.004+00:00</published><updated>2014-03-22T00:24:34.073+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="11"/><category scheme="http://www.blogger.com/atom/ns#" term="8.1"/><category scheme="http://www.blogger.com/atom/ns#" term="crash"/><category scheme="http://www.blogger.com/atom/ns#" term="crashing"/><category scheme="http://www.blogger.com/atom/ns#" term="explorer"/><category scheme="http://www.blogger.com/atom/ns#" term="IE"/><category scheme="http://www.blogger.com/atom/ns#" term="IE11"/><category scheme="http://www.blogger.com/atom/ns#" term="internet"/><category scheme="http://www.blogger.com/atom/ns#" term="periodically"/><category scheme="http://www.blogger.com/atom/ns#" term="problem"/><category scheme="http://www.blogger.com/atom/ns#" term="random"/><category scheme="http://www.blogger.com/atom/ns#" term="Win8"/><category scheme="http://www.blogger.com/atom/ns#" term="win8.1"/><category scheme="http://www.blogger.com/atom/ns#" term="windows"/><title type='text'>How I&#39;ve fixed the periodic crashing IE problem</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi there,&lt;br /&gt;
&lt;br /&gt;
Today I&#39;ve finally managed to fix the crashing IE problem, under Windows 8.1. So decided to share my experience, hoping for some of you out there - this post will be in help.&lt;br /&gt;
But first, let me give you a bit of history.&lt;br /&gt;
&lt;br /&gt;
Frankly saying, I&#39;m a Microsoft fan. Yes, I do work in Microsoft as a software developer.&lt;br /&gt;
So you guessed already, that I do use Windows - I love it ! I have had a chance to play with other OS-s as well, so please - no comments about &quot;you haven&#39;t seen others&quot;.&lt;br /&gt;
So, my machine was running Windows 7 for a while already. And one day - Microsoft announces the availability of Windows 8 Preview. Of course - I do install it (on a VM for now). I guess it took some time for me to get used to it, and eventually, I liked it as well. Then - the release, and, as expected, I&#39;m upgrading my main OS to it immediately.&lt;br /&gt;
Let me tell you the truth&amp;nbsp;- that was the best OS upgrade experience I&#39;ve ever went through. No issues, no hassles - and everything works: everything except the most used product - the Internet Explorer, which keeps crashing periodically - about 50 times a day.&lt;br /&gt;
Well, I now realize, that my laziness was keeping me away from trying to look into the actual problem, and fix it. So I was just tolerating that, and using it again and again. By the way, the non-desktop version of IE&amp;nbsp;was&amp;nbsp;ok, no crashes, no issues. And I started using that one more and more.&lt;br /&gt;
And finally, the final crash&amp;nbsp;made me so angry, that I&#39;ve forgot about my laziness, and decided to dig deeper.&lt;br /&gt;
First thing&amp;nbsp;was to open the &lt;strong&gt;Windows Event Log (Windows Logs \ Application)&lt;/strong&gt;, and look for most recent errors: luckily the only errors were from&amp;nbsp;IE (those I&#39;ve been hitting).&amp;nbsp;And the error message was&amp;nbsp;very similar to:&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;Faulting application name: IEXPLORE.EXE, version: 11.0.9600.16518, time stamp: 0x52f347b2&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Faulting module name&lt;/span&gt;&lt;/strong&gt;: &amp;lt;&lt;strong&gt;module name&lt;/strong&gt;&amp;gt;, version: &amp;lt;&lt;strong&gt;module version&lt;/strong&gt;&amp;gt;, time stamp: 0x513d9755&lt;br /&gt;Exception code: 0xc0000005&lt;br /&gt;Fault offset: 0x00046ce7&lt;br /&gt;Faulting process id: 0x4280&lt;br /&gt;Faulting application start time: 0x01cf2df55b897962&lt;br /&gt;Faulting application path: C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Faulting module path&lt;/span&gt;&lt;/strong&gt;: &amp;lt;&lt;strong&gt;the path to the failing module&lt;/strong&gt;&amp;gt;&lt;br /&gt;Report Id: 9aa55680-99e8-11e3-bf29-c48508058295&lt;br /&gt;Faulting package full name: &lt;br /&gt;Faulting package-relative application ID:&lt;/em&gt; &lt;br /&gt;
&lt;br /&gt;
Looking to the &quot;Faulting module path&quot; I guess will give you a clear picture - of what is really failing. In my case that was a file download application. I&#39;ve then just uninstalled that application,&amp;nbsp;and it&#39;s already 4 hours (I would have about 15 crashes already) - no issues.&lt;br /&gt;
Hopefully you&#39;ll be able to handle this now.&lt;br /&gt;
&lt;br /&gt;
Sorry for non development related post.&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Artak&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/6176370602996578231/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/6176370602996578231' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/6176370602996578231'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/6176370602996578231'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2014/02/how-to-fix-periodic-crashing-ie-problem.html' title='How I&#39;ve fixed the periodic crashing IE problem'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-8959486675796989181</id><published>2013-10-17T23:25:00.001+01:00</published><updated>2013-10-17T23:25:34.585+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DateTime"/><category scheme="http://www.blogger.com/atom/ns#" term="DateTime.Now"/><category scheme="http://www.blogger.com/atom/ns#" term="DateTime.UtcNow"/><category scheme="http://www.blogger.com/atom/ns#" term="Now"/><category scheme="http://www.blogger.com/atom/ns#" term="refactor"/><category scheme="http://www.blogger.com/atom/ns#" term="testing"/><category scheme="http://www.blogger.com/atom/ns#" term="Unit Test"/><category scheme="http://www.blogger.com/atom/ns#" term="ut"/><category scheme="http://www.blogger.com/atom/ns#" term="UtcNow"/><title type='text'>Unit Testing DateTime.Now or DateTime.UtcNow</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
TDD is a great methodology, and if you follow it properly and get fanatic about it, then you eventually look for time to refactor all your code to be properly designed - hence - testable.&lt;br /&gt;
Eventually, you&#39;ll come up to a code where you&#39;ve used DateTime.Now or DateTime.UtcNow properties, and start to think, should you really need to test this minor bit? The answer will be &quot;Absolutely YES&quot;. But then, does it worth it to refactor the code for this simple thing?&lt;br /&gt;
If you&#39;re lazy enough and would be happy to solve the problem with some minor risk, then the following technique will be for you.&lt;br /&gt;
Let&#39;s say you want to test the following code - the Save() method:&lt;br /&gt;
&lt;br /&gt;
internal class SampleState&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public DateTime LastSaved {get; set;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Save()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.LastSaved = DateTime.UtcNow;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The proper way would be to mock out the date/time provider and refer to it in the code. Alternatively this simple technique will be helpful:&lt;br /&gt;
&lt;br /&gt;
[TestMethod]&lt;br /&gt;
public void Save_SetsLastSavedTime()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Setup&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SampleState sut = new SampleState();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; DateTime beforeCall = DateTime.UtcNow;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Act&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Save();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; DateTime afterCall = DateTime.UtcNow;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Validate&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.IsTrue(sut.LastSaved &amp;gt;= beforeCall &amp;amp;&amp;amp; sut.LastSaved &amp;lt;= afterCall);&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The technique insures, that the time saved is within the test interval between the call started and ended. As mentioned before - this is not an explicit verification, but still - it gives high confidence that the proper time is being set.&lt;br /&gt;
&lt;br /&gt;
Hope this will be useful !&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/8959486675796989181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/8959486675796989181' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/8959486675796989181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/8959486675796989181'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2013/10/unit-testing-datetimenow-or.html' title='Unit Testing DateTime.Now or DateTime.UtcNow'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-1456938849354501014</id><published>2012-10-10T06:27:00.001+01:00</published><updated>2014-02-21T18:59:22.831+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="child"/><category scheme="http://www.blogger.com/atom/ns#" term="Class"/><category scheme="http://www.blogger.com/atom/ns#" term="derived type"/><category scheme="http://www.blogger.com/atom/ns#" term="Dispose"/><category scheme="http://www.blogger.com/atom/ns#" term="Dispose()"/><category scheme="http://www.blogger.com/atom/ns#" term="IDisposable"/><category scheme="http://www.blogger.com/atom/ns#" term="implementation"/><category scheme="http://www.blogger.com/atom/ns#" term="inheritance"/><category scheme="http://www.blogger.com/atom/ns#" term="method"/><category scheme="http://www.blogger.com/atom/ns#" term="parent"/><category scheme="http://www.blogger.com/atom/ns#" term="polymorphysm"/><category scheme="http://www.blogger.com/atom/ns#" term="virtual"/><title type='text'>The Dispose() method and the inheritance</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;Today I am going to write about the IDisposable interface – it’s Dispose() method, and how this should be really implemented in derived classes. The simplest implementation of Dispose() method will be just introduction of the following code to the class, and that’s all:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;public&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;class&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; : &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;IDisposable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;public&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;void&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; Dispose()&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;// Disposal logic here &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;    }
}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;and this seems to be fine, &lt;strong&gt;but it is not&lt;/strong&gt;.&lt;/span&gt;

&lt;span style=&quot;font-size: x-small;&quot;&gt;Let’s assume for now that everything is fine, and we ship a library with the above code in it. Somebody, who is using the library, can easily use it in the following way:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;public&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;class&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; : &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;public&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;void&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; Dispose()&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;//Class B specific dispose logic in here&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;//And the following call is essential here&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;base&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;.Dispose();&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;    }
}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;This seems to be fine as well, unless someone else, or even the owner of class B will use it in the following way:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;using&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; (&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; item = &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: x-small;&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas; font-size: x-small;&quot;&gt;B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-family: Consolas; font-size: x-small;&quot;&gt;// some logic here&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Consolas; font-size: x-small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;In this case, when exiting from the using block, the Dispose() method of type A will be called, which is sitting in the method table in memory as a separate record, and has nothing related to the Dispose() method of type B. And this will become a possible reason for a memory leak – any unmanaged resources introduced in class B will not be cleaned up !&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;One solution, of course will be to cast item to type B and call it’s Dispose() method explicitly, but that’s just complicating things, because this will need to be done in a finally block.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;So let’s now see what I meant saying “it is not” in the beginning of the article, when was defining class A.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;The thing is the the implementer of a public type must take the responsibility of problems like the one described by implementing types the way, so the users will not need to worry about the usage.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;And the solution is actually simple – &lt;strong&gt;in every case, when introducing a type which implements the IDisposable interface, the Dispose() method must be marked either virtual (and the derived classes should override that method), or whole the class must be marked as sealed&lt;/strong&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;So by defining class A as sealed we will explicitly restrict its usage the way it was described – the incorrect usage. Now let’s see what will change, if we’ll mark the Dispose() method in class A as virtual and in class B override it.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;Remember, this will force a single method record for a derived type to appear in the method table, so no matter how you’ll reference an instance of type B (either through type A, or any other type in the hierarchy) the Dispose method will call the Dispose method of type B, which, assumable (if implemented correct – as in the example above) will call the parent type’s Dispose() method and this will go by chain until the top of the hierarchy, making sure that any resource in whole the hierarchy got disposed.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: arial;&quot;&gt;Good luck with development !&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/1456938849354501014/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/1456938849354501014' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/1456938849354501014'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/1456938849354501014'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/10/the-dispose-method-and-inheritance.html' title='The Dispose() method and the inheritance'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-959131732168345185</id><published>2012-08-02T18:02:00.001+01:00</published><updated>2012-08-02T18:02:15.349+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="abstract"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="generic"/><category scheme="http://www.blogger.com/atom/ns#" term="lock"/><category scheme="http://www.blogger.com/atom/ns#" term="lock(this)"/><category scheme="http://www.blogger.com/atom/ns#" term="monitor"/><category scheme="http://www.blogger.com/atom/ns#" term="net"/><category scheme="http://www.blogger.com/atom/ns#" term="net 4.0"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="synchronization"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><category scheme="http://www.blogger.com/atom/ns#" term="thread safe"/><title type='text'>Synchronization: Working with synchronized resources</title><content type='html'>&lt;p&gt;Today I&#39;m going to write again about the synchronization, but will mainly concentrate on the way of helping to avoid some code duplication, and will save some time. &lt;p&gt;In one of my previous posts I&#39;ve described several ways of synchronizing access to the resource, and what we’ve pointed out there as a general (of course with exceptions) practice each synchronized resource in needs to have an associated object, used for synchronizing access to that resource only, and for nothing else. &lt;p&gt;Thinking about that I came up with the following class definition which I found very handy: &lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; ConcurrentResource&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre&gt;{ &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;readonly&lt;/span&gt; Object syncRoot = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Object(); &lt;/pre&gt;&lt;pre&gt;  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;readonly&lt;/span&gt; T Resource; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  &lt;/pre&gt;&lt;pre&gt;  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; ConcurrentResource(T argResource) &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  {&lt;/pre&gt;&lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (argResource == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;) &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;    { &lt;/pre&gt;&lt;pre&gt;      &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class=&quot;str&quot;&gt;&quot;argResource&quot;&lt;/span&gt;); &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;    }&lt;/pre&gt;&lt;pre&gt;    &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Resource = argResource; &lt;/pre&gt;&lt;pre&gt;  }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type=&quot;text/css&quot;&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, &quot;Courier New&quot;, courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The ConcurrentResource type will encapsulate the object which to the access is going to be synchronized, and will also define internally a read-only property called syncRoot, which will be used for synchronizing the access.&lt;br /&gt;&lt;p&gt;Using that type I found it really handy but let me describe a bit more the use of it and what methods we can add to it to make it handier to work with.&lt;br /&gt;&lt;p&gt;So let&#39;s imagine we want to do some action with locking that resource. The following method will be really handy here, because it will encapsulate the locking mechanism in it as you can see:&lt;br /&gt;&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; HandleActionInSyncronizedContext(Action argAction)&lt;/pre&gt;&lt;pre&gt;{&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt;(argAction == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre&gt;  {&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class=&quot;str&quot;&gt;&quot;argAction&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;  }&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;  &lt;span class=&quot;kwrd&quot;&gt;lock&lt;/span&gt;(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.syncRoot)&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  {&lt;/pre&gt;&lt;pre&gt;    argAction();&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  }&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type=&quot;text/css&quot;&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, &quot;Courier New&quot;, courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;So the method internally will handle the locking mechanism and the caller will not need to use it directly in the code. One advantage of the abstraction, is that it will be very handy when we will need to switch from lock{…} to Monitor.Enter(…) call with timeout in one place, and get it working everywhere. There are bunch of interesting methods we can add to that type, and here are few of those:&lt;br /&gt;&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; WaitForPulse()&lt;/pre&gt;&lt;pre&gt;{&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  Monitor.Wait(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Resource);&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Pulse()&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;{&lt;/pre&gt;&lt;pre&gt;  &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.HandleActionInSyncronizedContext(()=&amp;gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  {&lt;/pre&gt;&lt;pre&gt;    Monitor.PulseAll(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Resource);&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;  });&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type=&quot;text/css&quot;&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, &quot;Courier New&quot;, courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As a complete example of usage of the ConcurrentResource( of T) type you can take a look to the sources of ConsoleHoster project in &lt;a href=&quot;http://consolehoster.codeplex.com/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/959131732168345185/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/959131732168345185' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/959131732168345185'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/959131732168345185'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/08/synchronization-working-with.html' title='Synchronization: Working with synchronized resources'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-5454777832725516924</id><published>2012-07-22T00:46:00.000+01:00</published><updated>2012-07-22T00:47:09.755+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="delegate"/><category scheme="http://www.blogger.com/atom/ns#" term="event"/><category scheme="http://www.blogger.com/atom/ns#" term="handler"/><category scheme="http://www.blogger.com/atom/ns#" term="invocaton"/><category scheme="http://www.blogger.com/atom/ns#" term="invoke"/><category scheme="http://www.blogger.com/atom/ns#" term="iterator"/><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="multicast"/><category scheme="http://www.blogger.com/atom/ns#" term="multicastdelegate"/><category scheme="http://www.blogger.com/atom/ns#" term="net"/><category scheme="http://www.blogger.com/atom/ns#" term="safe"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>Delegates and Events</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today I’d like to write about the events, but because I cannot talk about events without delegates, I’ll cover those as well in this post. &lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
To make the article easier to understand (for new developers at least) let me describe a more real-world example so we’ll be able to model things from it. One such example can be the fire brigades&#39; work – how they react to fires, how they being notified about fires and so on. &lt;br /&gt;
So thinking about it I assume that every fire brigade works for some kind of center, which control’s its work. And every such center has several brigades under its control. So let’s model that center as FireAlarmingCenter class described below: &lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; FireAlarmingCenter
{
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt; font-size: small;&lt;br /&gt; color: black;&lt;br /&gt; font-family: consolas, &quot;Courier New&quot;, courier, monospace;&lt;br /&gt; background-color: #ffffff;&lt;br /&gt; /*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt; background-color: #f4f4f4;&lt;br /&gt; width: 100%;&lt;br /&gt; margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;
&lt;/style&gt;&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, &quot;Courier New&quot;, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 
&lt;/style&gt;&lt;br /&gt;
As we said earlier the FireAlarmingCenter will be one who notifies about fire alerts to the fire brigades. Before we will be able to add that notification capability to the FireAlarmingCenter class, we need to define what the notification will look like. and here where the delegates come in. To specify the details about the specifics of the notification we need to describe those through a delegate, which will define a signature of a method it can reference to, or a method, which can be used as a handler for an event of that delegate type.&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; FireAlarmEventHandler(FireAlarmingCenter argCenter, AlarmDetails argDetails);&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, &quot;Courier New&quot;, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 
&lt;/style&gt;&lt;br /&gt;
Now let’s add the event definition to the FireAlarmingCenter type as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot; dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;event&lt;/span&gt; FireAlarmEventHandler FireAlarm;&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, &quot;Courier New&quot;, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 
&lt;/style&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Great, now we have the FireAlarmingCenter type, so let&#39;s see how this event is going to be consumed.&lt;br /&gt;
As we have said already, the consumers of FireAlarm event will be the fire brigades, which we can model like this: &lt;/div&gt;
&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; FireBrigade
{
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; FireBrigade(FireAlarmingCenter argCenter)
    {
        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (argCenter == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)
            &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class=&quot;str&quot;&gt;&quot;argCenter&quot;&lt;/span&gt;);

        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Center = argCenter;
    }

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; FireAlarmingCenter Center
    {
        get;
        &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; set;
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, &quot;Courier New&quot;, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 
&lt;/style&gt;&lt;br /&gt;
Now we’ve created the base plumbing so the FireBrigade will have the reference to the FireAlarmingCenter it’s “working for”. But there is still a hole there – the FIreBrigade doesn’t somehow “listen” to the FireAlarm event of its center. To do so, we just need to aappend the following line to the FireBrigade class constructor: &lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Center.FireAlarm += &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; FireAlarmEventHandler(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.HandleFireAlarm);&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Although the C# 4.0 language let’s the developers to skip the delegate type after += operation, when registering for events and specify only the method name, which is going to work as handler for that event, I like that style, so its really visible from code what type exactly the event is, so to the handler method signature is. So that line will assume that we’ve defined the “HandleFireAlarm” method already in the FireBrigade class, which will contain logic about what to do in case of a fire.&lt;br /&gt;
Now let&#39;s see how the FireAlarmingCenter will notify the FireBrigade-s about a fire. I’m not going to write or guess what the conditions are on when to notify it, but am really going to concentrate on how it will be fired. The most simple way to do that is as follows:&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnFireAlarm(AlarmDetails argDetails)
{
    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.FireAlarm != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)
    {
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.FireAlarm(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;, argDetails);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, &quot;Courier New&quot;, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 
&lt;/style&gt;&lt;br /&gt;
Very nice and simple, it’s really not what we want. So let’s go and see what is happening behind those lines ? &lt;br /&gt;
So the first line in the method checks whether there are any listeners registered for the FireAlarm event or not. Next, if there are listeners to the FireAlarm event, the most important thing is going to happen – the event is going to be called. To answer the question – “what means an event is called?”, I’ll need to go back a bit to describe what the delegate is.&lt;br /&gt;
So the delegate is a reference of a method, but not only – it is actually a list of references to methods, which all are matching the signature of the delegate. Actually, the definition of the delegate we’ve created is being translated by the compiler to a class definition with the name of that delegate and deriving from an abstract class called “MulticastDelegate”:&lt;br /&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;div class=&quot;csharpcode&quot;&gt;
&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; FireAlarmEventHandler : MulticastDelegate&lt;br /&gt;
{&lt;br /&gt;
} &lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt; font-size: small;&lt;br /&gt; color: black;&lt;br /&gt; font-family: consolas, &quot;Courier New&quot;, courier, monospace;&lt;br /&gt; background-color: #ffffff;&lt;br /&gt; /*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt; background-color: #f4f4f4;&lt;br /&gt; width: 100%;&lt;br /&gt; margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;
&lt;/style&gt;&lt;br /&gt;
So now we can approach to an event as a field of a specified delegate type in a class it’s defined in. So when in the FireBrigade class’s constructor the += operation on the FireAlarm event is being called, it actually adds a new method reference (which is specified on the right from the += operator) to the delegate invocation list.&lt;br /&gt;
Now, when the event (in this case the FireAlarm) is being called, the following happens:An iterator is being created which iterates over all the registered methods to that delegate instance, and each element of that iterator (in this case a method reference) the method is being called with parameters passed in to the event call. But this seems to contain a problem.Imagine a situation when during the event listeneres execution (iterating over the list of handler methods) a new FireBrigade will be constructed for that FireAlarmingCenter – the invocation list will be modified so an exception will be thrown here.But don’t worry – there is a very handy way of handling this and I really like it. If we’ll modify the code like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnFireAlarm(AlarmDetails argDetails)
{
    FireAlarmEventHandler tmpEvent = &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.FireAlarm;
    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (tmpEvent != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)
    {
        tmpEvent(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;, argDetails);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
on the first line in the method the invokation list of the delegate will be copied and the next operations will be working on the copied list including the invocation itself) so adding or removing any new handlers to the FireAlarm event won’t affect the invocation any more. This technique is usually referenced as “Event Invoke Pattern”.&lt;br /&gt;
Visual Studio has a very nice snippet for this: “invoke”. Just type invoke in VS.Net and hit Tab twice (Tab+Tab) and you’ll get the code automatically appear in your source.&lt;br /&gt;
&lt;br /&gt;
So I think that’s all for this post. Good luck with your development.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/5454777832725516924/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/5454777832725516924' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5454777832725516924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5454777832725516924'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/07/delegates-and-events.html' title='Delegates and Events'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-4692069251158430444</id><published>2012-07-15T16:36:00.001+01:00</published><updated>2012-07-15T16:36:27.014+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="app"/><category scheme="http://www.blogger.com/atom/ns#" term="console"/><category scheme="http://www.blogger.com/atom/ns#" term="console-based"/><category scheme="http://www.blogger.com/atom/ns#" term="ConsoleHoster"/><category scheme="http://www.blogger.com/atom/ns#" term="err"/><category scheme="http://www.blogger.com/atom/ns#" term="explorer"/><category scheme="http://www.blogger.com/atom/ns#" term="host"/><category scheme="http://www.blogger.com/atom/ns#" term="idea"/><category scheme="http://www.blogger.com/atom/ns#" term="import"/><category scheme="http://www.blogger.com/atom/ns#" term="log"/><category scheme="http://www.blogger.com/atom/ns#" term="output"/><category scheme="http://www.blogger.com/atom/ns#" term="project"/><category scheme="http://www.blogger.com/atom/ns#" term="stream"/><category scheme="http://www.blogger.com/atom/ns#" term="tab"/><category scheme="http://www.blogger.com/atom/ns#" term="windows"/><title type='text'>The ConsoleHoster app</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi.&lt;br /&gt;
&lt;br /&gt;
The recent 4-5 months I was working on a very interesting project which will most probably be a help for other developers. Feeling quite good about it I finally decided to make it open source.&lt;br /&gt;
So here is the link where it&#39;s published: &lt;a href=&quot;http://consolehoster.codeplex.com/&quot;&gt;http://consolehoster.codeplex.com/&lt;/a&gt;&lt;br /&gt;
Hope you&#39;ll like it, and please leave your feedback there and join me in developing it, if you&#39;re intersted.&lt;br /&gt;
In my future posts I&#39;ll try to cover it in more details.&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Artak&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/4692069251158430444/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/4692069251158430444' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/4692069251158430444'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/4692069251158430444'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/07/consolehoster-app.html' title='The ConsoleHoster app'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-42726115252772144</id><published>2012-07-08T23:29:00.001+01:00</published><updated>2012-07-15T18:23:19.421+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="absolute"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="character"/><category scheme="http://www.blogger.com/atom/ns#" term="exclude"/><category scheme="http://www.blogger.com/atom/ns#" term="expression"/><category scheme="http://www.blogger.com/atom/ns#" term="file"/><category scheme="http://www.blogger.com/atom/ns#" term="folder"/><category scheme="http://www.blogger.com/atom/ns#" term="full"/><category scheme="http://www.blogger.com/atom/ns#" term="group"/><category scheme="http://www.blogger.com/atom/ns#" term="net"/><category scheme="http://www.blogger.com/atom/ns#" term="path"/><category scheme="http://www.blogger.com/atom/ns#" term="pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="prefix"/><category scheme="http://www.blogger.com/atom/ns#" term="RegEx"/><category scheme="http://www.blogger.com/atom/ns#" term="regular"/><category scheme="http://www.blogger.com/atom/ns#" term="relative"/><title type='text'>Regex: Identifying paths</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi again and thank you for visiting my blog.&lt;br /&gt;
&lt;br /&gt;
It&#39;s already three months I&#39;m working on a project which again, as many other projects I&#39;m working/worked on, started from just an idea.&lt;br /&gt;
So one of the challenges I met in this project&amp;nbsp;was to&amp;nbsp;come up with a regular expression, which will identify whether the given part of the string is a path, part of it, or relative one.&lt;br /&gt;
Remember (or if you don&#39;t know try) the TAB support in windows command window - you can type in a relative or full path, then type some prefix and on each sequentiall hit of&amp;nbsp;TAB button, the file/folder names, which are in the specified path and start with the typed prefix, will be placed like auto-completition feature one after another.&lt;br /&gt;
So I finally came up with the following regular expressoin:&lt;br /&gt;
&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&quot;((((^\w:|\s\w:)|\.\.?)(\/|\\))?(([\w.]+( [\w.]+)?)(\/|\\))*)|((((^\w:|\s\w:)|\.\.?)(\/|\\))?(([\w.]+([\w.]+)?)(\/|\\))*))([\w\.]*)\t&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
Because it&#39;s quite complex, the processing was really taking long. First I tried to compile that regular expression into a separate assembly and then use the compiled one, but unfortunately it affected the performance harder, than earlier. Eventually I understood that I don&#39;t need all the groups defined in the pattern, so I removed those (marked those not to be counted as groups using (?:...)), and it increased the performance really good. So here what I ended up with:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: x-small;&quot;&gt;&quot;(?:(?:(?:(^\w:|\s\w:)|\.\.?)(?:\/|\\))?(?:(?:[\w.]+(?: [\w.]+)?)(?:\/|\\))*)|(?:(?:(?:(^\w:|\s\w:)|\.\.?)(?:\/|\\))?(?:(?:[\w.]+(?:[\w.]+)?)(?:\/|\\))*))([\w\.]*)\t&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Althought the compiled query execution time need to be shorter than the non compiled one, I haven&#39;t went into details why it was like that, but the fact is fact - that&#39;s true. I&#39;m pretty sure that for shorter regular expressions that will work faster, but no in this case.&lt;br /&gt;
&lt;br /&gt;
Hope the regex will be helpful.&lt;br /&gt;
&lt;br /&gt;
Enjoy .Net Development !!!&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/42726115252772144/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/42726115252772144' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/42726115252772144'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/42726115252772144'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/07/regex-identifying-paths.html' title='Regex: Identifying paths'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-377670292516898093</id><published>2012-04-09T23:45:00.001+01:00</published><updated>2012-04-12T01:32:35.170+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="deadlock"/><category scheme="http://www.blogger.com/atom/ns#" term="Enter"/><category scheme="http://www.blogger.com/atom/ns#" term="Exist"/><category scheme="http://www.blogger.com/atom/ns#" term="finally"/><category scheme="http://www.blogger.com/atom/ns#" term="identify"/><category scheme="http://www.blogger.com/atom/ns#" term="lock"/><category scheme="http://www.blogger.com/atom/ns#" term="monitor"/><category scheme="http://www.blogger.com/atom/ns#" term="net"/><category scheme="http://www.blogger.com/atom/ns#" term="syncronization"/><category scheme="http://www.blogger.com/atom/ns#" term="syncronize"/><category scheme="http://www.blogger.com/atom/ns#" term="TryEnter"/><title type='text'>Syncronization: deadlocks and how to identify those</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
These days synchronization become very actual problem for any developer - nowdays its really hard to even imagine a single threaded application. Of course with this, some headache arises with deadlocks. Usually developers use &lt;em&gt;lock(...) { }&lt;/em&gt; statement to synchronize access to some code block, and that is one possible reason of a deadlock. Don&#39;t get me wrong - I don&#39;t say that if you&amp;nbsp;use lock statement, then you have a deadlock, but what I say, is that if you have a deadlock - then&amp;nbsp;its definitely happening on one of the lock statements you&#39;ve used in your code. So what&amp;nbsp;to do to avoid those ? Or actually, which will be more correct to say, how to identify the&amp;nbsp;lock statement&amp;nbsp;where the thread blocks causing a deadlock forever ?&lt;br /&gt;
Fortunately, as almost always, there is a very handy option under the hand - The &lt;strong&gt;Monitor&lt;/strong&gt; class. You probably know, that the lock (obj){...} statements translates to the following code:&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Monitor.Enter(obj);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // ...&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Montior.Exit(obj);&lt;br /&gt;
}&lt;br /&gt;
To make this code &quot;deadlock-proof&quot;, you need to replace the Monitor.Enter method call as follows:&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(!Monitor.TryEnter(obj, TimeSpan.FromSeconds(2))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Log deadlock reason in here&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new Exception(&quot;Unable to take exclusive lock on the given object&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // ...&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Montior.Exit(obj);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Here we are passing a TimeStamp parameter to the TryEnter method, specifying the amount of time to wait for the lock. So if during the specified time period it is impossible to take lock over the specified object, then the method will return false, and we&#39;ll be able to log something, which will tell us the &lt;span style=&quot;font-family: inherit;&quot;&gt;place in code where the actual deadlock is happening. Of course it&#39;s not so convenient to use whole this construct in code&lt;/span&gt;, so this can be extracted to an extension method like the following:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;public&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;static&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;void&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; SyncronizeCallByLocking(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;this&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;Action&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; argAction, &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;object&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; argLock)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; // Argument validation in here&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;nbsp;if&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; (!&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;Monitor&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;.TryEnter(argLock, TimeSpan.FromSeconds(2000)))&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;{&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Log exception in here, if required&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;nbsp; throw&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;ApplicationException&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: maroon; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: maroon; font-family: Consolas;&quot;&gt;&quot;Unable to lock the object&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&amp;nbsp; try&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; argAction();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&amp;nbsp; finally&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&lt;span style=&quot;color: #2b91af; font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Monitor&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;.Exit(argLock);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;After this it&#39;s really easy to wrap the code block, which needs to be synchronized like this:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;new Action(()=&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Code which needs to be syncronized&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; }).&lt;strong&gt;SyncronizeCallByLocking&lt;/strong&gt;(objectToLock);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;By using this technique you will definitely not waste many hours of struggling by trying to find the deadlock reasons in your application, and guessing where those can arise from. Of course, if you want after fixing all the issues you&#39;re free to convert back to using the lock statement instead of what was described, but I won&#39;t bother about it, because you don&#39;t know what changes may come later, and how those may affect your application.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Good luck with your development.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/377670292516898093/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/377670292516898093' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/377670292516898093'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/377670292516898093'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/04/syncronization-deadlocks-and-how-to.html' title='Syncronization: deadlocks and how to identify those'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-5623514638391789612</id><published>2012-03-27T09:19:00.001+01:00</published><updated>2012-03-27T09:19:26.236+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="CreateNoWindow"/><category scheme="http://www.blogger.com/atom/ns#" term="DataReceivedEventHandler"/><category scheme="http://www.blogger.com/atom/ns#" term="Diagnostics"/><category scheme="http://www.blogger.com/atom/ns#" term="message"/><category scheme="http://www.blogger.com/atom/ns#" term="NewLine"/><category scheme="http://www.blogger.com/atom/ns#" term="OutputDataReceived"/><category scheme="http://www.blogger.com/atom/ns#" term="Process"/><category scheme="http://www.blogger.com/atom/ns#" term="ProcessStartInfo"/><category scheme="http://www.blogger.com/atom/ns#" term="read"/><category scheme="http://www.blogger.com/atom/ns#" term="RedirectStandardOutput"/><category scheme="http://www.blogger.com/atom/ns#" term="UseShellExecute"/><title type='text'>Reading redirected output of the process: limitations and how to work around</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;


&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;During last month
I was working on a project built on top of the Console-windows. An interesting
problem I was facing is the read of redirected output (asyncronous)&amp;nbsp;from
that process. It seems to be quite easy to handle such a task, and it is, unless
you need to read &quot;all the output&quot;.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;Well,&amp;nbsp;the
simple approach of handling&amp;nbsp;such a task is like the following:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;1. Define a
ProcessStartInfo with redirected output:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ProcessStartInfo&amp;nbsp;tmpStartInfo = &lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt;
ProcessStartInfo()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileName = &lt;span style=&quot;color: #a31515;&quot;&gt;&quot;consoleApp.exe&quot;,&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UseShellExecute = &lt;span style=&quot;color: blue;&quot;&gt;false,&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RedirectStandardOutput = &lt;span style=&quot;color: blue;&quot;&gt;true&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;2. Create a
process and set the start info to alrelady defined one:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ProcessStartInfo&amp;nbsp;tmpProcess = &lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt;
Process()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tmpProcess.StartInfo
= tmpStartInfo;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;3. Define and
register a handler for the OutputDataReceived event:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; Process_OutputDataReceived(&lt;span style=&quot;color: blue;&quot;&gt;object&lt;/span&gt; sendingProcess, DataReceivedEventArgs outLine)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(outLine);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
tmpProcess.OutputDataReceived += &lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt;
DataReceivedEventHandler (Process_OutputDataReceived);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;4. Start the
process.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
tmpProcess.Start();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;Yes, it&#39;s simple,
but there is an open issue left in here. The process won&#39;t redirect the output,
until there is a new-line character in the stream, which means, that the
Process will fire the OutputDataReceived event only as soon as a new line is
available on the stream. So for me this was a big limitation, because if the
process is writing something on a line and still waits for some input from the
user on the same line, we will never receive that line, and won&#39;t even know
that the process is waiting for user input there.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;To fix this we
need to observe the stream on our own, not awaiting the event to fire. So, here
is the algorithm I came up with, to handle the behavior described:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;
ObserveStreamInBackground()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;
try&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;? tmpStreamChar = &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style=&quot;color: blue;&quot;&gt;do&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; tmpStatus = &lt;span style=&quot;color: blue;&quot;&gt;this&lt;/span&gt;.Stream.Peek();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; if&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; (tmpStatus ==
-1)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
if&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; (&lt;span style=&quot;color: blue;&quot;&gt;this&lt;/span&gt;.messageBuffer.Length &amp;gt; 0)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; this&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;.InformMessage();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
tmpStreamChar = (&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;)&lt;span style=&quot;color: blue;&quot;&gt;this&lt;/span&gt;.Stream.Read();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; char&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; tmpCurrentChar =
tmpStreamChar ?? (&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;)&lt;span style=&quot;color: blue;&quot;&gt;this&lt;/span&gt;.Stream.Read();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; if&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;
(tmpStreamChar.HasValue)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
tmpStreamChar = &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; this&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;.messageBuffer.Append(tmpCurrentChar);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; if&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; (tmpCurrentChar
== &lt;span style=&quot;color: maroon;&quot;&gt;&#39;\n&#39;&lt;/span&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
this&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;.InformMessage();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
} &lt;span style=&quot;color: blue;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: blue;&quot;&gt;true&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;
catch&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; (&lt;span style=&quot;color: #2b91af;&quot;&gt;ThreadAbortException&lt;/span&gt; ex)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;
InformMessage()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;
string&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt; tmpMessage = &lt;span style=&quot;color: blue;&quot;&gt;this&lt;/span&gt;.messageBuffer.ToString();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;
this&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;.messageBuffer.Clear();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;
this&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;.lastMessage =
tmpMessage;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;
this&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;.lastMessageReceivedAt
= &lt;span style=&quot;color: #2b91af;&quot;&gt;DateTime&lt;/span&gt;.Now;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-family: Calibri;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;
this&lt;/span&gt;&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;.OnMessageReceived(tmpMessage);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 0pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;The
InformMessage() method is there to just clear the current message buffer and
fire the event with the message data in it.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin: 0in 0in 6pt;&quot;&gt;
&lt;span style=&quot;font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: Calibri;&quot;&gt;That&#39;s all. Experiment with it and
give me your feedback. Good Luck !&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/5623514638391789612/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/5623514638391789612' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5623514638391789612'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5623514638391789612'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/03/reading-redirected-output-of-process.html' title='Reading redirected output of the process: limitations and how to work around'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-2132491965500531026</id><published>2012-03-09T02:38:00.000+00:00</published><updated>2012-03-15T08:45:46.793+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Activator"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Class"/><category scheme="http://www.blogger.com/atom/ns#" term="Create"/><category scheme="http://www.blogger.com/atom/ns#" term="generic"/><category scheme="http://www.blogger.com/atom/ns#" term="GetGenericArguments"/><category scheme="http://www.blogger.com/atom/ns#" term="GetGenericTypeDefinition"/><category scheme="http://www.blogger.com/atom/ns#" term="Instance"/><category scheme="http://www.blogger.com/atom/ns#" term="MakeGenericType"/><category scheme="http://www.blogger.com/atom/ns#" term="methodinfo"/><category scheme="http://www.blogger.com/atom/ns#" term="reflection"/><category scheme="http://www.blogger.com/atom/ns#" term="type"/><title type='text'>Reflection: working with generic types</title><content type='html'>&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;An interesting problem I was facing during last few days drives me to write this post.&amp;nbsp;The problem&amp;nbsp;was to create a generic type instance (let&#39;s say List&lt;t&gt;), the type parameter for which&amp;nbsp;is known only during runtime. Let&#39;s examine the following code fragment:&lt;br /&gt;
&lt;pre&gt;Type genericListOfInt = typeof(List&amp;lt;int&amp;gt;);
Type genericListOfString = typeof(List&amp;lt;string&amp;gt;);
bool sameType = genericListOfInt&amp;nbsp;== genericListOfString;
//sameType&amp;nbsp;is false here, so those Types are differenet
&lt;/pre&gt;Both the variables seems to be instances of the same List&lt;t&gt; (List of T)&amp;nbsp;type, but because the T type parameter is different the compiler will generate two different classes for List&lt;int&gt; and List&lt;string&gt; so the variables will not be equal. But how to handle the problem we do face ?&lt;/string&gt;&lt;/int&gt;&lt;/t&gt;&lt;br /&gt;
Examining the &lt;em&gt;System.Type&lt;/em&gt; class we will find out a very interesting method called:&lt;strong&gt;GetGenericTypeDefinition()&lt;/strong&gt; method. This method returns a Type object, representing the generic type definition, from which the type, which for the method was called, was constructed, so actually the type, which can be used to construct other Generic types as well.&lt;br /&gt;
So modifying the code above will give us the following:&lt;br /&gt;
&lt;pre&gt;Type genericListOfInt = typeof(List&amp;lt;int&amp;gt;);
Type genericListOfString = typeof(List&amp;lt;string&amp;gt;);

bool sameType =&amp;nbsp;genericListOfInt == genericListOfString;
//sameType variable is true now, so both variables are referencing the same Type instance
&lt;/pre&gt;Apparently, the type, which is reffered by both genericListOfInt and genericListOfString variables can be accessed with the following variable as well:&lt;br /&gt;
&lt;pre&gt;Type genericList = typeof(List&amp;lt;&amp;gt;);
&lt;/pre&gt;This makes it really handy, so we don&#39;t want&amp;nbsp;the compiler to generate any classed which are actually useless just to get the reference to a generic type.&lt;br /&gt;
Now this type can be used to construct different Lists for different types dynamically (List&amp;lt;[any type you want here]&amp;gt;). So here is a simple code for constructing a list of the given type:&lt;br /&gt;
&lt;pre&gt;public static object CreateGenericList(Type ofType)
{
  Type genericListType = typeof(List&amp;lt;&amp;gt;);
  return Activator.CreateInstance(genericListType.MakeGenericType(new Type[]{ofType}));
}
&lt;/pre&gt;Please note also, that the List type was taken for example only, so to access multi-type based generic type (like KeyValuePair&amp;lt;k,v&amp;gt;) you can easily use the same technique:&lt;br /&gt;
&lt;pre&gt;Type genericKVPair = typeof(KeyValuePair&amp;lt;,&amp;gt;);
&lt;/pre&gt;Now, the last scenario left to discuss, is how to dynamically identify the generic type parameters a type has (string in case of List&lt;string&gt; and (int, string) in case of KeyValuePair&lt;int, string&gt;)? The Type class defines another interesting method called &lt;i&gt;GetGenericArguments()&lt;/i&gt;, which returns an Array of Type objects. Combining this method with &lt;i&gt;MakeGenericType&lt;/i&gt; will let us write the following code, which is used to build a KeyValuePair of Types, which are the switch from the original one by type parameter places (for KeyValuePair&lt;int, string&gt; it will return KeyValuePair&lt;string, int&gt;):&lt;br /&gt;
&lt;pre&gt;public object CreateInstnaceOfTheSameType(object original)
{
  if (original == null)
  {
    return null;
  }

  Type origType = original.GetType();
  if (!origType.IsGenericType || origType.GetGenericTypeDefinition() != typeof(KeyValuePair&lt;,&gt;)
  {
    throw new ArgumentException(&quot;KeyValuePairs expected&quot;);
  }

  Type[] typeArgs = origType.GetGenericArguments();
  Type typeToConstruct = origType.GetGenericTypeDefinition().MakeGenericType(new Type[] { typeArgs[1], typeArgs[0]});
  object[] constructorParams;
  // I&#39;m skipping the code for constructorParams initialization, cause it&#39;s out of scope
  return Activator.CreateInstance(typeToConstruct, constructorParams);
}
&lt;/pre&gt;Hope you will find this post helpful.&lt;br /&gt;
&lt;br /&gt;
Regards...&lt;/t&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/2132491965500531026/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/2132491965500531026' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/2132491965500531026'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/2132491965500531026'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/03/reflection-working-with-generic-types.html' title='Reflection: working with generic types'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-618434897303901888</id><published>2012-02-18T23:22:00.001+00:00</published><updated>2012-02-18T23:22:43.532+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="context"/><category scheme="http://www.blogger.com/atom/ns#" term="Enter"/><category scheme="http://www.blogger.com/atom/ns#" term="exit"/><category scheme="http://www.blogger.com/atom/ns#" term="lock"/><category scheme="http://www.blogger.com/atom/ns#" term="lock(this)"/><category scheme="http://www.blogger.com/atom/ns#" term="monitor"/><category scheme="http://www.blogger.com/atom/ns#" term="syncronization"/><category scheme="http://www.blogger.com/atom/ns#" term="syncronize"/><title type='text'>Syncronization with lock()  - bad habits</title><content type='html'>Hi all.&lt;br /&gt;
&lt;br /&gt;
Today I wanted to touch the lock statement in C#, and how many developers used to use it. There are a lot of books on C#, where authors bring examples like the following:&lt;br /&gt;
&lt;pre&gt;public class LibraryType
{
  public void X()
  {
    lock (this)
    {
      // some actions
    }
  }
}
&lt;/pre&gt;I&#39;d like to touch this because many developers are using the same technique of syncronizing access to a shared resource. There is nothing wrong in such a code, but there is a potential problem hidden in it.&lt;br /&gt;
What if somebody, who is going to use the library (which contains above written code) will decide to syncronize access to some shared resource using an instnace of a type, which contains above shown method X()?&lt;br /&gt;
In single threaded application there will be no problems at all, but in case of multithreaded applicaiton, the caller will face a &lt;b&gt;deadlock&lt;/b&gt;. Take a look to the following code example:&lt;br /&gt;
&lt;pre&gt;// This code is in different assembly
internal class WrapperClass
{
  private LibraryType libTypeInstance = new LibraryType();

  private void XCaller()
  {
    lock(this.libTypeInstance)
    {
      Thread tmpThread = new Thread(new ThreadStart(()=&gt;this.libTypeInstance.X()));
      tmpThread.Start();
      tmpThread.Join();
    }
  }
}
&lt;/pre&gt;The thread which is calling the method XCaller() will lock the libTypeInstance object, and then will create another Thread inside, which will try to lock the &lt;b&gt;same object instnace&lt;/b&gt; from the inner X() method. So that thread will wait untill the object is not unlocked, but for this to happen that thread should finish its job. So the application will halt here.&lt;br /&gt;
So the prefered technique in syncronization scenarios will be to keep a private instancec of an Object type and lock that instance, instead of locking the object (this). This will result in the following change in the code:&lt;br /&gt;
&lt;pre&gt;public class LibraryType
{
  private Object syncObject = new Object();

  public void X()
  {
    lock (this.syncObject)
    {
      // some actions
    }
  }
}
&lt;/pre&gt;&lt;br /&gt;
After the following change the above mentioned example will work with no problems.&lt;br /&gt;
&lt;br /&gt;
Thanks for reading.</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/618434897303901888/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/618434897303901888' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/618434897303901888'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/618434897303901888'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/02/syncronization-with-lock-bad-habits.html' title='Syncronization with lock()  - bad habits'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-1372134564224950291</id><published>2012-01-29T22:45:00.002+00:00</published><updated>2012-01-29T22:45:43.645+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="3-rd party"/><category scheme="http://www.blogger.com/atom/ns#" term="AppDomain"/><category scheme="http://www.blogger.com/atom/ns#" term="applicaiton"/><category scheme="http://www.blogger.com/atom/ns#" term="Component"/><category scheme="http://www.blogger.com/atom/ns#" term="Domain"/><category scheme="http://www.blogger.com/atom/ns#" term="event"/><category scheme="http://www.blogger.com/atom/ns#" term="Exception"/><category scheme="http://www.blogger.com/atom/ns#" term="exit"/><category scheme="http://www.blogger.com/atom/ns#" term="external"/><category scheme="http://www.blogger.com/atom/ns#" term="log"/><category scheme="http://www.blogger.com/atom/ns#" term="net 4.0"/><category scheme="http://www.blogger.com/atom/ns#" term="Process"/><category scheme="http://www.blogger.com/atom/ns#" term="terminate"/><category scheme="http://www.blogger.com/atom/ns#" term="unhandled"/><category scheme="http://www.blogger.com/atom/ns#" term="UnhandledException"/><title type='text'>AppDomains - Why and How ?</title><content type='html'>Finally I&#39;ve found time for this post, which I was planning last year already.&lt;br /&gt;
So, where you may need an AppDomain in your application, how you&#39;ll treat it and what are the benefits of using it? I&#39;ll try to answer to those question. Read further.&lt;br /&gt;
Let&#39;s start from the theory: An Application Domain represents an isolated environment in a managed .net application process. Saying roughly those are sub-process layers, where the code will execute. The important features of Applicaiton Domains I&#39;d like to mention here are the following:&lt;br /&gt;
1. An applicaiton domain has its own - isolated from others, memory&lt;br /&gt;
2. Those can be loaded and unloaded dynamically. Unloading an applicaiton domain will unload all the assemblies loaded in that applicaiton domain (there are still some cases when this is not true, although non-practical cases).&lt;br /&gt;
Now let&#39;s try to find a usage. Imagine a scenario, where the applicaiton needs to load a component from non-trusted source. There are several issues with this, and the most important for me are Security and Reliability.&lt;br /&gt;
&lt;b&gt;Security&lt;/b&gt;: you never know what the code in that component is doing behind the scenes.&lt;br /&gt;
&lt;b&gt;Reliability&lt;/b&gt;: the component can be non-well designed, leaving holes for exceptions to arise.&lt;br /&gt;
Let&#39;s first concentrate on the case, if we are going to handle such scenario without application domains. Of course for interaction with that third-party component there will be a well-defined interface, and every call of a method of that interface (actually an instnace of 3-rd party component, which I will call &lt;b&gt;Component X&lt;/b&gt; further) will be wrapped in a try-catch block. But what if the component does something else behind the scenes ? For example it can has a background thread which does some other processing. In this case, your handlers won&#39;t be able to handle exceptions coming from that background processed code, because those are not on the stack of a thread you&#39;ve just called a method in. So what will happen ? I won&#39;t go into details here, but will say that an exception coming out from such backgroun thread (which is not properly caught in Component X, will finally take your process down, and you&#39;ll have no chance to react, cause have no handlers at all.&lt;br /&gt;
Don&#39;t blame me here, cause I&#39;m not going into some special cases here, like WinForms, where you can handle all the exceptions on the Application level by registering to ThreadException event.&lt;br /&gt;
On the reliability side - you don&#39;t know what the code is doing from data-accessing perspective. So there are limited choices for you to react here - limit your own application permissions to not allow the Component X to do something wrong. But still, you will share the same address space in memory with that component.&lt;br /&gt;
Ok, now let&#39;s jump to the usage of application domains for this scenario, and see how those will solve the above mentioned problems.&lt;br /&gt;
First, in FCL (Framework Class Library) there is a type called AppDomain, which is handling management and interaction with application domains. So let me from now on call application domains just AppDomain for the sake of simplicity only.&lt;br /&gt;
Well, the proper design for the above scenario will be the following:&lt;br /&gt;
1. Create an AppDomain&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
AppDomain tmpDomain = AppDomain.CreateDomain(&quot;ComponentX Domain&quot;);&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
2. Register for the UnhandledException event of that AppDomain&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
tmpDomain.UnhandledException += new UnhandledExceptionEventHandler(OnComponentDomain_UnhandledException);&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
3. Add proper permissions to that AppDomain (this is called sandboxing - limiting permissions)&lt;br /&gt;
// Leaving code for this section, because this is out of scope from this article&lt;br /&gt;
4. Load the Component X assembly into that AppDomain&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
Assembly tmpComponentAssembly = tmpDomain.Load(AssemblyName.GetAssemblyName(&quot;./ComponentX.dll&quot;));&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
5. Create Component X instance in that AppDomain.&lt;br /&gt;
6. Create a proxy in our AppDomain to interact with Component X instance in the new-created domain(because the memory of each AppDomain is isolated, the process of passing data between AppDomain-s involves serialization and deserialization of that data, and whole the process is refered as Marshalling).&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
Type tmpComponentType = GetComponentTypeFromAssembly(tmpComponentAssembly, typeof(IComponentContract).Name);&lt;br /&gt;
IComponentContract tmpComponentProxy = tmpDomain.CreateInstanceAndUnwrap(tmpComponentAssembly.FullName, tmpComponentType.FullName) as IComponentContract;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
7. After finishing working with Component X unload AppDomain of Component X to free up memory. This will also unload the assembly loaded in that AppDomain.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
// This call is missing in the example solution attached to this article.&lt;br /&gt;
tmpDomain.Unload();&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
Great, this solves the security problem and what about the reliability? The AppDomain type (in Net Framework 4.0) defines an event called &lt;b&gt;UnhandledException&lt;/b&gt;. Unfortunately there is no way to handle that exception and continue the normal flow of the application, but the good thing here is that you get your &quot;last-chance&quot; to save state, or log the exception, and even schedule a task in OS to start a new instance of your application. There are several important aspects you should be aware of regarding this event: &lt;i&gt;&quot;This event can be handled in any Applicaiton Domain, although its not necessarily raised in the application domain, where the exception occured. An exception is unhandled only if the entire stack for the thread has been unwound without finding an applicable exception handler, so the first place the event can be raised is in the application domain where the thread originated.&lt;br /&gt;
If the UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in. If the thread started in an application domain that has an event handler for UnhandledException, the event is raised in that application domain. If that application domain is not the default application domain, and there is also an event handler in the default application domain, the event is raised in both application domains.&quot;&lt;/i&gt; The quoted part I&#39;ve taken from MSDN documentation cause it very well explains that event propogation. I still recommend you to read the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx&quot; target=&quot;_blank&quot;&gt;MSDN page&lt;/a&gt;.&lt;br /&gt;
In the section above I&#39;ve specified the exact version of Net Framework because in the earlier versions the behavior was slightly different. Here is another section from MSDN for earlier versions: &lt;i&gt;&quot;In the .NET Framework versions 1.0 and 1.1, an unhandled exception that occurs in a thread other than the main application thread is caught by the runtime and therefore does not cause the application to terminate. Thus, it is possible for the UnhandledException event to be raised without the application terminating. Starting with the .NET Framework version 2.0, this backstop for unhandled exceptions in child threads was removed, because the cumulative effect of such silent failures included performance degradation, corrupted data, and lockups, all of which were difficult to debug.&quot;&lt;/i&gt;&lt;br /&gt;
I&#39;ve also created a small solution with code representing the scenario described in this article. Click &lt;a href=&quot;https://skydrive.live.com/redir.aspx?cid=024cbf27c999610b&amp;resid=24CBF27C999610B!221&amp;parid=24CBF27C999610B!220&amp;authkey=!AOhXyxqC-KaJJxk&quot; target=&quot;_blank&quot;&gt;here &lt;/a&gt;to download the example codes.</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/1372134564224950291/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/1372134564224950291' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/1372134564224950291'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/1372134564224950291'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/01/appdomains-why-and-how.html' title='AppDomains - Why and How ?'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-4398894542120339608</id><published>2012-01-27T15:56:00.002+00:00</published><updated>2012-01-27T15:56:51.255+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="=="/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="compare"/><category scheme="http://www.blogger.com/atom/ns#" term="comparison"/><category scheme="http://www.blogger.com/atom/ns#" term="Equals"/><category scheme="http://www.blogger.com/atom/ns#" term="IPAddress"/><category scheme="http://www.blogger.com/atom/ns#" term="operator"/><category scheme="http://www.blogger.com/atom/ns#" term="parse"/><title type='text'>Comparing IPAddress-es</title><content type='html'>Recently I was struggling with a LINQ expression, where I had a comparison of two ip addresses. Like used to - the comparison was done by the == operator, which was the cause of the headache.&lt;br /&gt;
NEVER USE == WITH IPAddress.&lt;br /&gt;
Seems IPAddress have no its own overrie for ==, and because it&#39;s a reference type that operator is just comparing two addresses(whether those point to the same location in memory or not). And I was getting false for equal ip address comparison.&lt;br /&gt;
You can just try to do the following:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
    bool areEqual = IPAddress.Parse(&quot;150.148.1.54&quot;) == IPAddress.Parse(&quot;150.148.1.54&quot;);&lt;br /&gt;
    // the areEqual now is false&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
So the proper way to compare them is to use the Equals method, which IPAddress type has an override for:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
    bool areEqual = IPAddress.Parse(&quot;150.148.1.54&quot;).Equals(IPAddress.Parse(&quot;150.148.1.54&quot;));&lt;br /&gt;
    // and now the areEqual will be true&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Good luck with your coding.&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/4398894542120339608/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/4398894542120339608' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/4398894542120339608'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/4398894542120339608'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2012/01/comparing-ipaddress-es.html' title='Comparing IPAddress-es'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-9047645610255838461</id><published>2011-11-21T08:42:00.001+00:00</published><updated>2011-11-21T10:48:54.050+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AppDomain"/><category scheme="http://www.blogger.com/atom/ns#" term="Application Pool"/><category scheme="http://www.blogger.com/atom/ns#" term="AppPool"/><category scheme="http://www.blogger.com/atom/ns#" term="Architecture"/><category scheme="http://www.blogger.com/atom/ns#" term="ASP.Net"/><category scheme="http://www.blogger.com/atom/ns#" term="aspnet_wp.exe"/><category scheme="http://www.blogger.com/atom/ns#" term="Domain"/><category scheme="http://www.blogger.com/atom/ns#" term="IIS"/><category scheme="http://www.blogger.com/atom/ns#" term="Process"/><title type='text'>ASP.Net: Application Pools, Application Domains and/or processes</title><content type='html'>Hi all.&lt;br /&gt;
&lt;br /&gt;
Today I just wanted to bring some light on the way ASP.Net handles requests. So let&#39;s start from scratch.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is ASP.Net itself from OS perspective ?&lt;/b&gt;&lt;br /&gt;
ASP.Net is just a process called &lt;b&gt;aspnet_wp.exe&lt;/b&gt;. How in that case it&#39;s stable against application crashes - read further.&lt;br /&gt;
&lt;br /&gt;
In the last question we&#39;ve mentioned something called &lt;b&gt;Application&lt;/b&gt;.What is that ? Ok, Application is an atomic unit of the ASP.Net process - it&#39;s the type of the objects, which are responsible for processing client requests. But there is a good and important trick here, which ASP.Net process takes care of - the &lt;b&gt;Application Domains&lt;/b&gt;. In fact, for each application (which is represented by a vritual directory in IIS) ASP.Net creates a separate AppDomain, and everything related to specific application happens in the boundaries of that domain. This makes one Application be resistant against others&#39; crashes.&lt;br /&gt;
So for each request coming from the client, ASP.Net &lt;b&gt;&quot;creates&quot;&lt;/b&gt; (I will cover this a bit later) a separate Application type instance (Application object) to handle that request. Why I&#39;ve quoted the &lt;b&gt;creates&lt;/b&gt; word, is because that&#39;s not the case and there is a nice optimization done under the hood: as soon as the application being started (AppDomain for that application being created), ASP.Net creates a pool of up to 100 instances of Application classes to not spend time on heavy object-creation operation on each request. So, finally, what we get, is that for each request a &quot;random&quot; existing Application instance processes that request.&lt;br /&gt;
This brings us to one important point: &lt;b&gt;Never keep state in Application instance&lt;/b&gt;, or even if you need to take into account the fact that the instnace you&#39;ve written some state to most probably will be chosen to process request from other clients as well.&lt;br /&gt;
All this is great, but seems we&#39;re missing something: where do the &lt;b&gt;Application Pools&lt;/b&gt; stand in whole this architecture ? Ok, time to change your understanding about Application Pools, because those are just number of settings, which can be applied to different applications. Applicaiton Pools are just a conveniet way to group important settings alltogether and then be able to apply them to any application you&#39;d like to.&lt;br /&gt;
&lt;br /&gt;
I think this is all for this post.&lt;br /&gt;
Good luck in your development.</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/9047645610255838461/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/9047645610255838461' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/9047645610255838461'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/9047645610255838461'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2011/11/aspnet-application-pools-application.html' title='ASP.Net: Application Pools, Application Domains and/or processes'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6433679371380083688.post-5832699568601312675</id><published>2011-11-02T21:43:00.000+00:00</published><updated>2011-11-02T21:45:01.538+00:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="development"/><category scheme="http://www.blogger.com/atom/ns#" term="dual"/><category scheme="http://www.blogger.com/atom/ns#" term="Game"/><category scheme="http://www.blogger.com/atom/ns#" term="monitor"/><category scheme="http://www.blogger.com/atom/ns#" term="screen"/><category scheme="http://www.blogger.com/atom/ns#" term="Silverlight"/><category scheme="http://www.blogger.com/atom/ns#" term="Tetris+"/><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio"/><category scheme="http://www.blogger.com/atom/ns#" term="Windows Phone"/><category scheme="http://www.blogger.com/atom/ns#" term="WP7"/><category scheme="http://www.blogger.com/atom/ns#" term="XAML"/><title type='text'>Better Work Environment: Bigger Screens</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyjlGM31T8voo4G-Og0b6cWkRX2wbY6BHfi1zXtQOmtKNdJ-iPlwc1afpwN43exmgopPO8qBr1BpeL78rwtVToAT5wRSUIJj6Y1oadF-xhbytUye2IMaBhFT0J4YjQZiG_PcOpAATidtYR/s1600/IMG_9618.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear:left; float:left;margin-right:1em; margin-bottom:1em&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;240&quot; width=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyjlGM31T8voo4G-Og0b6cWkRX2wbY6BHfi1zXtQOmtKNdJ-iPlwc1afpwN43exmgopPO8qBr1BpeL78rwtVToAT5wRSUIJj6Y1oadF-xhbytUye2IMaBhFT0J4YjQZiG_PcOpAATidtYR/s320/IMG_9618.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
It&#39;s almost 5 years I used to work on two monitors, and now sometimes feel the need of the third one. Yes, it&#39;s very handy to work on several, but there is a drawback as well: you get used to it and its getting harder and harder to work on a single one at home.&lt;br /&gt;
Today, I had to spent some time on my WP7 game (which is in the Marketplace from february), and suddenly thought about moving from 22&quot; Samsung (monitor) to 32&quot; Samsung (TV). And you know what  - that&#39;s amazing - clear picture, wide area to work and you can see every long line of your code (I&#39;m not fan of splitting lines by enters).</content><link rel='replies' type='application/atom+xml' href='http://mkartak.blogspot.com/feeds/5832699568601312675/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6433679371380083688/5832699568601312675' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5832699568601312675'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6433679371380083688/posts/default/5832699568601312675'/><link rel='alternate' type='text/html' href='http://mkartak.blogspot.com/2011/11/better-work-environment-bigger-screens.html' title='Better Work Environment: Bigger Screens'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/06048861318206825665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyjlGM31T8voo4G-Og0b6cWkRX2wbY6BHfi1zXtQOmtKNdJ-iPlwc1afpwN43exmgopPO8qBr1BpeL78rwtVToAT5wRSUIJj6Y1oadF-xhbytUye2IMaBhFT0J4YjQZiG_PcOpAATidtYR/s72-c/IMG_9618.JPG" height="72" width="72"/><thr:total>0</thr:total></entry></feed>