forked from appccelerate/appccelerate.github.com
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added async and user interface thread synchronizer
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 -> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |