“Not Responding” in window title when running in new process

前端 未结 8 1281
清酒与你
清酒与你 2021-02-07 14:39

I have a long running method that must run on UI thread. (Devex - gridView.CopyToClipboard())

I do not need the UI to be responsive while c

8条回答
  •  走了就别回头了
    2021-02-07 15:16

    You could start the process hidden and then check if responding and bring it back into view when complete....your splash screen would show its still "responding".

     Process proc = new Process();
     proc.StartInfo.FileName = ".exe"
    
     proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    

    Edit: You could also create a Timer event watching the other process and roll your own timeout logic

        DateTime dStartTime = DateTime.Now;
        TimeSpan span = new TimeSpan(0, 0, 0);
        int timeout = 30; //30 seconds        
    
        private void timer1_Tick(Object myObject, EventArgs myEventArgs)
        {
            while (span.Seconds < timeout)
            {
                Process[] processList = Process.GetProcessesByName("");
                if (processList.Length == 0)
                {
                    //process completed
                    timer1.Stop();
                    break;
                }
                span = DateTime.Now.Subtract(dStartTime);
            }
            if (span.Seconds > timeout)
            {
                Process[] processList = Process.GetProcessesByName("");
    
                //Give it one last chance to complete
                if (processList.Length != 0)
                {
                    //process not completed
                    foreach (Process p in processList)
                    {
                        p.Kill();
                    }
                }
                timer1.Stop();
            }
        }
    

    Edit2

    You could use pInvoke "ShowWindow" to accomplish hiding and showing the window too after its started

    private const int SW_HIDE = 0x00;
    private const int SW_SHOW = 0x05;
    
    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    

提交回复
热议问题