Framework / System / Threading Considerations
In This Topic
    Threading Considerations
    In This Topic

    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.

     Types of Threads
    NOV distinguishes only between two types of thread – the UI Thread and all others. The UI Thread is the thread on which the NOV NApplication object is initialized during the NOV Installation.
    The UI Thread can be obtained from the NApplication.UIThread property. You can also check whether your code is currently running on the UI Thread by calling the NApplication.IsUIThread property.
     Launching Other Threads

    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
    });
    
     Getting Back on the UI Thread

    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();
            });
        });
    }