tag:blogger.com,1999:blog-60316607038909384992024-02-21T02:04:19.119+02:00Ami Bar's blogAmi Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-6031660703890938499.post-52325426601429890902012-09-11T00:33:00.000+03:002012-09-11T00:41:52.946+03:00How to generate a strongly typed WMI object in Visual StudioOk, I returned, no one seemed to notice anyway.<br />
<br />
I spent about an hour to figure out how to generate a strongly typed WMI object in Visual Studio. It's the third time I forget how to do this, so I am writing it here so I won't forget.<br />
<br />
I quote from the link <a href="http://msdn.microsoft.com/en-us/library/2wkebaxa.aspx">http://msdn.microsoft.com/en-us/library/2wkebaxa.aspx</a> , "You can alternately generate a strongly-typed class using the Server Explorer Management Extension in Visual Studio .NET."<br />
<br />
This means you open the Server Explorer, expand your computer, expand the Management Classes branch and voila!<br />
<br />
If you can't find the specific class you want, you can right click on the Management Classes and choose Add Classes...<br />
<br />
AmiAmi Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-3644217751785840342010-05-05T22:44:00.002+03:002010-05-05T22:47:37.611+03:00So long and thanks for all the phish(ing)Hi,<br /><br />I thought it is a nice place here to publish some thoughts, I guess I was wrong.<br />A few months ago I started getting a lot of gibberish comments from anonymous sources.<br />I can't stand this any more.<br /><br />So I sign out for the last time.<br /><br />So long and thanks for all the phish(ing)<br /> AmiAmi Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-11695210541670014632008-12-02T11:28:00.001+02:002008-12-02T11:28:14.958+02:00Memory leak in WCF proxies when not aborting.<p>I wrote an application that samples the progress of a service. </p> <p>The interface was:</p> <p><em><font color="#ff8000">public interface IProgressService<br>{<br> int GetProgress();<br>}</font></em> <p>I created a proxy for it on the client side: <p><em><font color="#ff8000">public class ProgressServiceProxy : ClientBase<IProgressService>, IProgressService<br>{<br> public int GetProgress()<br> {<br> return Channel.GetProgress();<br> }<br>}</font></em> <p>The sampling code was: <p><em><font color="#ff8000">private void SampleProgress()<br>{<br> ProgressServiceProxy proxy = null; </font></em> <p><em><font color="#ff8000"> while (!_shutdown)<br> {<br> if (null == proxy)<br> {</font></em></p> <p><em><font color="#ff8000"> // Create a new proxy<br> proxy = new ProgressServiceProxy();<br> } </font></em></p> <p><em><font color="#ff8000"> try<br> {</font></em></p> <p><em><font color="#ff8000"> // Try to get progress<br> int progress = proxy.GetProgress();<br> }<br> catch (Exception)<br> {</font></em></p><font color="#ff8000"> // The service is down<br></font> <p><em><font color="#ff8000"> proxy = null;<br> }</font></em></p> <p><em><font color="#ff8000"> // Wait some time for the next sample<br> Thread.Sleep(250);<br> } </font></em></p> <p><em><font color="#ff8000"> if (null != proxy)<br> {<br> proxy.Close();<br> }<br>}</font></em></p> <p>I left this code to run at night when the service was down. When I got back at morning I found that the client process occupies about 400MB of memory. With WinDBG ans SOS.dll I counted about 170000 instances of the proxy!!!!</p> <p>Where is the GC when you need it?!</p> <p>To solve this I first tried to do <font color="#ff8000"><em>proxy.Close()</em></font> in the catch block, but it doesn't work since the proxy is in faulted state.</p> <p>The correct solution is to use <em><font color="#ff8000">proxy.Abort()</font></em> this let the GC know that the proxy is no longer needed at release it.</p> <p>The corrected code is:</p> <p><em><font color="#ff8000">private void SampleProgress()<br>{<br> ProgressServiceProxy proxy = null; </font></em> <p><em><font color="#ff8000"> while (!_shutdown)<br> {<br> if (null == proxy)<br> {</font></em></p> <p><em><font color="#ff8000"> // Create a new proxy<br> proxy = new ProgressServiceProxy();<br> } </font></em></p> <p><em><font color="#ff8000"> try<br> {</font></em></p> <p><em><font color="#ff8000"> // Try to get progress<br> int progress = proxy.GetProgress();<br> }<br> catch (Exception)<br> {</font></em></p><font color="#ff8000"> // The service is down<br></font> <p><em><font color="#ffff00"> proxy.Abort();</font></em></p> <p><em><font color="#ff8000"> proxy = null;<br> }</font></em></p> <p><em><font color="#ff8000"> // Wait some time for the next sample<br> Thread.Sleep(250);<br> } </font></em></p> <p><em><font color="#ff8000"> if (null != proxy)<br> {<br> proxy.Close();<br> }<br>}</font></em></p> <p>After this fix the proxies instance count went as high as about 50.</p> <p>Ami</p>Ami Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-61038686641978338992008-06-17T12:58:00.001+03:002008-06-17T12:58:44.803+03:00Reading Excel content without office using OleDbConnection in .NET<p>I needed to open an Excel doc and read the content its tables. </p> <p>First we need to open the Excel file:</p> <p><em>string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0 Xml;HDR=No\";"; <br>using (OleDbConnection connection = new OleDbConnection(cs))<br>{<br> connection.Open();<br> string[] tableNames = GetTableNames(connection);<br>}</em></p> <p> </p> <p>For advances connection strings refer to <a title="http://www.connectionstrings.com/" href="http://www.connectionstrings.com/">http://www.connectionstrings.com/</a></p> <p> </p> <p>Then we use the connection to get the table names:</p> <p><em>string[] GetTableNames(OleDbConnection connection)<br>{<br> List<string> tableNames = new List<string>();<br> DataTable dt = connection.GetSchema("Tables");<br> foreach (DataRow row in dt.Rows)<br> {<br> string tableName = row["TABLE_NAME"].ToString(); </em> <p><em> // Table names ends with a $, or with $'<br> if (tableName.EndsWith("$") || tableName.EndsWith("$\'"))<br> {<br> tableNames.Add(tableName);<br> }<br> } </em> <p><em> return tableNames.ToArray();<br>} </em> <p>The name of the columns in the Excel are named Fn where n is the column index (1 indexed)</p> <p>The following code iterates over the tables and prints the 3rd column of the first 10 rows of each worksheet.</p> <p><em>for (int i = 0; i < tableNames.Length; i++)<br>{<br> OleDbCommand command = connection.CreateCommand();<br> command.CommandText = "select top 10 F3 from [" + tableNames[i] + "]";<br> command.CommandType = CommandType.Text; </em> <p><em> OleDbDataReader reader = command.ExecuteReader(); </em> <p><em> Console.WriteLine("\n\nReading from table: " + tableNames[i]); </em> <p><em> while (reader.Read())<br> {<br> for (int j = 0; j < reader.FieldCount; j++)<br> {<br> Console.Write(reader.GetValue(j) + ", ");<br> }<br> Console.WriteLine("");<br> } </em> <p><em> reader.Dispose();<br> command.Dispose();<br>}</em></p> <p>As you can see I didn't change the names of the tables although sometimes they are not the same as they appear in the Excel.</p> <p>A worksheet named A.B will be converted to 'A#B$'</p> <p>A worksheet named A B C will be converted to 'A B C$'</p> <p>I didn't have time to check all the conversions, but it works with what you get as a table name.</p> <p> </p> <p>Ami</p>Ami Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-9473077528885875592008-01-31T16:27:00.001+02:002008-01-31T16:27:19.441+02:00Alternative to lock(..) use using(..)<p>Hi,</p> <p>I have a code that sometimes need to run in single threaded environment (STE) and sometimes in multi threaded environment (MTE). In the MTE I have to set locks in order to synchronize the objects and in the STE I need to avoid locks since it is time consuming and will affect performance.</p> <h4>Implementation</h4> <p>To solve this I created a new lock mechanism:</p> <p>I created an interface named <em><font color="#ff8000">ILocker</font></em>:</p> <p><em><font color="#ffffff"><font color="#ff8000">public interface ILocker : IDisposable<br>{<br> ILocker Lock();<br>}</font> </font></em></p> <p>This interface has two implementations:</p> <p>One that implements a lock, for the MTE:</p> <p><em><font color="#ff8000">public class Locker : ILocker<br>{<br> private readonly object _lock = new object(); </font></em></p> <p><em><font color="#ff8000"> public ILocker Lock()<br> {<br> Monitor.Enter(_lock);<br> return this;<br> } </font></em></p> <p><em><font color="#ff8000"> public void Dispose()<br> {<br> Monitor.Exit(_lock);<br> }<br>}</font></em></p> <p>And another one that does nothing, for the STE</p> <p><em><font color="#ff8000">public class NoOpLocker : ILocker<br>{<br> public ILocker Lock()<br> {<br> return this;<br> } </font></em></p> <p><em><font color="#ff8000"> public void Dispose()<br> {<br> }<br>}</font></em></p> <h4>Usage</h4> <p>An original code of this form:</p> <p><font color="#ff8000"><em>object locker = new object();</em></font></p> <p><font color="#ff8000"><em>lock(locker) { ... }</em></font></p> <p>Will look like:</p> <p><font color="#ff8000"><em>ILocker locker = new Locker(); // or NoOpLocker();</em></font></p> <p><font color="#ff8000"><em>using (locker.Lock()) { ... } </em></font> <h4>Performance</h4> <p>I ran 100,000,000 iterations on one thread for each locking and checked how long it takes. The test function looks like this:</p> <p><font color="#ff8000"><em>void CheckLocker(ILocker locker)<br>{<br> Stopwatch stopwatch = Stopwatch.StartNew(); </em></font> <p><font color="#ff8000"><em> for (int i = 0; i < 100000000; i++)<br> {<br> using (locker.Lock())<br> {<br> DoNothing();<br> }<br> } </em></font> <p><font color="#ff8000"><em> Console.WriteLine(stopwatch.ElapsedMilliseconds);<br>} </em></font></p> <p><em><font color="#ff8000">void DoNothing() </font></em><em><font color="#ff8000">{ /* </font></em><em><font color="#ff8000">Do nothing */ </font></em><em><font color="#ff8000">}</font></em></p> <p>The results were:</p> <table cellspacing="0" cellpadding="2" width="400" border="1" unselectable="on"> <tbody> <tr> <td valign="top" width="200"> <p align="center">Lock type</p></td> <td valign="top" width="200"> <p align="center">Time in milliseconds</p></td></tr> <tr> <td valign="top" width="200">No locks</td> <td valign="top" width="200"> <p align="center">10</p></td></tr> <tr> <td valign="top" width="200"><em><em><font color="#ff8000">using(..)</font></em></em> with <em><em><font color="#ff8000">NoOpLocker</font></em></em></td> <td valign="top" width="200"> <p align="center">775</p></td></tr> <tr> <td valign="top" width="200"><em><font color="#ff8000">lock(..)</font></em></td> <td valign="top" width="200"> <p align="center">4865</p></td></tr> <tr> <td valign="top" width="200"><em><font color="#ff8000">using(..)</font></em> with <em><font color="#ff8000">Locker</font></em></td> <td valign="top" width="200"> <p align="center">6603</p></td></tr></tbody></table> <p> </p> <h4>Pros</h4> <p>- The type of lock can be determined at run time.</p> <p>- The change from <em><font color="#ff8000">lock(..)</font> </em>to <em><font color="#ff8000">using(..)</font></em> is simple.</p> <p>- A class can be written with MTE in mind and can be used for STE when needed without rewriting the class.</p> <p>- A class that is already written with <font color="#ff8000"><em>lock(..)</em></font> and used also in STE, can use <em><font color="#ff8000">using(..)</font></em> and improve performance, while still supporting MTE as needed.</p> <h4>Cons</h4> <p>- The <em><font color="#ff8000">Locker</font></em> works slower than just using <font color="#ff8000"><em>lock(..)</em></font></p> <p>- The <em><font color="#ff8000">NoOpLocker </font></em>works slower than using nothing.</p> <p><em><font color="#ff8000"></font></em></p>Ami Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-39295573895952416672007-09-05T09:49:00.001+03:002007-09-05T09:49:40.601+03:00New article at CodeProject - CompactMessageEncoder<p>I published a new article at <a href="http://www.codeproject.com">CodeProject</a> about compressing messages on WCF channels. Check it out <a href="http://www.codeproject.com/useritems/CompactMessageEncoder.asp">here</a>.</p> <p>Ami</p>Ami Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com2tag:blogger.com,1999:blog-6031660703890938499.post-88564889178664416272007-09-03T15:33:00.001+03:002007-09-03T15:33:16.291+03:00.NET Application runs faster as multi processed than multi threaded<p>I have simulation that does a lot of number crunching. I wanted to make it faster utilizing the CPU (I have a quad CPU). I tired to run the same algorithm it two ways:</p> <ol> <li>One process with several threads. </li> <li>Several process with one thread in each.</li></ol> <p>Each thread ran the same algorithm and internally spawn other threads.</p> <p>I found out that the option 2 was faster than option 1. Looking into it I concluded that the first option spends too much time in the GC. Every time the GC runs it suspends all the threads of the process causing all the algorithms that run on the same process to stop.</p> <p>By looking at the performance monitor the first option spends ~50% in GC, while the second option spends ~25% in GC.</p> <p>Also checking the total time it took to run. The difference was higher as the number of algorithms run concurrently was higher.</p> <p> </p> <p>Ami</p>Ami Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com1tag:blogger.com,1999:blog-6031660703890938499.post-73680896794611334842007-09-03T15:21:00.001+03:002007-09-03T15:21:35.642+03:00Performance Counters of .NET x86 applications on x64 machine<p>When you compile a .NET application to x86 CPU and run it on 64-Bit machine, the performance counters are not displayed by the Performance Counter Monitor (perfmon) and the Process Explorer (Systinternals). This is very weird. </p> <p>The solution for the Performance Counter Monitor is to run it:</p> <p>mmc.exe /32 perfmon.msc</p> <p>This way the mmc.exe runs a 32-Bit application an can see the Performance Counters of 32-Bit .NET application.</p> <p>Note that it doesn't show the 64-Bit Performance Counter when using the /32 flag.</p> <p> </p> <p>Ami</p>Ami Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-89237974819083959322007-07-19T12:01:00.000+03:002007-07-19T12:15:31.033+03:00How to configure .net applications to have more than two download sessionsHi,<br /><br />This post applied to applications which download using the WebClient class of the .net framework.<br /><br />To change the connection number merge the following with the app.config or web.config of your application.<br /><pre class="code" space="preserve"><br /><configuration><br /> <system.net><br /> <connectionmanagement><br /> <add address="*" maxconnection="10"><br /> </connectionmanagement><br /> </system.net><br /></configuration><br /></pre><br /><br /><span style="font-style: italic;"></span>This will change the limit number of the connection to 10 to every server.<br /><br />For more information refer to <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;821268">http://support.microsoft.com/default.aspx?scid=kb;en-us;821268</a> and <a href="http://msdn2.microsoft.com/en-us/library/ms998549.aspx"><span style="text-decoration: underline;">http://msdn2.microsoft.com/en-us/library/ms998549.aspx</span></a><br /><br />AmiAmi Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-29848033298767322702007-07-18T20:49:00.000+03:002007-07-19T11:59:41.030+03:00How to configure Internet Explorer to have more than two download sessionsHi,<br /><br />By default the Internet Explorer is limited to two downloads at a time this article explain how to change this : <a href="http://support.microsoft.com/kb/282402">http://support.microsoft.com/kb/282402</a><br /><br />In short, copy the following text into a file named update.reg with notepad, save it in Unicode format, and run it with double click.<br /><br /><span style="font-style:italic;">Windows Registry Editor Version 5.00<br /><br />[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]<br />MaxConnectionsPer1_0Server"=dword:0000000a<br />MaxConnectionsPerServer"=dword:0000000a<br /></span><br />This will raise the connections limit to 10 (0xA) per server.<br /><br />Note that this configuration is per user, and doesn't apply to the whole machine. Also note that it applies only to Internet Explorer and not to other applications.<br /><br />AmiAmi Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0tag:blogger.com,1999:blog-6031660703890938499.post-3393070040631958552007-07-17T14:35:00.000+03:002007-07-17T14:38:33.456+03:00Smart Thread PoolHi,<br /><br />This is my new blog.<br /><br />I have an article in CodeProject which I published long time ago, and I keep updating it.<br />I am going to publish a new version soon with some new features.<br /><br />The SmartThreadPool link is: <a href="http://www.codeproject.com/cs/threads/smartthreadpool.asp">SmartThreadPool</a><br /><br />AmiAmi Barhttp://www.blogger.com/profile/12576288839333503622noreply@blogger.com0