Skip to content

Commit

Permalink
added async and user interface thread synchronizer
Browse files Browse the repository at this point in the history
  • Loading branch information
ursenzler committed Apr 3, 2012
1 parent d7369ae commit 0e67324
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions _config.yml
Expand Up @@ -37,9 +37,12 @@ navigation:
- name: Evaluation Engine
link: evaluationengine.html
- name: Async
link: async.html
grandchildren:
- name: AsyncWorker
link: asyncworker.html
- name: User Interface Thread Synchronizer
link: userinterfacethreadsynchronizer.html
- name: State Machine
link: statemachine.html
- name: Road Map
Expand Down
14 changes: 14 additions & 0 deletions async.html
@@ -0,0 +1,14 @@
---
layout: documentation
title: EvaluationEngine
teaser: Decouple business logic from control flow
navigation:
- name: Async Worker
link: asyncworker.html
- name: User Interface Thread Synchronizer
link: userinterfacethreadsynchronizer.html
---
<h2>Async Components</h2>
<p>
The async package includes several components for asynchronous execution. See navigation on the right -&gt;
</p>
58 changes: 58 additions & 0 deletions userinterfacethreadsynchronizer.html
@@ -0,0 +1,58 @@
---
layout: documentation
title: User Interface Thread Synchronizer
teaser: Your quick way to execute some code on the user interface thread when you are working hard on a worker thread.
navigation:
- name: Overview
link: userinterfacethreadsynchronizer.html
---
<h2>User Interface Thread Synchronizer</h2>
<p>
With the <code>UserInterfaceThreadSynchronizer</code> you can execute code on the user interface thread from any thread.
This is usefull when an operation runs on a worker thread and needs for example to update a label on the UI or needs to create a UI component for later usage.
</p>
<p>
The user interface thread synchronizer needs to be created on the user interface thread. Otherwise, it is not capable of switching to the user interface thread.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
var synchronizer = new UserInterfaceThreadSynchronizer();
]]></script>
<p>
Later, you can use the user interface thread synchronizer to execute code on the UI thread:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
synchronizer.Execute(() => { /* this code will be executed on UI thread */ });
]]></script>
<p>
This will block the worker thread and execute the action specified on the user interface thread.
You can also pass up to four arguments to the <code>Execute</code> method:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
synchronizer.Execute(
(a, b) => { /* this code will be executed on UI thread */ },
17,
42);
]]></script>
<p>
You can return values back to the worker thread:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
var result = synchronizer.Execute(() => { return 42; });
]]></script>
<p>
And you can execute the action asynchronously. Obviously, it's not possible to return a value.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
synchronizer.ExecuteAsync(() => { /* executed asynchronously on UI thread */ });
]]></script>

<h3>Testability</h3>
<p>
For simpler tests, you can tell the user interface thread synchronizer to run synchronously by passing a <code>SyncrhonizationContext</code> in the constructor:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
var synchronizer = new UserInterfaceThreadSynchronizer(new SynchronisationContext());
]]></script>
<p>
Now, all actiosn are executed synchronously when <code>Execute</code> or <code>ExecuteAsync</code> is called.
</p>

0 comments on commit 0e67324

Please sign in to comment.