aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/Helpers/ThreadsHelper.cs
blob: 43f0f422dd6d06c758c84592fba3a47e73b482e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Tango.Logging;

namespace Tango.Core.Helpers
{
    /// <summary>
    /// Contains several UI threading helper methods.
    /// </summary>
    public static class ThreadsHelper
    {
        private static Dispatcher _dispatcher; //Holds the current dispatcher.
        public static event Action<Dispatcher> DispatcherSet;
        private static List<Action> _whenAvailableActions;
        private static bool _whenAvailableActionsInvoked;
        private static bool _hasDispatcher;

        static ThreadsHelper()
        {
            _whenAvailableActions = new List<Action>();
        }

        /// <summary>
        /// Sets the current dispatcher.
        /// </summary>
        /// <param name="dispatcher">The dispatcher.</param>
        public static void SetDisptacher(Dispatcher dispatcher)
        {
            _dispatcher = dispatcher;
            DispatcherSet?.Invoke(dispatcher);
            _hasDispatcher = true;

            if (!_whenAvailableActionsInvoked)
            {
                foreach (var action in _whenAvailableActions)
                {
                    InvokeUI(action);
                }

                _whenAvailableActionsInvoked = true;
            }
        }

        /// <summary>
        /// Invokes the UI thread.
        /// </summary>
        /// <param name="action">The action.</param>
        public static void InvokeUI(Action action)
        {
            if (_dispatcher == null)
            {
                LogManager.Default.Log(new NullReferenceException("The UI dispatcher was not set!"));
                action();
            }
            else
            {
                _dispatcher.BeginInvoke(action);
            }
        }

        /// <summary>
        /// Invokes the UI thread while blocking the current thread.
        /// </summary>
        /// <param name="action">The action.</param>
        public static void InvokeUINow(Action action)
        {
            if (_dispatcher == null)
            {
                LogManager.Default.Log(new NullReferenceException("The UI dispatcher was not set!"));
                action();
            }
            else
            {
                _dispatcher.Invoke(action);
            }
        }

        /// <summary>
        /// Invokes UI thread after a dispatcher has been set by <see cref="SetDisptacher(Dispatcher)"/>.
        /// </summary>
        /// <param name="action">The action.</param>
        public static void InvokeWhenAvailable(Action action)
        {
            if (_hasDispatcher)
            {
                InvokeUI(action);
            }
            else
            {
                _whenAvailableActions.Add(action);
            }
        }

        /// <summary>
        /// Starts a new STA thread which will be running the specified action.
        /// </summary>
        /// <param name="action">The action.</param>
        public static void StartStaThread(Action action)
        {
            Thread thread = new Thread(() =>
            {
                action();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
        }
    }
}