<?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; threadsafety</title>
	<atom:link href="http://www.blog.dapfor.com/tag/threadsafety/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>Binding list and thread safety</title>
		<link>http://www.blog.dapfor.com/binding-list-and-thread-safety</link>
		<comments>http://www.blog.dapfor.com/binding-list-and-thread-safety#comments</comments>
		<pubDate>Mon, 13 Feb 2012 21:05:28 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[Data binding]]></category>
		<category><![CDATA[thread safety]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[threadsafety]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=64</guid>
		<description><![CDATA[Data binding is the basis of modern applications based on separation of data layer from presentation layer. The main purpose of such separation is to make application logic independent of its representation. Otherwise, logic code should not directly call presentation layer class methods (i.e. Control class methods). When internal state changes, business logic layer sends [...]]]></description>
			<content:encoded><![CDATA[<p>Data binding is the basis of modern applications based on separation of data layer from presentation layer. The main purpose of such separation is to make application logic independent of its representation. Otherwise, logic code should not directly call presentation layer class methods (i.e. Control class methods). When internal state changes, business logic layer sends a GUI notification via <strong>IBindingList</strong> / <strong>INotifyPropertyChanged</strong> interfaces and the presentation layer displays modified data on screen. </p>
<p>It is necessary to note that there is a serious obstacle to implementing such event-driven model – thread safety. Business logic shouldn’t know anything of presentation, not only with regard to direct links to GUI classes but also with regard to thread used for subscriber notification. However, in actual applications things are more complicated. Application usually has a single GUI thread where the controls work. All control methods should be called only from this thread. However, data binding doesn’t consider this aspect, i.e. when a data source is connected, most controls imply that all notifications come from the main thread and don’t synchronize. This results in application crashes when notifications arrive from non-GUI thread. </p>
<p>While working with numerous real-world applications we have noted that developers often don’t follow the principle of separating data layer from the presentation layer because it is necessary to synchronize notifications with GUI thread. This means that synchronization is performed by business logic and not by the presentation layer. Thus, the required call of <strong>Control.Invoke</strong>/<strong>Control.BeginInvoke</strong> method is placed to business logic that should contain a reference to control that will process notifications. </p>
<pre>
class FeedCollection : BindingList&lt;Feed&gt;
{
    <strong>private Control _control;</strong>

    //The method is called when data is updated (for ex. data comes from TCP/IP)
    void OnUpdateReceiced(...)
    {
        _control.Invoke(new MethodInvoker(delegate
        {
            //Raise notification in GUI thread
            OnListChanged(new ListChangedEventArgs(...));
        }));
    }
}

DataGrid someGrid = ...;
FeedCollection feed = ...;
someGrid.DataSource = feed;
</pre>
<p><br/><br />
It is only one of the examples where rules of separating logic from presentation are violated. When our developers were working on .Net Grid, they have initially designed its architecture to receive notifications from any thread and to perform thread synchronization after that. Thus, business logic can be fully separated from the presentation layer and safely notify subscribers without pre-synchronization of threads and therefore without unnecessary references to GUI controls. </p>
<pre>
class FeedCollection : BindingList&lt;Feed&gt;
{
    //The method is called when data is updated (for ex. data comes from TCP/IP)
    void OnUpdateReceiced(...)
    {
        //Raise notification directly from non-GUI thread
        //Dapfor .Net Grid will synchronize threads itself.
        OnListChanged(new ListChangedEventArgs(...));
    }
}

Dapfor.Net.Ui.Grid grid = ...;
FeedCollection feed = ...;
grid.DataSource = feed;
</pre>
<p><br/></p>
<p>As we have said above, ensuring thread safety is not a trivial task for application developers. Complex modern applications may contain a  lot of assemblies. Some of them may contain codes with graphical controls, others may contain business logic, various math libraries, code  for TCP/IP interaction, etc. However, limitations related to GUI operation only in one thread and thread synchronization method require unneeded and dangerous dependencies of business logic from graphical components (Control.Invoke/Control.BeginInvoke).  This may seriously violate the principle of business logic independence from its presentation. Dapfor .Net Grid doesn’t just enable thread synchronization, but also makes it possible to completely avoid such dependencies using an event-driven model. It means that if the application is well architected,  business logic assemblies will not (and should not!) depend on Dapfor assemblies and System.Windows.Forms libraries.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/binding-list-and-thread-safety/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
	</channel>
</rss>
