Exemple #1
0
        private static bool PostToSyncContext(Form appFormWithMessageLoop, CallInUIThreadCallback action, params object[] additionalParams)
        {
            bool callFinished = false;
            int  counter      = 0;

            syncContext.Post(new SendOrPostCallback(delegate(object state)
            {
                action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                callFinished = true;
            }), null);
            while (!callFinished && counter < 300 /* 3 sec timeout */)
            {
                Thread.Sleep(10);
                counter++;
            }

            return(callFinished);
        }
        public static void Invoke(CallInUIThreadCallback action, params object[] additionalParams)
        {
            Form appFormWithMessageLoop = Application.OpenForms.Cast <Form>().FirstOrDefault(x => x != null && x.Owner == null);

            if (appFormWithMessageLoop == null)
            {
                appFormWithMessageLoop = Application.OpenForms.Cast <Form>().FirstOrDefault(x => x != null);
            }

            if (appFormWithMessageLoop != null && (Application.MessageLoop || hasOwnMessageLoop))
            {
                if (appFormWithMessageLoop.InvokeRequired)
                {
                    DebugTrace.TraceInfo("Making UIThreadCaller call on the thread of existing top level form.");
                    appFormWithMessageLoop.Invoke(action, appFormWithMessageLoop, additionalParams);
                }
                else
                {
                    DebugTrace.TraceInfo("Making UIThreadCaller call on the current thread as the found top level form runs on the same thread as us.");
                    action.Invoke(appFormWithMessageLoop, additionalParams);
                }
            }
            else if ((!Application.MessageLoop || appFormWithMessageLoop == null) && syncContext == null)
            {
                if (syncContext == null)
                {
                    DebugTrace.TraceInfo("UIThreadCaller is creating a MessageLoop thread.");
                    ThreadPool.QueueUserWorkItem(RunAppThread);
                    while (syncContext == null)
                    {
                        Thread.Sleep(10);
                    }
                }

                if (syncContext != null)
                {
                    DebugTrace.TraceInfo("Making UIThreadCaller call on an existing WindowsFormsSynchronizationContext.");
                    bool callFinished = false;
                    syncContext.Post(new SendOrPostCallback(delegate(object state)
                    {
                        action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                        callFinished = true;
                    }), null);
                    while (!callFinished)
                    {
                        Thread.Sleep(10);
                    }
                }
                else
                {
                    DebugTrace.TraceInfo("Making UIThreadCaller call directly.");
                    action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                }
            }
            else
            {
                if (syncContext == null)
                {
                    DebugTrace.TraceInfo("UIThreadCaller is creating new WindowsFormsSynchronizationContext on the current thread.");
                    syncContext = new WindowsFormsSynchronizationContext();
                }

                if (syncContext != null)
                {
                    DebugTrace.TraceInfo("Making UIThreadCaller call on an existing WindowsFormsSynchronizationContext.");
                    bool callFinished = false;
                    syncContext.Post(new SendOrPostCallback(delegate(object state)
                    {
                        action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                        callFinished = true;
                    }), null);
                    while (!callFinished)
                    {
                        Thread.Sleep(10);
                    }
                }
                else
                {
                    DebugTrace.TraceInfo("Making UIThreadCaller call directly.");
                    action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                }
            }
        }
Exemple #3
0
        public static void InvokeOnNewMessageLoop(CallInUIThreadCallback action, params object[] additionalParams)
        {
            if (!hasOwnMessageLoop)
            {
                syncContext = null;
                ThreadPool.QueueUserWorkItem(RunAppThread);
                while (syncContext == null) Thread.Sleep(10);
            }

            if (syncContext != null)
            {
                bool callFinished = false;
                syncContext.Post(new SendOrPostCallback(delegate(object state)
                {
                    action.Invoke(null, additionalParams);
                    callFinished = true;
                }), null);
                while (!callFinished) Thread.Sleep(10);
            }
            else
            {
                action.Invoke(null, additionalParams);
            }
        }
Exemple #4
0
        public static void Invoke(CallInUIThreadCallback action, params object[] additionalParams)
        {
            Form appFormWithMessageLoop = Application.OpenForms.Cast <Form>().FirstOrDefault(x => x != null && x.Owner == null);

            if (appFormWithMessageLoop == null)
            {
                appFormWithMessageLoop = Application.OpenForms.Cast <Form>().FirstOrDefault(x => x != null);
            }

            if (appFormWithMessageLoop != null && (Application.MessageLoop || hasOwnMessageLoop))
            {
                if (appFormWithMessageLoop.InvokeRequired)
                {
                    if (hasOwnMessageLoop)
                    {
                        PostToSyncContext(appFormWithMessageLoop, action, additionalParams);
                    }
                    else
                    {
                        appFormWithMessageLoop.Invoke(action, appFormWithMessageLoop, additionalParams);
                    }
                }
                else
                {
                    action.Invoke(appFormWithMessageLoop, additionalParams);
                }
            }
            else if ((!Application.MessageLoop || appFormWithMessageLoop == null) && syncContext == null)
            {
                if (syncContext == null)
                {
                    ThreadPool.QueueUserWorkItem(RunAppThread);
                    while (syncContext == null)
                    {
                        Thread.Sleep(10);
                    }
                }

                if (syncContext != null)
                {
                    PostToSyncContext(appFormWithMessageLoop, action, additionalParams);
                }
                else
                {
                    action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                }
            }
            else
            {
                if (syncContext == null)
                {
                    syncContext = new WindowsFormsSynchronizationContext();
                }

                if (syncContext != null)
                {
                    PostToSyncContext(appFormWithMessageLoop, action, additionalParams);
                }
                else
                {
                    action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                }
            }
        }
Exemple #5
0
        public static void Invoke(CallInUIThreadCallback action, params object[] additionalParams)
        {
            Form appFormWithMessageLoop = Application.OpenForms.Cast<Form>().FirstOrDefault(x => x != null && x.Owner == null);

            if (appFormWithMessageLoop != null && (Application.MessageLoop || hasOwnMessageLoop))
            {
                if (appFormWithMessageLoop.InvokeRequired)
                {
                    appFormWithMessageLoop.Invoke(action, appFormWithMessageLoop, additionalParams);
                }
                else
                {
                    action.Invoke(appFormWithMessageLoop, additionalParams);
                }
            }
            else if (!Application.MessageLoop && syncContext == null)
            {
                if (syncContext == null)
                {
                    ThreadPool.QueueUserWorkItem(RunAppThread);
                    while (syncContext == null) Thread.Sleep(10);
                }

                if (syncContext != null)
                {
                    bool callFinished = false;
                    syncContext.Post(new SendOrPostCallback(delegate(object state)
                    {
                        action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                        callFinished = true;
                    }), null);
                    while (!callFinished) Thread.Sleep(10);
                }
                else
                {
                    action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                }
            }
            else
            {
                if (syncContext == null)
                {
                    syncContext = new WindowsFormsSynchronizationContext();
                }

                if (syncContext != null)
                {
                    bool callFinished = false;
                    syncContext.Post(new SendOrPostCallback(delegate(object state)
                    {
                        action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                        callFinished = true;
                    }), null);
                    while (!callFinished) Thread.Sleep(10);
                }
                else
                {
                    action.Invoke(appFormWithMessageLoop != null && !appFormWithMessageLoop.InvokeRequired ? appFormWithMessageLoop : null, additionalParams);
                }
            }
        }