<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tamir Khason - Just code &#187; tutorial</title>
	<atom:link href="http://khason.net/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://khason.net</link>
	<description>Take care of the sense, and the sounds will take care of themselves.</description>
	<lastBuildDate>Wed, 25 Jan 2012 16:37:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Quick IT tip: How to build bootable USB stick</title>
		<link>http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/</link>
		<comments>http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 11:55:12 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[ITPRO]]></category>
		<category><![CDATA[DEV]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows Embedded]]></category>
		<category><![CDATA[Work process]]></category>

		<guid isPermaLink="false">http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/</guid>
		<description><![CDATA[Because of my main job and lack of human resources there, I invest less and less in community. Thus I lost my MVP title. Sorry, guys. Also a ton of management tasks in big company prevents me from actual coding. However I am still able to find some time for doing “real” things such as [...]<p/>]]></description>
			<content:encoded><![CDATA[<p>Because of my main job and <a href="/tag/better-place/" target="_blank">lack of human resources</a> there, I invest less and less in community. Thus I <a href="http://twitter.com/tamir/status/4638500894" target="_blank">lost my MVP title</a>. Sorry, guys. Also a ton of management tasks in big company prevents me from actual coding. However I am still able to find some time for doing “real” things such as <a href="http://connect.microsoft.com/windowsembedded" target="_blank">Windows Embedded Standard 2011</a> image building. Thus today I will explain how to build <a href="http://www.bootdisk.com/pendrive.htm" target="_blank">bootable flash USB disk</a> with a couple of simple commands and without <a href="http://www.askvg.com/a-bootable-usb-utility-to-create-bootable-usb-drive-to-install-windows-vista-server-2008-and-7/" target="_blank">using special utilities</a>. </p>
<p>&#160;<img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="World first USB drive by Trek Technology" border="0" alt="World first USB drive by Trek Technology" src="http://khason.net/images/2009/10/image.png" width="186" height="150" /> </p>
<p>Why to use bootable USB instead of regular <a href="http://en.wikipedia.org/wiki/Polycarbonate" target="_blank">CD or DVD ROM</a>? Well, it is more convenience, takes less storage, faster and fully recycle. So let’s start.</p>
<p>1. Insert USB flash drive <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />     <br />2. Run command prompt shell as Administrator (just in case the keyboard shortcut for “Run as Administrator” is Ctrl+Alt+Shift)     <br />3. Type “<strong>diskpart</strong>” to run Microsoft DiskPart utility. </p>
<blockquote><p>C:\Windows\system32&gt;diskpart </p>
<p>Microsoft DiskPart version 6.1.7600      <br />Copyright (C) 1999-2008 Microsoft Corporation.       <br />On computer: TAMIRK-DEV</p>
</blockquote>
<p>4. List your disks by typing in “<strong>list disk</strong>” or for those who like it shorter (like me) “<strong>list dis</strong>”</p>
<blockquote><p>DISKPART&gt; lis dis </p>
<p>&#160; Disk ###&#160; Status&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Size&#160;&#160;&#160;&#160; Free&#160;&#160;&#160;&#160; Dyn&#160; Gpt      <br />&#160; &#8212;&#8212;&#8211;&#160; &#8212;&#8212;&#8212;&#8212;-&#160; &#8212;&#8212;-&#160; &#8212;&#8212;-&#160; &#8212;&#160; &#8212;       <br />&#160; Disk 0&#160;&#160;&#160; Online&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 149 GB&#160; 1024 KB       <br />&#160; Disk 1&#160;&#160;&#160; Online&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 75 GB&#160;&#160;&#160;&#160; 2 GB       <br />&#160; Disk 2&#160;&#160;&#160; Online&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 3814 MB&#160;&#160;&#160;&#160;&#160; 0 B       <br />&#160; Disk 3&#160;&#160;&#160; No Media&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0 B&#160;&#160;&#160;&#160;&#160; 0 B       <br />&#160; Disk 4&#160;&#160;&#160; No Media&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0 B&#160;&#160;&#160;&#160;&#160; 0 B       <br />&#160; Disk 5&#160;&#160;&#160; Online&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 14 GB&#160;&#160;&#160;&#160;&#160; 0 B</p>
</blockquote>
<p>5. Identify your flash drive (in my case it is Disk 5)    <br />6. Select this drive to mark it for work by using “<strong>select disk</strong> 5” or “<strong>sel dis</strong> 5” command</p>
<blockquote><p>DISKPART&gt; sel dis 5</p>
<p>Disk 5 is now the selected disk.</p>
</blockquote>
<p>7. Clean it (this will delete everything on your disk drive, so be careful) by using “<strong>clean</strong>” or “<strong>cle</strong>” command.</p>
<blockquote><p>DISKPART&gt; cle</p>
<p>DiskPart succeeded in cleaning the disk.</p>
</blockquote>
<p>8. Create primary partition &#8211; “<strong>create partition primary</strong>” or “<strong>cre par pri</strong>”</p>
<blockquote><p>DISKPART&gt; cre par pri</p>
<p>DiskPart succeeded in creating the specified partition.</p>
</blockquote>
<p>9. Select new partition &#8211; “<strong>select partition 1</strong>” or “<strong>sel par 1</strong>”</p>
<blockquote><p>DISKPART&gt; sel par 1</p>
<p>Partition 1 is now the selected partition.</p>
</blockquote>
<p>10. Mark it as Active partition &#8211; “<strong>active</strong>” or “<strong>act</strong>”</p>
<blockquote><p>DISKPART&gt; act</p>
<p>DiskPart marked the current partition as active.</p>
</blockquote>
<p>11. Format &#8211; “<strong>format fs=ntfs quick</strong>” or “<strong>for fs=ntfs quick</strong>”</p>
<blockquote><p>DISKPART&gt; for fs=ntfs quick</p>
<p>&#160; 100 percent completed </p>
<p>DiskPart successfully formatted the volume.</p>
</blockquote>
<p>12. And finally my favorite command &#8211; “<strong>assign</strong>” or “<strong>ass</strong>” to mark it ready and create mount point</p>
<blockquote><p>DISKPART&gt; ass</p>
<p>DiskPart successfully assigned the drive letter or mount point.</p>
</blockquote>
<p>13. Exit &#8211; “exit” or “exi” to return to command shell</p>
<blockquote><p>DISKPART&gt; exi</p>
<p>Leaving DiskPart&#8230;</p>
</blockquote>
<p>Now your thumb drive is ready and bootable. So you can start copying files from ISO image into it.</p>
<p>Other option is to work with volumes rather than with disks. The all difference is in steps 4-6. Instead of “<strong>lis dis</strong>” use “<strong>lis vol</strong>” and instead of “<strong>sel dis</strong>” use “<strong>sel vol</strong>”. Maybe it is more convenience way of work because in this case you can identify partitions by labels and sizes rather than by sizes only.</p>
<blockquote><p>DISKPART&gt; lis vol </p>
<p>&#160; Volume ###&#160; Ltr&#160; Label&#160;&#160;&#160;&#160;&#160;&#160;&#160; Fs&#160;&#160;&#160;&#160; Type&#160;&#160;&#160;&#160;&#160;&#160;&#160; Size&#160;&#160;&#160;&#160; Status&#160;&#160;&#160;&#160; Info      <br />&#160; &#8212;&#8212;&#8212;-&#160; &#8212;&#160; &#8212;&#8212;&#8212;&#8211;&#160; &#8212;&#8211;&#160; &#8212;&#8212;&#8212;-&#160; &#8212;&#8212;-&#160; &#8212;&#8212;&#8212;&#160; &#8212;&#8212;&#8211;       <br />&#160; Volume 0&#160;&#160;&#160;&#160; E&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DVD-ROM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0 B&#160; No Media       <br />&#160; Volume 1&#160;&#160;&#160;&#160; G&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DVD-ROM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0 B&#160; No Media       <br />&#160; Volume 2&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System Rese&#160; NTFS&#160;&#160; Partition&#160;&#160;&#160; 100 MB&#160; Healthy&#160;&#160;&#160; System       <br />&#160; Volume 3&#160;&#160;&#160;&#160; C&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; NTFS&#160;&#160; Partition&#160;&#160;&#160;&#160; 68 GB&#160; Healthy&#160;&#160;&#160; Boot       <br />&#160; Volume 4&#160;&#160;&#160;&#160; D&#160;&#160; DATA&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; NTFS&#160;&#160; Partition&#160;&#160;&#160;&#160; 80 GB&#160; Healthy       <br />&#160; Volume 5&#160;&#160;&#160;&#160; F&#160;&#160; READYBOOST&#160;&#160; FAT&#160;&#160;&#160; Removable&#160;&#160; 3812 MB&#160; Healthy       <br />&#160; Volume 6&#160;&#160;&#160;&#160; H&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Removable&#160;&#160;&#160;&#160;&#160;&#160; 0 B&#160; No Media       <br />&#160; Volume 7&#160;&#160;&#160;&#160; I&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Removable&#160;&#160;&#160;&#160;&#160;&#160; 0 B&#160; No Media       <br />&#160; Volume 8&#160;&#160;&#160;&#160; K&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; NTFS&#160;&#160; Removable&#160;&#160;&#160;&#160; 14 GB&#160; Healthy</p>
</blockquote>
<p>If you already copied your image into disk, you can mark MBR by using special utility called <a href="http://technet.microsoft.com/en-us/library/cc749177(WS.10).aspx" target="_blank">BootSect.exe</a> shipped with WAIK. In our case (with Windows 7 embedded), you’ll have to update master boot code to use BOOTMGR (Vista and up) rather than NTLDR (XP and down)</p>
<blockquote><p>BOOTSECT.EXE /NT60 K: /mbr</p>
</blockquote>
<p>We done, have a good day and be good people. Additional information regarding USB core guys from MS can be archived from <a href="http://blogs.msdn.com/usbcoreblog/" target="_blank">their brand new blog</a> (hope it will be up to date).</p>
<p>At the end, just you to know how are CDs make by Discovery Channel    </p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/O3FQzwNzUE4&amp;hl=en&amp;fs=1&amp;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/O3FQzwNzUE4&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Real singleton approach in WPF application</title>
		<link>http://khason.net/dev/read-singleton-approach-in-wpf-application/</link>
		<comments>http://khason.net/dev/read-singleton-approach-in-wpf-application/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 20:19:35 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://khason.net/dev/read-singleton-approach-in-wpf-application/</guid>
		<description><![CDATA[One of the most common problems in WPF is memory/processor time consumption. Yes, WPF is rather greedy framework. It become even greedier when using unmanaged resources, such as memory files or interop images. To take care on it, you can implement singleton pattern for the application and share only one unmanaged instance among different application [...]
Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>One of the most common problems in WPF is memory/processor time consumption. Yes, WPF is rather greedy framework. It become even greedier when using unmanaged resources, such as memory files or interop images. To take care on it, you can implement singleton pattern for the application and share only one unmanaged instance among different application resources. So today we’ll try to create one large in-memory dynamic bitmap and share it between different instances of WPF controls. Let’s start</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="The Singleton" border="0" alt="The Singleton" src="http://khason.net/images/2009/08/image2.png" width="206" height="520" /> </p>
<p>First of all let’s create our single instance source. The pattern is straight forward. Create a class derived from INotifyPropertyChanged, create private constructor and static member returns the single instance of the class.</p>
<blockquote><p>public class MySingleton : INotifyPropertyChanged { </p>
<p>&#160;&#160; #region Properties     <br />&#160;&#160; public BitmapSource Source { get { return _source; } }      <br />&#160;&#160; public static MySingleton Instance {      <br />&#160;&#160;&#160;&#160;&#160; get {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (_instance == default(MySingleton)) _instance = new MySingleton();      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return _instance;      <br />&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160; }      <br />&#160;&#160; #endregion </p>
<p>&#160;&#160; #region ctor     <br />&#160;&#160; private MySingleton() { _init(); }      <br />&#160;&#160; #endregion</p>
</blockquote>
<p>Now we need to create this single instance of this class inside our XAML program. To do this, we have great extension x:Static</p>
<blockquote><p>&lt;Window.DataContext&gt;     <br />&#160;&#160;&#160; &lt;x:StaticExtension Member=&quot;l:MySingleton.Instance&quot; /&gt;      <br />&lt;/Window.DataContext&gt;</p>
</blockquote>
<p>Now we need to find a way to do all dirty work inside MySingleton and keep classes using it as simple is possible. For this purpose we’ll register class handler to catch all GotFocus routed events, check the target of the event and rebind the only instance to new focused element. How to do this? Simple as 1-2-3</p>
<p>Create class handler</p>
<blockquote><p>EventManager.RegisterClassHandler(typeof(FrameworkElement), FrameworkElement.GotFocusEvent, (RoutedEventHandler)_onAnotherItemFocused);</p>
</blockquote>
<p>Check whether selected and focused item of the right type</p>
<blockquote><p>private void _onAnotherItemFocused(object sender, RoutedEventArgs e) {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DependencyPropertyDescriptor.FromProperty(ListBoxItem.IsSelectedProperty, typeof(ListBoxItem)).AddValueChanged(sender, (s, ex) =&gt; {}</p>
</blockquote>
<p>and reset binding </p>
<blockquote><p>var item = s as ListBoxItem;     <br />var img = item.Content as Image;      <br />if (_current != null &amp;&amp; _current.Target is Image &amp;&amp; _current.Target != img) {      <br />&#160;&#160; ((Image)_current.Target).ClearValue(Image.SourceProperty);      <br />}       <br />if (img != null) {      <br />&#160;&#160; _current = new WeakReference(img);      <br />&#160;&#160; img.SetBinding(Image.SourceProperty, _binding);      <br />}</p>
</blockquote>
<p>We almost done. a bit grease to make the source bitmap shiny </p>
<blockquote><p>var count = (uint)(_w * _h * 4);     <br />var section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0&#215;04, 0, count, null);      <br />_map = MapViewOfFile(section, 0xF001F, 0, 0, count);      <br />_source = Imaging.CreateBitmapSourceFromMemorySection(section, _w, _h, PixelFormats.Bgr32, (int)(_w * 4), 0) as InteropBitmap;      <br />_binding = new Binding {      <br />&#160;&#160; Mode = BindingMode.OneWay,      <br />&#160;&#160; Source = _source      <br />};      <br />CompositionTarget.Rendering += (s, e) =&gt; { _invalidate(); };</p>
</blockquote>
<blockquote><p>private void _invalidate() {     <br />&#160;&#160; var color = (uint)((uint)0xFF &lt;&lt; 24) | (uint)(_pixel &lt;&lt; 16) | (uint)(_pixel &lt;&lt; <img src='http://khason.net/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> | (uint)_pixel;      <br />&#160;&#160; _pixel++; </p>
<p>&#160;&#160; unsafe {     <br />&#160;&#160;&#160;&#160;&#160; uint* pBuffer = (uint*)_map;      <br />&#160;&#160;&#160;&#160;&#160; int _pxs = (_w * _h);      <br />&#160;&#160;&#160;&#160;&#160; for (var i = 0; i &lt; _pxs; i++) {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; pBuffer[i] = color;      <br />&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160; }      <br />&#160;&#160; _source.Invalidate();      <br />&#160;&#160; OnPropertyChanged(&quot;Source&quot;);      <br />}</p>
</blockquote>
<p>And we done. The usage of this approach is very simple – there is no usage at all. All happens automagically inside MySingleton class, all you need is to set static data context and add images</p>
<blockquote><p>&lt;StackPanel&gt;     <br />&#160;&#160;&#160; &lt;Button Click=&quot;_addAnother&quot;&gt;Add another&#8230;&lt;/Button&gt;      <br />&#160;&#160;&#160; &lt;ListBox Name=&quot;target&quot; /&gt;      <br />&lt;/StackPanel&gt;      <br />…      <br />private void _addAnother(object sender, RoutedEventArgs e) {      <br />&#160;&#160; var img = new Image { Width=200, Height=200, Margin=new Thickness(0,5,0,5) };      <br />&#160;&#160; target.Items.Add(img);      <br />&#160;&#160; this.Height += 200;      <br />}</p>
</blockquote>
<p>To summarize: in this article we learned how to use singletons as data sources for your XAML application, how to reuse it across WPF, how to connect to routed events externally and also how to handle dependency property changed from outside of the owner class. Have a nice day and be good people. </p>
<p><a href="http://khason.net/images/2009/08/ReuseSingleton.zip" target="_blank">Source code for this article (21k) &gt;&gt;</a></p>
<p>To make it works press number of times on “Add another…” button and then start selecting images used as listbox items. Pay attention to the working set of the application. Due to the fact that only one instance is in use it is not growing.</p>
<p>Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/dev/read-singleton-approach-in-wpf-application/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>INotifyPropertyChanged auto wiring or how to get rid of redundant code</title>
		<link>http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/</link>
		<comments>http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 19:43:41 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/</guid>
		<description><![CDATA[For the last week most of WPF disciples are discussing how to get rid of hardcoded property name string inside INotifyPropertyChanged implementation and how to keep using automatic properties implementation but keep WPF binding working. The thread was started by Karl Shifflett, who proposed interesting method of using StackFrame for this task. During this thread [...]
Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
<li><a href='http://khason.net/dev/video-encoder-and-metadata-reading-by-using-windows-media-foundation/' rel='bookmark' title='Video encoder and metadata reading by using Windows Media Foundation'>Video encoder and metadata reading by using Windows Media Foundation</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For the last week most of <a title="RSS feed of all disciples" href="http://pipes.yahoo.com/pipes/pipe.run?_id=Jp_ob_fn3RGFP6DnBRNMsA&amp;_render=rss" rel="rss" target="_blank">WPF disciples</a> are discussing how to get rid of hardcoded property name string inside <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" target="_blank">INotifyPropertyChanged</a> implementation and how to keep using automatic properties implementation but keep WPF binding working. The thread was started by Karl Shifflett, <a href="http://karlshifflett.wordpress.com/2009/08/02/inotifypropertychanged-how-to-remove-the-property-name-string-code-smell/" target="_blank">who proposed interesting method</a> of using <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe.aspx" target="_blank">StackFrame</a> for this task. During this thread other methods were proposed including code snippets, R#, <a href="http://ayende.com/Blog/archive/2009/08/08/an-easier-way-to-manage-inotifypropertychanged.aspx" target="_blank">Observer Pattern</a>, <a href="http://www.codeproject.com/KB/WPF/CinchII.aspx#Better_INPC" target="_blank">Cinch framework</a>, <a href="http://mbrownchicago.spaces.live.com/blog/cns!2221DC39E0C749A4!1285.entry" target="_blank">Static Reflection</a>, <a href="http://danielvaughan.orpius.com/post/Property-Change-Notification-using-a-Weak-Referencing-Strategy.aspx" target="_blank">Weak References</a> and others. I also proposed the method we’re using for our classes and promised to blog about it. So the topic today is how to use <a href="http://www.postsharp.org" target="_blank">PostSharp</a> to wire automatic implementation of INotifyPropertyChanged interface based on automatic setters only.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="My 5 ¢" border="0" alt="My 5 ¢" src="http://khason.net/images/2009/08/image1.png" width="483" height="337" /> </p>
<p>So, I want my code to looks like this:</p>
<blockquote><p>public class AutoWiredSource {      <br />&#160;&#160; public double MyProperty { get; set; }       <br />&#160;&#160; public double MyOtherProperty { get; set; }       <br />}</p>
</blockquote>
<p>while be fully noticeable about any change in any property and makes me able to bind to those properties.</p>
<blockquote><p>&lt;StackPanel DataContext=&quot;{Binding Source={StaticResource source}}&quot;&gt;      <br />&#160;&#160;&#160; &lt;Slider Value=&quot;{Binding Path=MyProperty}&quot; /&gt;       <br />&#160;&#160;&#160; &lt;Slider Value=&quot;{Binding Path=MyProperty}&quot; /&gt;       <br />&lt;/StackPanel&gt;</p>
</blockquote>
<p>How to achieve it? How to make compiler to replace my code with following?:</p>
<blockquote><p>private double _MyProperty;      <br />public double MyProperty {       <br />&#160;&#160; get { return _MyProperty; }       <br />&#160;&#160; set {       <br />&#160;&#160;&#160;&#160;&#160; if (value != _MyProperty) {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _MyProperty = value; OnPropertyChanged(&quot;MyProperty&quot;);       <br />&#160;&#160;&#160;&#160;&#160; }       <br />&#160;&#160; }       <br />}       <br />public event PropertyChangedEventHandler PropertyChanged;       <br />internal void OnPropertyChanged(string propertyName) {       <br />&#160;&#160; if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException(&quot;propertyName&quot;); </p>
<p>&#160;&#160; var handler = PropertyChanged as PropertyChangedEventHandler;      <br />&#160;&#160; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));       <br />}</p>
</blockquote>
<p>Simple: to use aspect oriented programming to inject set of instructions into pre-compiled source.</p>
<p>First of all we have to build some attribute will be used for marking classes requires change tracking. This attribute should be combined (compound) aspect to include all aspects used for change tracking. All we’re doing here is to get all set methods to add composition aspect to</p>
<blockquote><p>[Serializable, DebuggerNonUserCode, AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, AllowMultiple = false, Inherited = false),      <br />MulticastAttributeUsage(MulticastTargets.Class, AllowMultiple = false, Inheritance = MulticastInheritance.None, AllowExternalAssemblies = true)]       <br />public sealed class NotifyPropertyChangedAttribute : CompoundAspect {       <br />&#160;&#160; public int AspectPriority { get; set; } </p>
<p>&#160;&#160; public override void ProvideAspects(object element, LaosReflectionAspectCollection collection) {      <br />&#160;&#160;&#160;&#160;&#160; Type targetType = (Type)element;       <br />&#160;&#160;&#160;&#160;&#160; collection.AddAspect(targetType, new PropertyChangedAspect { AspectPriority = AspectPriority });       <br />&#160;&#160;&#160;&#160;&#160; foreach (var info in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(pi =&gt; pi.GetSetMethod() != null)) {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; collection.AddAspect(info.GetSetMethod(), new NotifyPropertyChangedAspect(info.Name) { AspectPriority = AspectPriority });       <br />&#160;&#160;&#160;&#160;&#160; }       <br />&#160;&#160; }       <br />}</p>
</blockquote>
<p>Next aspect is change tracking composition aspect. Which is used for marking only</p>
<blockquote><p>[Serializable]      <br />internal sealed class PropertyChangedAspect : CompositionAspect {       <br />&#160;&#160; public override object CreateImplementationObject(InstanceBoundLaosEventArgs eventArgs) {       <br />&#160;&#160;&#160;&#160;&#160; return new PropertyChangedImpl(eventArgs.Instance);       <br />&#160;&#160; } </p>
<p>&#160;&#160; public override Type GetPublicInterface(Type containerType) {      <br />&#160;&#160;&#160;&#160;&#160; return typeof(INotifyPropertyChanged);       <br />&#160;&#160; } </p>
<p>&#160;&#160; public override CompositionAspectOptions GetOptions() {      <br />&#160;&#160;&#160;&#160;&#160; return CompositionAspectOptions.GenerateImplementationAccessor;       <br />&#160;&#160; }       <br />}</p>
</blockquote>
<p>And the next which is most interesting one, we will put onto method boundary for tracking. There are some highlights here. First we do not want to fire PropertyChanged event if the actual value did not changed, thus we’ll handle the method on it entry and on it exit for check.</p>
<blockquote><p>[Serializable]      <br />internal sealed class NotifyPropertyChangedAspect : OnMethodBoundaryAspect {       <br />&#160;&#160; private readonly string _propertyName; </p>
<p>&#160;&#160; public NotifyPropertyChangedAspect(string propertyName) {      <br />&#160;&#160;&#160;&#160;&#160; if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException(&quot;propertyName&quot;);       <br />&#160;&#160;&#160;&#160;&#160; _propertyName = propertyName;       <br />&#160;&#160; } </p>
<p>&#160;&#160; public override void OnEntry(MethodExecutionEventArgs eventArgs) {      <br />&#160;&#160;&#160;&#160;&#160; var targetType = eventArgs.Instance.GetType();       <br />&#160;&#160;&#160;&#160;&#160; var setSetMethod = targetType.GetProperty(_propertyName);       <br />&#160;&#160;&#160;&#160;&#160; if (setSetMethod == null) throw new AccessViolationException();       <br />&#160;&#160;&#160;&#160;&#160; var oldValue = setSetMethod.GetValue(eventArgs.Instance,null);       <br />&#160;&#160;&#160;&#160;&#160; var newValue = eventArgs.GetReadOnlyArgumentArray()[0];       <br />&#160;&#160;&#160;&#160;&#160; if (oldValue == newValue) eventArgs.FlowBehavior = FlowBehavior.Return;       <br />&#160;&#160; } </p>
<p>&#160;&#160; public override void OnSuccess(MethodExecutionEventArgs eventArgs) {      <br />&#160;&#160;&#160;&#160;&#160; var instance = eventArgs.Instance as IComposed&lt;INotifyPropertyChanged&gt;;       <br />&#160;&#160;&#160;&#160;&#160; var imp = instance.GetImplementation(eventArgs.InstanceCredentials) as PropertyChangedImpl;       <br />&#160;&#160;&#160;&#160;&#160; imp.OnPropertyChanged(_propertyName);       <br />&#160;&#160; }       <br />}</p>
</blockquote>
<p>We almost done, all we have to do is to create class which implements INotifyPropertyChanged with internal method to useful call</p>
<blockquote><p>[Serializable]      <br />internal sealed class PropertyChangedImpl : INotifyPropertyChanged {       <br />&#160;&#160; private readonly object _instance; </p>
<p>&#160;&#160; public PropertyChangedImpl(object instance) {      <br />&#160;&#160;&#160;&#160;&#160; if (instance == null) throw new ArgumentNullException(&quot;instance&quot;);       <br />&#160;&#160;&#160;&#160;&#160; _instance = instance;       <br />&#160;&#160; } </p>
<p>&#160;&#160; public event PropertyChangedEventHandler PropertyChanged; </p>
<p>&#160;&#160; internal void OnPropertyChanged(string propertyName) {      <br />&#160;&#160;&#160;&#160;&#160; if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException(&quot;propertyName&quot;); </p>
<p>&#160;&#160;&#160;&#160;&#160; var handler = PropertyChanged as PropertyChangedEventHandler;      <br />&#160;&#160;&#160;&#160;&#160; if (handler != null) handler(_instance, new PropertyChangedEventArgs(propertyName));       <br />&#160;&#160; }       <br />}</p>
</blockquote>
<p>We done. The last thing is to reference to PostSharp Laos and Public assemblies and mark compiler to use Postsharp targets (inside your project file (*.csproj)</p>
<blockquote><p>&lt;Import Project=&quot;$(MSBuildToolsPath)\Microsoft.CSharp.targets&quot; /&gt;      <br />&lt;PropertyGroup&gt;       <br />&#160; &lt;DontImportPostSharp&gt;True&lt;/DontImportPostSharp&gt;       <br />&lt;/PropertyGroup&gt;       <br />&lt;Import Project=&quot;PostSharp\PostSharp-1.5.targets&quot; /&gt;</p>
</blockquote>
<p>Now we done. We can use clear syntax like following to make all our properties has public setter to be traceable. The only disadvantage is that you’ll have to drag two Post Sharp files with your project. But after all it much more convenience than manual notify change tracking all over your project.</p>
<blockquote><p>[NotifyPropertyChanged]      <br />public class AutoWiredSource {       <br />&#160;&#160; public double MyProperty { get; set; }       <br />}</p>
</blockquote>
<p>Have a nice day and be good people. Also try to thing what other extremely useful things can be done with PostSharp (or any other aspect oriented engine)</p>
<p><a href="http://khason.net/images/2009/08/AutoPropertyChangedWiring.zip" rel="enclosure" target="_blank">Source code for this article (1,225 KB)&gt;&gt;</a></p>
<p>Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
<li><a href='http://khason.net/dev/video-encoder-and-metadata-reading-by-using-windows-media-foundation/' rel='bookmark' title='Video encoder and metadata reading by using Windows Media Foundation'>Video encoder and metadata reading by using Windows Media Foundation</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>TFS licensing model demystification or what should I buy for my company in order not to step on the licensing mine?</title>
		<link>http://khason.net/itpro/tfs-licensing-model-demystification-or-what-should-i-buy-for-my-company-in-order-not-to-step-on-the-licensing-mine/</link>
		<comments>http://khason.net/itpro/tfs-licensing-model-demystification-or-what-should-i-buy-for-my-company-in-order-not-to-step-on-the-licensing-mine/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 17:41:54 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[ITPRO]]></category>
		<category><![CDATA[DEV]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[VSTS]]></category>

		<guid isPermaLink="false">http://khason.net/itpro/tfs-licensing-model-demystification-or-what-should-i-buy-for-my-company-in-order-not-to-step-on-the-licensing-mine/</guid>
		<description><![CDATA[Microsoft loves cumbersome licensing models . This is not because of their evil-heartedness, but because it make them possible to get more from bigger companies and less from smaller. However when you come into the real decision about how many and what kind of licenses you have to purchase, you stuck. Today we’ll try to [...]<p/>]]></description>
			<content:encoded><![CDATA[<p>Microsoft loves cumbersome licensing models . This is not because of their evil-heartedness, but because it make them possible to get more from bigger companies and less from smaller. However when you come into the real decision about how many and what kind of licenses you have to purchase, you stuck. Today we’ll try to make things clearer, at least for Team Foundation Server and Visual Studio, which is very base things for any software house develops things using Microsoft technologies.</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Cumbersomeness of the TFS licening model" border="0" alt="Cumbersomeness of the TFS licening model" src="http://khason.net/images/2009/08/image.png" width="636" height="454" />     <br /><font size="1">© image for cumbersomeness proposal via </font><a href="http://dotnet.org.za/blogs/willy/" target="_blank"><font size="1">Willy-Peter Schaub</font></a><font size="1"> by </font><a href="http://www.saarchitect.net/" target="_blank"><font size="1">SA Architect</font></a></p>
<p>To make things even simpler, let’s assume that we do not need TFS Workgroup edition (which is special edition for TFS 5 users only) and we are not using TFS Device CAL (as opposed to User CAL this Client Access License permits one device to be used by any number of users. This kind of CAL is good for kiosks rather then for development environments). Also <a href="http://blogs.msdn.com/vstsloadtestblog/" target="_blank">Test Load Agent</a> needs it own license. So now, and under all those circumstances, let’s start.</p>
<p>To work with TFS we need:</p>
<ol>
<li>One or more Team Foundation Server</li>
<li>More then one Visual Studio Client (editions can vary)</li>
<li>Optional one or more <a href="http://www.microsoft.com/licensing/software-assurance/" target="_blank">Software Assurance</a>, which can be licenses separately or together with <a href="http://msdn.microsoft.com/en-us/subscriptions/" target="_blank">MSDN subscription</a></li>
<li>… and some other optional tools</li>
</ol>
<h3>TFS Licensing</h3>
<p><font size="3">Each instance of TFS needs it license. Even if you have mirrored deployment of TFS, you need a server license for each instance. Also you need separate license if you are using TFS Data Tier on SQL Server cluster or using TFS Proxy. I think it’s clear, that in addition to TFS license you’ll need Windows Server and SQL server licenses (if it used especially for TFS). You can also put Data Tier on existing SQL server in this case you need only another TFS license without SQL.</font></p>
<p><font size="3">You do not need additional Team Foundation Server license for the machine used for TF build services. Also this machine does not need another CAL, except one used for the system user used for initialize builds.</font></p>
<blockquote><p><font size="3"><strong>To summarize</strong>: each instance of TFS need server license in addition to CALs and other server licenses (such as Windows, SQL, SharePoint, IIS etc).</font></p>
</blockquote>
<h4>Client Access License</h4>
<p><font size="3">In addition to server license you need also CAL for each used reads and writes to TFS. There are different versions of Visual Studio includes CAL:</font></p>
<ul>
<li><font size="3">Visual Studio 2008 Team Suite</font></li>
<li><font size="3">Visual Studio 2008 Architecture edition</font></li>
<li><font size="3">Visual Studio 2008 Development edition</font></li>
<li><font size="3">Visual Studio 2008 Test edition</font></li>
<li><font size="3">Visual Studio 2008 Database edition</font></li>
</ul>
<p><font size="3">Visual Studio 2008 Professional <strong>does not</strong> includes CAL. So each one of contributes needs one of Visual Studios which includes CAL. The TFS clients might be installed on one of those editions and does requires additional license.</font></p>
<p><font size="3">You <strong>do not need</strong> additional license when you are using TFS for <strong>only</strong>:</font></p>
<ul>
<li><font size="3">Create work items, bugs, etc.</font></li>
<li><font size="3">Query for work items</font></li>
<li><font size="3">Update work items</font></li>
</ul>
<p><font size="3">Other words product definition, system analysts, managers and “bug fillers</font><font size="3">” do not required additional CAL. Note, that they will probably need proper Microsoft Office licenses to use Excel or Project to do this, however they can also use TFS web access (browser) or any other 3rd party tool without purchasing separate CAL.</font></p>
<p><font size="3">Also you need only one CAL for server software. Other words, if you are using TFS on Windows Server you do not need TFS and Windows Server CAL. Also those CALs covers all earlier versions of all products in use.</font></p>
<blockquote><p><font size="3"><strong>To summarize</strong>: Each TFS user <u>does not need</u> additional CAL when he has proper license for Visual Studio Team Suite or using TFS for only bug/issues tracking. </font></p>
</blockquote>
<h4>Software assurance vs. MSDN</h4>
<p><font size="3">MSDN is more expensive then SA (Software Assurance), however it includes SA and provides some benefits by allowing access to several Microsoft products for development and testing purposes. </font></p>
<p><font size="3">There are two different MSDN editions – professional and premium. The difference between those editions (except price) is that Premium editing includes Windows Server Systems and Microsoft Office. Thus with Professional edition you got software assurance for Visual Studio 2008 Professional while with Premium for all other versions.</font></p>
<h4>Let’s simulate the results</h4>
<p><font size="3">For small software house with 10 developers (two architects, 1 DBA and 3 QA), two product definition guys, and manager we’ll need (in addition to OS, other server and Office licenses):</font></p>
<ul>
<li><font size="3">1 TFS license</font></li>
<li><font size="3">2 Visual Studio 2008 Architecture edition</font></li>
<li><font size="3">1 Visual Studio 2008 Database edition</font></li>
<li><font size="3">3 Visual Studio 2008 Test edition</font></li>
<li><font size="3">4 Visual Studio 2008 Development edition</font></li>
<li><font size="3">1&lt;n&lt;10 MSDN Licenses Premium (as number of employees need it for testing or development purposes)</font></li>
<li><font size="3">10-n SA licenses (if SA required)</font></li>
<li><font size="3">Additional CAL for build machine</font></li>
</ul>
<p><font size="3">I think, that now it become a bit clearer. For additional information regarding TFS licensing model, please refer </font><a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=CE194742-A6E8-4126-AA30-5C4E969AF2A3&amp;displaylang=en" target="_blank">Visual Studio Team System 2008 Licensing White Paper</a> or ask your local licensing expert at Microsoft.</p>
<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/itpro/tfs-licensing-model-demystification-or-what-should-i-buy-for-my-company-in-order-not-to-step-on-the-licensing-mine/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Visual Studio debugger related attributes cheat sheet</title>
		<link>http://khason.net/dev/visual-studio-debugger-related-attributes-cheat-sheet/</link>
		<comments>http://khason.net/dev/visual-studio-debugger-related-attributes-cheat-sheet/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 16:20:54 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[soft]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[VSTS]]></category>
		<category><![CDATA[Work process]]></category>

		<guid isPermaLink="false">http://khason.net/dev/visual-studio-debugger-related-attributes-cheat-sheet/</guid>
		<description><![CDATA[There are some debugger-oriented attributes in .Net, however 70% of developers not even know that they exist and 95% of them has no idea what they doing and how to use it. Today we’ll try to lid light on what those attributes doing and how to achieve the best of using it. First of all [...]<p/>]]></description>
			<content:encoded><![CDATA[<p>There are some debugger-oriented attributes in .Net, however 70% of developers not even know that they exist and 95% of them has no idea what they doing and how to use it. Today we’ll try to lid light on what those attributes doing and how to achieve the best of using it.</p>
<p>First of all let’s define what we want to get from debugger in VS</p>
<table border="1" cellspacing="1" cellpadding="2" width="600">
<tbody>
<tr>
<td valign="top" width="116">Term</td>
<td valign="top" width="484">What it actually does</td>
</tr>
<tr>
<td valign="top" width="116">Step Into</td>
<td valign="top" width="484">Steps into immediate child (that is what F11 does for standard VS layout)         <br /><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://khason.net/images/2009/04/image1.png" width="240" height="17" /> </td>
</tr>
<tr>
<td valign="top" width="116">Step Over</td>
<td valign="top" width="484">Skips to any depth (that is what F10 does)         <br /><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://khason.net/images/2009/04/image2.png" width="238" height="19" /> </td>
</tr>
<tr>
<td valign="top" width="116">Step Deeper</td>
<td valign="top" width="484">Steps into bypassing code, using certain attribute</td>
</tr>
<tr>
<td valign="top" width="116">Run Through</td>
<td valign="top" width="484">Steps into, but only one level. All lower lavels will be Stepped Over</td>
</tr>
</tbody>
</table>
<p>Now, when we have our set of terms, we can learn what JMC means. It is not famous whisky brand or another car company. It Just My Code option, checked in or out in “Option” dialog inside Visual Studio</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://khason.net/images/2009/04/image3.png" width="749" height="437" /> </p>
<p> Next turn is for attributes, there are four (I know about) attributes, related to debugger and used by me for efficient programming: <a title="Read more in MSDN about this" href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerhiddenattribute.aspx" target="_blank">DebuggerHidden</a>, <a title="Read more in MSDN about this attribute" href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggernonusercodeattribute.aspx" target="_blank">DebuggerNonUserCode</a>, <a title="Read more in MSDN about this attribute" href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerstepthroughattribute.aspx" target="_blank">DebuggerStepThrough</a> and <a title="Read more in MSDN about this attribute" href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerstepperboundaryattribute.aspx" target="_blank">DebuggerStepperBoundary</a>. We will use only three first. DebuggerStepperBoundary is the most secret attribute, which is related to debugging only in multithreaded environment. It used to avoid delusive effect, might appears when a context switch is made on a <a title="Read MSDN documentation about System.Threading.Thread class" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx" rel="tag" target="_blank">thread</a> within DebuggerNonUserCode applied. Other words, when you need to Step Through in Thread A and keep running at the same time in Thread B.</p>
<p>So let’s see the effects occurred when using those debugger attributes in case, you are trying to Step Into place, this attribute applied or set a Breakpoint there. When Just My Code (JMC) is checked all those attributes behaviors the same – they Step Deeper. However, when JMC is turned off (as in my picture) they begin to behavior differently.</p>
<table border="1" cellspacing="1" cellpadding="2" width="600">
<tbody>
<tr>
<td valign="top" width="200">Attribute</td>
<td valign="top" width="200">Step Into</td>
<td valign="top" width="200">Breakpoint</td>
</tr>
<tr>
<td valign="top" width="200">DebuggerHidden</td>
<td valign="top" width="200">Step Deeper</td>
<td valign="top" width="200">Step Deeper</td>
</tr>
<tr>
<td valign="top" width="200">DebuggerNonUserCode</td>
<td valign="top" width="200">Step Into</td>
<td valign="top" width="200">Step Into</td>
</tr>
<tr>
<td valign="top" width="200">DebuggerStepThrough</td>
<td valign="top" width="200">Step Deeper</td>
<td valign="top" width="200">Step Into</td>
</tr>
</tbody>
</table>
<p>As you can see, in this case </p>
<ul>
<li>DebuggerNonUserCode respects both for F11 (Step Into) and Breakpoints</li>
<li>DebuggerStepThrough respects only for Breakpoints</li>
<li>DebuggerHidden does not respects at all – just like when JMC is checked.</li>
</ul>
<p><strong>Bottom line</strong>: if you want people to manage whether to enter or not into your hidden methods – use DebuggerNonUserCode attribute. If you prefer them not to even know that those methods exists, use DebuggerHidden. If you want them to be able to put Breakpoints and stop on them, but keep running without explicit action – use&#160; DebuggerStepThrough</p>
<p>Have a nice day and be good people.&#160; Happy other developers friendly debugging.</p>
<p><em>Small bonus</em>: To visualize your struct, class, delegate, enum, field, property or even assembly for user debugger, you can use <a title="Read more on MSDN about this attribute" href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx" target="_blank">DebuggerDisplay</a> attribute (you need to put executable code into {} for example (“Value = {X}:{Y}”)]</p>
<p><em>Thanks to <a title="Boris unleashed with huge passover gift" href="http://twitpic.com/2yivn" target="_blank">Boris</a> for deep investigation</em></p>
<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/dev/visual-studio-debugger-related-attributes-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to calculate CRC in C#?</title>
		<link>http://khason.net/dev/how-to-calculate-crc-in-c/</link>
		<comments>http://khason.net/dev/how-to-calculate-crc-in-c/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 19:38:15 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>

		<guid isPermaLink="false">http://khason.net/dev/how-to-calculate-crc-in-c/</guid>
		<description><![CDATA[First of all, I want to beg your pardon about the frequency of posts last time. I’m completely understaffed and have a ton of things to do for my job. This why, today I’ll just write a quick post about checksum calculation in C#. It might be very useful for any of you, working with [...]
Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
<li><a href='http://khason.net/dev/video-encoder-and-metadata-reading-by-using-windows-media-foundation/' rel='bookmark' title='Video encoder and metadata reading by using Windows Media Foundation'>Video encoder and metadata reading by using Windows Media Foundation</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>First of all, I want to beg your pardon about the frequency of posts last time. I’m completely understaffed and have a ton of things to do for my job. This why, today I’ll just write a quick post about checksum calculation in C#. It might be very useful for any of you, working with devices or external systems.</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="BIOS CRC Error for old thinkpad" src="http://khason.net/images/2009/04/image.png" border="0" alt="BIOS CRC Error for old thinkpad" width="636" height="53" /></p>
<p><a href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check" target="_blank">CRC – Cyclic Redundancy Check</a> is an algorithm, which is widely used in different communication protocols, packing and packaging algorithms for assure robustness of data. The idea behind it is simple – calculate unique checksum (frame check sequence) for each data frame, based on it’s content and stick it at the end of each meaningful message. Once data received it’s possible to perform the same calculating and compare results – if results are similar, message is ok.</p>
<p>There are two kinds of CRC – 16 and 32 bit. There are also less used checksums for 8 and 64 bits. All this is about appending a string of zeros to the frame equal in number of frames and modulo two device by using generator polynomial containing one or more bits then checksum to be generated. This is very similar to performing a bit-wise XOR operation in the frame, while the reminder is actually our CRC.</p>
<p>In many industries first polynomial is in use to create CRC tables and then apply it for performance purposes. The default polynomial, defined by IEEE 802.3 which is 0xA001 for 16 bit and 0x04C11DB7 for 32 bit. We’re in C#, thus we should use it inversed version which is 0&#215;8408 for 16 bit and 0xEDB88320 for 32 bit. Those polynomials we’re going to use also in our sample.</p>
<p>So let’s start. Because CRC is HashAlgorithm after all, we can derive our classes from System.Security.Cryptography.HashAlgorithm class.</p>
<blockquote><p>public class CRC16 : HashAlgorithm {<br />
public class CRC32 : HashAlgorithm {</p>
</blockquote>
<p>Then, upon first creation we’ll generate hashtables with CRC values to enhance future performance. It’s all about values table for bytes from 0 to 255 , so we should calculate it only once and then we can use it statically.</p>
<blockquote><p>[CLSCompliant(false)]<br />
public CRC16(ushort polynomial) {<br />
HashSizeValue = 16;<br />
_crc16Table = (ushort[])_crc16TablesCache[polynomial];<br />
if (_crc16Table == null) {<br />
_crc16Table = CRC16._buildCRC16Table(polynomial);<br />
_crc16TablesCache.Add(polynomial, _crc16Table);<br />
}<br />
Initialize();<br />
}</p>
<p>[CLSCompliant(false)]<br />
public CRC32(uint polynomial) {<br />
HashSizeValue = 32;<br />
_crc32Table = (uint[])_crc32TablesCache[polynomial];<br />
if (_crc32Table == null) {<br />
_crc32Table = CRC32._buildCRC32Table(polynomial);<br />
_crc32TablesCache.Add(polynomial, _crc32Table);<br />
}<br />
Initialize();<br />
}</p>
</blockquote>
<p>Then let’s calculate it</p>
<blockquote><p>private static ushort[] _buildCRC16Table(ushort polynomial) {<br />
// 256 values representing ASCII character codes.<br />
ushort[] table = new ushort[256];<br />
for (ushort i = 0; i &lt; table.Length; i++) {<br />
ushort value = 0;<br />
ushort temp = i;<br />
for (byte j = 0; j &lt; 8; j++) {<br />
if (((value ^ temp) &amp; 0&#215;0001) != 0) {<br />
value = (ushort)((value &gt;&gt; 1) ^ polynomial);<br />
} else {<br />
value &gt;&gt;= 1;<br />
}<br />
temp &gt;&gt;= 1;<br />
}<br />
table[i] = value;<br />
}<br />
return table;<br />
}</p>
<p>private static uint[] _buildCRC32Table(uint polynomial) {<br />
uint crc;<br />
uint[] table = new uint[256];</p>
<p>// 256 values representing ASCII character codes.<br />
for (int i = 0; i &lt; 256; i++) {<br />
crc = (uint)i;<br />
for (int j = 8; j &gt; 0; j&#8211;) {<br />
if ((crc &amp; 1) == 1)<br />
crc = (crc &gt;&gt; 1) ^ polynomial;<br />
else<br />
crc &gt;&gt;= 1;<br />
}<br />
table[i] = crc;<br />
}</p>
<p>return table;<br />
}</p>
</blockquote>
<p>The result will looks like this for 32 bits</p>
<blockquote>
<pre>        0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97,
        0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
        0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4,
        0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D,
        0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11,
        0x3F, 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8,
        0xC5, 0xF4, 0xA7, 0x96, 0x01, 0x30, 0x63, 0x52,
        0x7C, 0x4D, 0x1E, 0x2F, 0xB8, 0x89, 0xDA, 0xEB,
        0x3D, 0x0C, 0x5F, 0x6E, 0xF9, 0xC8, 0x9B, 0xAA,
        0x84, 0xB5, 0xE6, 0xD7, 0x40, 0x71, 0x22, 0x13,
        0x7E, 0x4F, 0x1C, 0x2D, 0xBA, 0x8B, 0xD8, 0xE9,
        0xC7, 0xF6, 0xA5, 0x94, 0x03, 0x32, 0x61, 0x50,
        0xBB, 0x8A, 0xD9, 0xE8, 0x7F, 0x4E, 0x1D, 0x2C,
        0x02, 0x33, 0x60, 0x51, 0xC6, 0xF7, 0xA4, 0x95,
        0xF8, 0xC9, 0x9A, 0xAB, 0x3C, 0x0D, 0x5E, 0x6F,
        0x41, 0x70, 0x23, 0x12, 0x85, 0xB4, 0xE7, 0xD6,
        0x7A, 0x4B, 0x18, 0x29, 0xBE, 0x8F, 0xDC, 0xED,
        0xC3, 0xF2, 0xA1, 0x90, 0x07, 0x36, 0x65, 0x54,
        0x39, 0x08, 0x5B, 0x6A, 0xFD, 0xCC, 0x9F, 0xAE,
        0x80, 0xB1, 0xE2, 0xD3, 0x44, 0x75, 0x26, 0x17,
        0xFC, 0xCD, 0x9E, 0xAF, 0x38, 0x09, 0x5A, 0x6B,
        0x45, 0x74, 0x27, 0x16, 0x81, 0xB0, 0xE3, 0xD2,
        0xBF, 0x8E, 0xDD, 0xEC, 0x7B, 0x4A, 0x19, 0x28,
        0x06, 0x37, 0x64, 0x55, 0xC2, 0xF3, 0xA0, 0x91,
        0x47, 0x76, 0x25, 0x14, 0x83, 0xB2, 0xE1, 0xD0,
        0xFE, 0xCF, 0x9C, 0xAD, 0x3A, 0x0B, 0x58, 0x69,
        0x04, 0x35, 0x66, 0x57, 0xC0, 0xF1, 0xA2, 0x93,
        0xBD, 0x8C, 0xDF, 0xEE, 0x79, 0x48, 0x1B, 0x2A,
        0xC1, 0xF0, 0xA3, 0x92, 0x05, 0x34, 0x67, 0x56,
        0x78, 0x49, 0x1A, 0x2B, 0xBC, 0x8D, 0xDE, 0xEF,
        0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15,
        0x3B, 0x0A, 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC</pre>
</blockquote>
<p>Now, all we have to do is to upon request to lookup into this hash table for related value and XOR it</p>
<blockquote><p>protected override void HashCore(byte[] buffer, int offset, int count) {</p>
<p>for (int i = offset; i &lt; count; i++) {</p>
<p>ulong ptr = (_crc &amp; 0xFF) ^ buffer[i];</p>
<p>_crc &gt;&gt;= 8;</p>
<p>_crc ^= _crc32Table[ptr];</p>
<p>}</p>
<p>}</p>
<p>new public byte[] ComputeHash(Stream inputStream) {</p>
<p>byte[] buffer = new byte[4096];</p>
<p>int bytesRead;</p>
<p>while ((bytesRead = inputStream.Read(buffer, 0, 4096)) &gt; 0) {</p>
<p>HashCore(buffer, 0, bytesRead);</p>
<p>}</p>
<p>return HashFinal();</p>
<p>}</p>
<p>protected override byte[] HashFinal() {</p>
<p>byte[] finalHash = new byte[4];</p>
<p>ulong finalCRC = _crc ^ _allOnes;</p>
<p>finalHash[0] = (byte)((finalCRC &gt;&gt; 0) &amp; 0xFF);</p>
<p>finalHash[1] = (byte)((finalCRC &gt;&gt; <img src='http://khason.net/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> &amp; 0xFF);</p>
<p>finalHash[2] = (byte)((finalCRC &gt;&gt; 16) &amp; 0xFF);</p>
<p>finalHash[3] = (byte)((finalCRC &gt;&gt; 24) &amp; 0xFF);</p>
<p>return finalHash;</p>
<p>}</p>
</blockquote>
<p>We done. Have a good time and be good people. Also, I want to thank Boris for helping me with this article. He promised to write here some day…</p>
<p><a href="http://khason.net/images/2009/04/crc-source.zip" target="_blank">Source code for this article</a></p>
<p>Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
<li><a href='http://khason.net/dev/video-encoder-and-metadata-reading-by-using-windows-media-foundation/' rel='bookmark' title='Video encoder and metadata reading by using Windows Media Foundation'>Video encoder and metadata reading by using Windows Media Foundation</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/dev/how-to-calculate-crc-in-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
<enclosure url="http://khason.net/images/2009/04/crc-source.zip" length="" type="" />
		</item>
		<item>
		<title>WPF Line-Of-Business labs and Silverlight vs. Flash</title>
		<link>http://khason.net/tech/wpf-line-of-business-labs-and-silverlight-vs-flash/</link>
		<comments>http://khason.net/tech/wpf-line-of-business-labs-and-silverlight-vs-flash/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 10:27:50 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[TECH]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[demos]]></category>
		<category><![CDATA[DEV]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[promo]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://khason.net/tech/wpf-line-of-business-labs-and-silverlight-vs-flash/</guid>
		<description><![CDATA[Small update today (mostly interesting links)… During my last “Smart Client” session I was asked about WPF LOB application development labs. So, there are two full labs, I noticed about: Southridge, which comes from Redmond team Order Manager, which comes from Swiss DPE team Both labs include WPF ribbon and DataGrid, Southridge also come with [...]<p/>]]></description>
			<content:encoded><![CDATA[<p>Small update today (mostly interesting links)… During my last “<a href="http://khason.net/blog/slides-and-desks-from-smart-client-development-session/" target="_blank">Smart Client</a>” session I was asked about WPF LOB application development labs. So, there are two full labs, I noticed about:</p>
<ul>
<li><a href="http://www.codeplex.com/wpf/Wiki/View.aspx?title=Southridge%20Lab" target="_blank">Southridge</a>, which <a href="http://codeplex.com/wpf" target="_blank">comes from Redmond team</a> </li>
<li><a href="http://www.microsoft.com/switzerland/msdn/de/presentationfinder/detail.mspx?id=106160" target="_blank">Order Manager</a>, which <a href="http://blogs.msdn.com/swiss_dpe_team/archive/2009/02/18/windows-presentation-foundation-line-of-business-hands-on-lab-material.aspx" target="_blank">comes from Swiss DPE team</a> </li>
</ul>
<p>Both labs include <a href="http://www.codeplex.com/wpf/Wiki/View.aspx?title=WPF%20Ribbon%20Preview" target="_blank">WPF ribbon</a> and <a href="http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-datagrid-feature-walkthrough.aspx" target="_blank">DataGrid</a>, Southridge also come with <a href="http://marlongrech.wordpress.com/2008/03/20/more-than-just-mvc-for-wpf/" target="_blank">M-VV-M design</a> sample and some other interesting features. As for me, it seemed, like some parts of those labs can be easily used “as-is” for production level applications, like it was done with <a href="http://windowsclient.net/wpf/starter-kits/sce-get-started.aspx" target="_blank">SCE starter</a>, which turned into <a href="http://select.nytimes.com/gst/timesreader.html?trial=1#" target="_blank">TimesReader</a> (by the way, it has free version again). </p>
<p><img title="Line of Business Hands-On-Lab Material" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="400" alt="Line of Business Hands-On-Lab Material" src="http://khason.net/images/2009/02/image3.png" width="606" border="0" /> </p>
<p>For those, who still trying to consider what to use for their next killer app, I propose to read <a href="http://blog.webjak.net/2009/02/11/evangelising-silverlight/" target="_blank">following article</a> from Jordan, which compares between Silverlight and Flash. And then see <a href="http://msdn.microsoft.com/en-us/library/dd458809.aspx" target="_blank">composite application guidance to use Prism for Silverlight development</a>. <a href="http://channel9.msdn.com/shows/Continuum/Prismv2/" target="_blank">Here the video</a> of it usage by Adam Kinney from Channel 9</p>
<p><img title="Prism for Silverlight" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="385" alt="Prism for Silverlight" src="http://khason.net/images/2009/02/image4.png" width="573" border="0" /> </p>
<p>Have a nice day and be good people</p>
<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/tech/wpf-line-of-business-labs-and-silverlight-vs-flash/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Slides and desks from Smart Client Development session</title>
		<link>http://khason.net/blog/slides-and-desks-from-smart-client-development-session/</link>
		<comments>http://khason.net/blog/slides-and-desks-from-smart-client-development-session/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 09:59:57 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[DEV]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[promo]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://khason.net/?p=2104</guid>
		<description><![CDATA[Great thank to everybody attended yesterday at &#8220;Smart Client development&#8221; session. As promises, please see slides and desks from this session Smart Client Development View more presentations from tamirk. (tags: wpf israel)<p/>]]></description>
			<content:encoded><![CDATA[<p>Great thank to everybody attended yesterday at <a href="http://khason.net/blog/action-required-smart-client-development-present-and-future/">&#8220;Smart Client development&#8221; session</a>. As promises, please see slides and desks from this session</p>
<div id="__ss_1019853" style="width: 425px; text-align: left;"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" title="Smart Client Development" href="http://www.slideshare.net/tamirk/smart-client-development?type=presentation">Smart Client Development</a><object width="425" height="355" data="http://static.slideshare.net/swf/ssplayer2.swf?doc=0802-smart-client-development-1234429782979672-1&amp;stripped_title=smart-client-development" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slideshare.net/swf/ssplayer2.swf?doc=0802-smart-client-development-1234429782979672-1&amp;stripped_title=smart-client-development" /><param name="allowfullscreen" value="true" /></object></p>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/tamirk">tamirk</a>. (tags: <a style="text-decoration:underline;" href="http://slideshare.net/tag/wpf">wpf</a> <a style="text-decoration:underline;" href="http://slideshare.net/tag/israel">israel</a>)</div>
</div>
<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/slides-and-desks-from-smart-client-development-session/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Quick how to: Reduce number of colors programmatically</title>
		<link>http://khason.net/dev/quick-how-to-reduce-number-of-colors-programmatically/</link>
		<comments>http://khason.net/dev/quick-how-to-reduce-number-of-colors-programmatically/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 18:46:50 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[DEV]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF crossbow]]></category>

		<guid isPermaLink="false">http://khason.net/dev/quick-how-to-reduce-number-of-colors-programmatically/</guid>
		<description><![CDATA[My colleague just asked me about how to reduce a number of colors in image programmatically. This is very simple task and contains of 43 steps: First of all, you have to read a source image using (var img = Image.FromFile(name)) { var bmpEncoder = ImageCodecInfo.GetImageDecoders().FirstOrDefault(e =&#62; e.FormatID == ImageFormat.Bmp.Guid); Then create your own encoder [...]
Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
<li><a href='http://khason.net/dev/video-encoder-and-metadata-reading-by-using-windows-media-foundation/' rel='bookmark' title='Video encoder and metadata reading by using Windows Media Foundation'>Video encoder and metadata reading by using Windows Media Foundation</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>My colleague just asked me about how to reduce a number of colors in image programmatically. This is very simple task and contains of 43 <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  steps:</p>
<p><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="Simple color matrix" src="http://khason.net/images/2009/02/image2.png" border="0" alt="Simple color matrix" width="383" height="366" /></p>
<p>First of all, you have to read a source image</p>
<blockquote><p>using (var img = Image.FromFile(name)) {<br />
var bmpEncoder = ImageCodecInfo.GetImageDecoders().FirstOrDefault(e =&gt; e.FormatID == ImageFormat.Bmp.Guid);</p></blockquote>
<p>Then create your own encoder with certain color depth (32 bits in this case)</p>
<blockquote><p>var myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;<br />
var myEncoderParameter = new EncoderParameter(myEncoder, 32L);<br />
var myEncoderParameters = new EncoderParameters(1) { Param = new EncoderParameter[] { myEncoderParameter } };</p></blockquote>
<p>Then save it</p>
<blockquote><p>img.Save(name.Replace(&#8220;.png&#8221;, &#8220;.bmp&#8221;), bmpEncoder, myEncoderParameters);</p></blockquote>
<p>It it enough? Not really, because if you’re going to loose colors (by reducing color depth), it makes sense to avoid letting default WIX decoder to do this, thus you have to find nearest base colors manually. How to do this? By using simple math</p>
<blockquote><p>Color GetNearestBaseColor(Color exactColor) {<br />
Color nearestColor = Colors.Black;<br />
int cnt = baseColors.Count;<br />
for (int i = 0; i &lt; cnt; i++) {<br />
int rRed = baseColors[i].R &#8211; exactColor.R;<br />
int rGreen = baseColors[i].G &#8211; exactColor.G;<br />
int rBlue = baseColors[i].B &#8211; exactColor.B;</p>
<p>int rDistance =<br />
(rRed * rRed) +<br />
(rGreen * rGreen) +<br />
(rBlue * rBlue);<br />
if (rDistance == 0.0) {<br />
return baseColors[i];<br />
} else if (rDistance &lt; maxDistance) {<br />
maxDistance = rDistance;<br />
nearestColor = baseColors[i];<br />
}<br />
}<br />
return nearestColor;<br />
}</p></blockquote>
<p>Now, you can either change colors on base image directly</p>
<blockquote><p>unsafe {<br />
uint* pBuffer = (uint*)hMap;<br />
for (int iy = 0; iy &lt; (int)ColorMapSource.PixelHeight; ++iy)<br />
{<br />
for (int ix = 0; ix &lt; nWidth; ++ix)<br />
{<br />
Color nc = GetNearestBaseColor(pBuffer[0].FromOle());</p>
<p>pBuffer[0] &amp;= (uint)((uint)nc.A &lt;&lt; 24) | //A<br />
(uint)(nc.R &lt;&lt; 16 ) | //R<br />
(uint)(nc.G &lt;&lt; 8 ) | //G<br />
(uint)(nc.B ); //B<br />
++pBuffer;<br />
}<br />
pBuffer += nOffset;<br />
}<br />
}</p></blockquote>
<p>Or, if you’re in WPF and .NET 3.5 <a title="HLSL (Pixel shader) effects tutorial" href="http://khason.net/blog/hlsl-pixel-shader-effects-tutorial/" target="_blank">create simple pixel shader effect</a> to do it for you in hardware. Now, my colleague can do it himself in about 5 minutes <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  . Have a nice day and be good people.</p>
<p>Related posts:<ol>
<li><a href='http://khason.net/dev/rsa-private-key-import-from-pem-format-in-c/' rel='bookmark' title='RSA private key import from PEM format in C#'>RSA private key import from PEM format in C#</a></li>
<li><a href='http://khason.net/dev/video-encoder-and-metadata-reading-by-using-windows-media-foundation/' rel='bookmark' title='Video encoder and metadata reading by using Windows Media Foundation'>Video encoder and metadata reading by using Windows Media Foundation</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/dev/quick-how-to-reduce-number-of-colors-programmatically/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Math world, simple mental calculations or what&#8217;s going on with education?</title>
		<link>http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/</link>
		<comments>http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 13:07:55 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[blogging general]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[jobs]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[quiz]]></category>
		<category><![CDATA[thoughts]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/</guid>
		<description><![CDATA[Today, I want to write blog post which is absolutely not related to programming. It related to math and education in general those days. During work interviews, I see a lot of people, who was absolutely unable to calculate mentally. They just can’t understand, that it’s possible to do without calculators. When my kids (2nd, [...]<p/>]]></description>
			<content:encoded><![CDATA[<p>Today, I want to write blog post which is absolutely not related to programming. It related to math and education in general those days. During <a href="http://khason.net/tech/brilliant-yet-simple-technical-questions-can-be-used-for-work-interview/" target="_blank">work interviews</a>, I see a lot of people, who was absolutely unable to calculate mentally. They just can’t understand, that it’s possible to do without calculators. When my kids (2nd, 6th and 7th grade) were small I taught them to play with numbers, and until 4th grade (bigger kids) they were able do it. but then school teachers “killed” this ability. Why people should use calculator for simple math operations, if he can do it mentally? Shame you, the modern educational system. Let’s go back and try to understand how people were able to live without devil devices, such as calculators…</p>
<p>Following the paint of <a href="http://en.wikipedia.org/wiki/Nikolai_Bogdanov-Belsky" target="_blank">Nikolai Bogdanov-Belsky</a> “Counting in their heads”. This painting is dated 1895.</p>
<p><img title="Богданов Бельский - Устный счёт" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="570" alt="Богданов Бельский - Устный счёт" src="http://khason.net/images/2009/02/image1.png" width="714" border="0" /> </p>
<p>As you can see at the painting, peasant kids trying to solve following exercise mentally:</p>
<blockquote><p>(10<sup>2</sup> + 11<sup>2</sup> + 12<sup>2</sup> + 13<sup>2</sup> + 14<sup>2</sup>) / 365</p>
</blockquote>
<p>This is not very simple exercise, especially when should be solved without your favorite calculator. However, when I was 4th grade I learned to square two-digit numbers mentally (my, and I think, yours too): <strong>First, find the nearest multiple of ten, by raising or lowering your number, then add and remove the rest to each of numbers and add the square of oddment</strong>. For example</p>
<blockquote><p>45 * 45 = (45+5) * (45-5) + (5 * 5) = 50 * 40 + 25 = (5 * 4) * 100 + 25 = 20 * 100 + 25 = 2000 + 25 = 2025      <br />14 * 14 = (14+4) * (14-4) + (4 * 4) = 18 * 10 + 16 = 180 + 16 = 196</p>
</blockquote>
<p>So, now it can be solved easily:</p>
<blockquote><p>10<sup>2</sup> = 100       <br />11<sup>2</sup>= (11+1) * (11-1) + 1 = 12 * 10 + 1 = 121      <br />12<sup>2</sup>= (12+2) * (12-2) + 4 = 14 * 10 + 4 = 144      <br />13<sup>2</sup>= (13+3) * (13-3) + 9 = 16 * 10 + 9 = 169      <br />14<sup>2</sup>= (14+4) * (14-4) + 16 = 18 * 10 + 16 = 196</p>
</blockquote>
<p>And so on… but wait, 100+121+144 already equals 365, which is our denominator. Next sequence will bring us 169+196, which is also 365. So the answer to this black board brain teaser is 2.</p>
<p>However, it can be rather complicated to calculate 86<sup>2</sup> for instance:</p>
<blockquote><p>86<sup>2</sup> = (86 + 4) * (86 – 4) + (4 * 4) = 90 * 82 + 16…</p>
</blockquote>
<p>Let’s try another way – <strong>multiple the difference between the number and 25 by 100, then add the square of the difference or excess of the number and 50</strong>. For example</p>
<blockquote><p>86<sup>2</sup> = (86 – 25) * 100 + (86 – 50)<sup>2</sup> = 61 * 100 +&#160; 36<sup>2</sup> = 6100 + (36 – 25) * 100 + (50 &#8211; 36)<sup>2</sup> = 6100 + 1100 + 14<sup>2</sup> = 7200 + 196 = 7396</p>
</blockquote>
<p>Isn’t it really simple and fun to calculate squares of numbers?</p>
<p><em>Bonus</em>: how to calculate multiple of two digit numbers with the sum of its unity digits equals to 10? </p>
<ol>
<li>Multiply first digit of the first number by 10</li>
<li>Add 1 to first digit of second number and multiply the result by 10</li>
<li>Multiply results of step 1 and step 2</li>
<li>Deduct second number and the result of step 1</li>
<li>Multiply second digit of the first number by the result of step 4</li>
<li>Add results of steps 3 and 5</li>
</ol>
<p>Looks complicated? Let’s make it easier. Assuming that first number is X = 10x + z and second number is Y = 10y + (10 – z), the formula for quick multiplication calculation is: <strong>100 * x * (y + 1) + z * (Y – 10 * x)</strong>. For example:</p>
<blockquote><p>96 * 84 = 100 * 9 * (8+1) + 6 * (84-10 * 9) = 100 * 9 * 9 + 6 * (84 – 90) = 8100 – 6 * 6 = 8100 – 36 = 8064     <br />37 * 93 = 100 * 3 * (9+1) + 7 * (93 – 10 * 3) = 3000 + 7 * 63 = 3000 + (100 * 6 * 1 + 3 * (7 – 60)) = 3000 + 600 – 3 * 53 = 3600 – 159 = 3441</p>
</blockquote>
<p>Have a nice day and be good people. Also, throw out all hardware calculators and uninstall all software <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

