Nevron Open Vision is engineered with multi-threading in mind. The purpose of this document is to get you acquainted with the fundamental multi-threading principles used in NOV.
You can use the standard .NET means to create a thread – for example:
Running Code on Another Thread |
Copy Code
|
---|---|
void MyFunction() { // Some work here } Thread myThread = new Thread(new ThreadStart(MyFunction)); myThread.Start(); |
Since thread creation and starting can be slow, NOV implements a thread pooling mechanism which aims to improve the starting performance of threaded tasks. In NOV you can also launch background threads like this:
Launching Pooled Threads in NOV |
Copy Code
|
---|---|
AutoResetEvent endedEvent = NThreadPool.StartTask(() =>
{
// Some work here
});
|
When a certain task running on a background thread has been completed, or a progress has achieved, it is usually necessary to update the UI of the application, or in other words execute some code on the UI thread. In NOV you can do that by calling the NApplication.BeginInvoke method, like shown in the following example function that updates a label with the result of a factories calculation.
Updating the DOM on the UI Thread |
Copy Code
|
---|---|
void CalculateFactorial(int n, NLabel label) { NThreadPool.StartTask(() => { int factorial = 1; for (int i = n; i > 0; i--) factorial *= i; NApplication.BeginInvoke(() => { label.Text = factorial.ToString(); }); }); } |