Esempio n. 1
0
        public void AsyncCall(Control container, string statusText, int timeout, OneParameterHandler funcNeedAsync, object objPar, OneParameterHandler completedCallback)
        {
            m_Container         = container;
            m_FuncNeedAsync     = funcNeedAsync;
            m_CompletedCallBack = completedCallback;
            m_Timeout           = timeout < 0 ? -1 : timeout;

            //Edit By Xkp
            //异步调用函数的线程放到SplashForm中进行启动,避免在SplashForm起来之前函数已经返回
            //现在新线程只会处理异步函数的调用和返回,不再处理和取消相关的逻辑
            var thread = new Thread(AsyncProcessFuncThread);

            m_PSplash = CreateSplashForm(m_CanCancel, statusText, thread, objPar);
            DialogResult result = m_PSplash.ShowDialog(container);

            m_PSplash.Close();
            //如果返回结果是OK,则是从新线程返回的,如果是其他,则是从SplashForm返回
            if (result != DialogResult.OK)
            {
                m_ThreadNoNeedRun = true;

                var asyncCallResult = new AsyncCallResult();
                asyncCallResult.CallException = new AsynCallUserAbortException();
                m_CallResult = asyncCallResult;

                InvokeCompletedCallback();
            }
            else
            {
                InvokeCompletedCallback();
            }
        }
Esempio n. 2
0
        private void AsyncProcessFuncThread(object objPar)
        {
            //如果线程起来之前,已经取消,则该线程不需要再执行
            if (m_ThreadNoNeedRun)
            {
                return;
            }

            var asyncCallResult = new AsyncCallResult();

            IAsyncResult result = m_FuncNeedAsync.BeginInvoke(objPar, null, null);

            if (result.AsyncWaitHandle.WaitOne(m_Timeout))
            {
                try
                {
                    object objRet             = m_FuncNeedAsync.EndInvoke(result);
                    var    tmpAsyncCallResult = objRet as AsyncCallResult;
                    if (tmpAsyncCallResult != null)
                    {
                        asyncCallResult.Output = tmpAsyncCallResult.Output;
                        if (tmpAsyncCallResult.CallException != null)
                        {
                            asyncCallResult.CallException = tmpAsyncCallResult.CallException;
                        }
                        else
                        {
                            asyncCallResult.Succeed = true;
                        }
                    }
                    else
                    {
                        asyncCallResult.Output  = objRet;
                        asyncCallResult.Succeed = true;
                    }
                }
                catch (Exception e)
                {
                    asyncCallResult.CallException = e;
                    asyncCallResult.Succeed       = false;
                }
            }
            else
            {
                asyncCallResult.CallException = new TimeoutException();
                asyncCallResult.Succeed       = false;
            }

            try
            {
                // m_Container may be closed and cause exception
                if (m_Container != null && !m_Container.IsDisposed)
                {
                    m_Container.Invoke(new OneParameterHandler(UICallBack), new object[] { asyncCallResult });
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("### FuncInvokeThread error: " + e.Message);
            }
        }