<?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>Dapfor.Net Grid Blog &#187; gui</title>
	<atom:link href="http://www.blog.dapfor.com/tag/gui/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blog.dapfor.com</link>
	<description>.Net Grid and MFC Grid</description>
	<lastBuildDate>Fri, 09 Sep 2016 05:40:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to avoid exceptions upon exiting application</title>
		<link>http://www.blog.dapfor.com/how-to-avoid-exceptions-upon-exiting-application</link>
		<comments>http://www.blog.dapfor.com/how-to-avoid-exceptions-upon-exiting-application#comments</comments>
		<pubDate>Fri, 23 Dec 2011 08:09:10 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[Performance]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[threadsafety]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=29</guid>
		<description><![CDATA[Almost every modern application is multi-threaded. Threads are needed to work with various devices and IP protocol stack and to perform demanding computing operations. Results of work performed in these threads should be displayed in graphical controls. Control.Invoke / Control.BeginInvoke are the main methods used for thread synchronization.  These methods work excellent only when control [...]]]></description>
			<content:encoded><![CDATA[<p>Almost every modern application is multi-threaded. Threads are needed to work with various devices and IP protocol stack and to perform demanding computing operations. Results of work performed in these threads should be displayed in graphical controls. <em><strong>Control.Invoke</strong></em> / <em><strong>Control.BeginInvoke</strong></em> are the main methods used for thread synchronization.  These methods work excellent only when control is initialized and contains non-zero wondow handle.</p>
<p>For synchronization programmers often use a Form object that contains controls requiring content update. They probably do it because it’s so easy to implement. It’s so easy to call the following code:</p>
<pre>myForm.BeginInvoke(new MethodInvoker(()=&gt;
{
    //a code to be executed in the GUI thread
}), null);</pre>
<p>However, simple doesn’t mean correct! GUI thread is not a separate control property but something that is used by all controls. GUI thread lifetime exceeds lifetime of an individual control. Controls and forms can be created or deleted, and all these operations are done via message loop common for all controls and forms. Now let’s say that we started synchronization via<em><strong> myForm.BeginInvoke()</strong></em> upon notification from non-GUI thread. This call adds a window message to message loop. When this message is processed, window handle associated with it is dispatched to specified control or form, and after that a delegate is called. So, what if the form has been closed before that? An exception will be thrown during dispatching. Don’t think that this situation is unusual. When application terminates, it closes windows and performs various actions with end devices, IP stack, etc. These devices in turn may notify GUI of their termination followed by synchronization. This may cause frequent exceptions that are hard to understand and even harder to fix.</p>
<p>We are absolutely convinced that there is only one way out – to create a global dispatcher that is initialized when application starts and is closed when it stops. Dispatcher may be based on the main form launched in <em><strong>Application.Run()</strong></em>. All multi-threaded synchronization should be performed only via this dispatcher. Besides, this approach enables use of other dispatcher for unit tests. As an example we will demonstrate this idea with a code:</p>
<p>&nbsp;</p>
<pre>//Dispatcher class
static class Dispatcher
{
    private static ISynchronizeInvoke _invoker;

    internal static void Init(ISynchronizeInvoke invoker) {_invoker = invoker;}

    public static ISynchronizeInvoke GuiDispatcher { get { return _invoker; }}
}

//How to initialize
using (Form mainForm = new Form())
{
    Dispatcher.Init(mainForm);

    //other initialization code...

    //Start the main loop as usual
    Application.Run(mainForm);
}

//How to use:
Dispatcher.GuiDispatcher.BeginInvoke(new MethodInvoker(()=&gt;
{
    //some code to be executed in the GUI thread
}), null);</pre>
<p>&nbsp;<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/how-to-avoid-exceptions-upon-exiting-application/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance of synchronization with GUI thread</title>
		<link>http://www.blog.dapfor.com/performance-of-synchronization-with-gui-thread</link>
		<comments>http://www.blog.dapfor.com/performance-of-synchronization-with-gui-thread#comments</comments>
		<pubDate>Tue, 20 Dec 2011 12:00:06 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[Performance]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=18</guid>
		<description><![CDATA[It is well known that all graphical controls should work in one thread. There are many articles on this subject, so we shall not repeat them. In multi-threaded applications every call should be synchronized with the main thread containing windows message loop. Control is a base class that provides Control.Invoke and Control.BeginInvoke methods. The first [...]]]></description>
			<content:encoded><![CDATA[<p>It is well known that all graphical controls should work in one thread. There are many articles on this subject, so we shall not repeat them. In multi-threaded applications every call should be synchronized with the main thread containing windows message loop. Control is a base class that provides <strong>Control.Invoke</strong> and <strong>Control.BeginInvoke</strong> methods. The first of these methods blocks the calling thread until the code contained in delegate is executed. The second method blocks the calling thread only for the time of adding delegate in queue. Execution of this delegate code is done in parallel in GUI thread.</p>
<p>Both <strong>Control.Invoke</strong> and <strong>Control.BeginInvoke</strong> methods may accept different delegate types and their parameters. However, this causes a serious performance issue as the main code calling method is Delegate.DynamicInvoke(params object[] args), which in turn uses low-performance reflection. At the same time, for some delegates such as <strong>EventHandler</strong>, <strong>MethodInvoker</strong> and <strong>WaitCallback</strong> the code is called directly. In the time of code execution the delegate is checked for belonging to one of the above types with specified number of parameters, and if it doesn’t – DynamicInvoke() is called.</p>
<p>From practical point of view the synchronization process should look as follows:</p>
<pre>    someControl.BeginInvoke(new MethodInvoker(delegate
    {
        //a code here
    }));</pre>
<p>&nbsp;</p>
<p>For comparison, if custom delegates are used, performance of Invoke call from non-GUI thread equals on the average:</p>
<p>Custom delegate: 25,000 calls per second.<br />
MethodInvoker: 40,000 calls per second.</p>
<p>If Control.Invoke is called from GUI thread that doesn’t count inter-thread synchronization rate and doesn&#8217;t have context switches, the results are as follows:</p>
<p>Custom delegate: 150,000 calls per second<br />
MethodInvoker:  1,200,000 calls per second.</p>
<p>The example code is provided below:</p>
<pre>    ThreadPool.QueueUserWorkItem(delegate
    {
        int k = 0;

        DateTime dt = DateTime.UtcNow;
        for (int i = 0; i &lt; 10000; ++i)
        {
            //CustomDelegate d = delegate { k++; };
            //Invoke(d, new object[]{EventArgs.Empty});

            Invoke(new MethodInvoker(delegate { k++; }));
        }
        TimeSpan ts = DateTime.UtcNow - dt;

        MessageBox.Show(string.Format("{0} invokes. Perf = {1} call/sec", k, 1000*k/ts.TotalMilliseconds));
    });</pre>
<p>&nbsp;</p>
<p>Summary. To improve performance it is always better to use MethodInvoker that improves performance by 50% when calling Control.Invoke method from non-GUI thread and tenfold when calling from GUI thread.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/performance-of-synchronization-with-gui-thread/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
