예제 #1
0
        /// <summary>
        /// ShowWithoutActivation
        /// </summary>
        //protected override bool ShowWithoutActivation
        //{
        //    get { return true; }
        //}

        //protected override void WndProc(ref Message m)
        //{
        //    //if(Visible)
        //    if (Environment.OSVersion.Version.Major < Common.CommonConstant.VistaVersion)
        //    {
        //        if (m.Msg == (int)WindowsMessages.WM_MOUSEACTIVATE)
        //        {
        //            m.Result = new IntPtr((int)WindowsMessages.MA_NOACTIVATE);
        //            return;
        //        }
        //        else if (m.Msg == (int)WindowsMessages.WM_NCACTIVATE)
        //        {
        //            //yunliang:please do not use following codes,it will cause exception in aync operations .
        //            //if (((int)m.WParam & 0xFFFF) != (int)WindowsMessages.WA_INACTIVE)
        //            //{
        //            //    if (m.LParam != IntPtr.Zero)
        //            //    {
        //            //        User32.SetActiveWindow(m.LParam);
        //            //    }
        //            //    else
        //            //    {
        //            //        User32.SetActiveWindow(IntPtr.Zero);
        //            //    }
        //            //}
        //        }
        //    }
        //    base.WndProc(ref m);
        //}

        //need more overload method
        public static void QueueWorkingItem(MethodInvoker invoker, string msg, IWin32Window parent)
        {
            using (ProgressForm pf = new ProgressForm())
            {
                pf.IsShowTextOnly = true;
                pf.Text           = msg;

                Form form = parent as Form;

                if (form != null)
                {
                    pf.OwnerForm = form;
                }
                else
                {
                    Control ctrl = parent as Control;
                    if (ctrl != null)
                    {
                        pf.OwnerForm = ctrl.TopLevelControl ?? ctrl;
                    }
                    else
                    {
                        pf.OwnerForm = ctrl;
                    }
                }


                AutoResetEvent evt = new AutoResetEvent(false);

                ThreadPool.QueueUserWorkItem(s =>
                {
                    while (!evt.WaitOne(10))
                    {
                        Application.DoEvents();
                    }
                });

                pf.DoWork += delegate
                {
                    try
                    {
                        Control ctrl = parent as Control;
                        if (ctrl != null)
                        {
                            ctrl.Invoke(invoker);
                        }
                        else
                        {
                            invoker.Invoke();
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        evt.Set();
                    }
                };
                pf.Start();
            }
        }