简体   繁体   中英

Minimize Window function only works when stepping through in Debugger (C# - Console)

**EDIT: When I put a breakpoint in my MinimizePlayer() method, and step through it, it minimizes the wmplayer process. But it does not without stepping through. I don't know why. **

I had a similar script on my Linux machine where I automated some tasks that I use every time I boot up. I was able to start Rhythmbox, begin playing my playlist, and minimize the process.

Well, I'm trying the same thing on my new Windows 10 machine, and I don't know how to minimize the wmplayer.exe process from my script.

This is what I have so far. It works fine, I just want wmplayer to be minimized:

using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;


namespace Playlist
{
    class Program
    {

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

        private enum WindowShowStyle:uint
        {
            Hide = 0,
            ShowMinimized = 2, 
            Minimize = 6
        }

        static void Main(string[] args)
        {
            Run();
        }


        public static void Run()
        {

            String username = Environment.UserName;
            username = char.ToUpper(username[0]) + username.Substring(1);
            Console.WriteLine("Hello " + username);
            Thread.Sleep(2000);
            Console.WriteLine("Opening Playlist...");
            Thread.Sleep(2000);

            Process.Start("wmplayer.exe", "C:\\Users\\" + username + "\\Music\\A_ChillstepMix.mp3");
            //Thread.Sleep(2000);
            //Console.WriteLine("Opening your IDE...");
            //Thread.Sleep(2000);
            //Process.Start("C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\devenv.exe");
            //Thread.Sleep(2000);
            MinimizePlayer();
            Thread.Sleep(2000);
            Console.WriteLine("Goodbye...");
            Thread.Sleep(2000);                
            System.Environment.Exit(0);   

        }       
        public static void MinimizePlayer()
        {
            Process[] ps = Process.GetProcesses();
            foreach(Process p in ps)
            {
                if(p.ProcessName.Contains("wmplayer"))
                {
                    IntPtr h = p.MainWindowHandle;

                    ShowWindow(h, WindowShowStyle.Minimize);
                }
            }
        }
    }
}

You can specify the WindowStyle of the process you are starting if you use the ProcessStartInfo object:

var psi = new System.Diagnostics.ProcessStartInfo();
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
psi.FileName = "wmplayer.exe";
System.Diagnostics.Process.Start(psi);

Ok, I got it. I needed to add Thread.Sleep() before the MinimizePlayer() method. Working code is as follows (I commented out the Visual Studio stuff while debugging to speed things up):

using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;


namespace Playlist
{
    class Program
    {

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

        private enum WindowShowStyle:uint
        {
            Hide = 0,
            ShowMinimized = 2, 
            Minimize = 6
        }

        static void Main(string[] args)
        {
            Run();
        }


        public static void Run()
        {

            String username = Environment.UserName;
            username = char.ToUpper(username[0]) + username.Substring(1);
            Console.WriteLine("Hello " + username);
            Thread.Sleep(2000);
            Console.WriteLine("Opening Playlist...");
            Thread.Sleep(2000);

            Process.Start("wmplayer.exe", "C:\\Users\\" + username + "\\Music\\A_ChillstepMix.mp3");
            //Thread.Sleep(2000);
            //Console.WriteLine("Opening your IDE...");
            //Thread.Sleep(2000);
            //Process.Start("C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\devenv.exe");
            Thread.Sleep(2000);
            Console.WriteLine("Minimizing Player...");
            Thread.Sleep(2000);
            MinimizePlayer();
            Thread.Sleep(2000);
            Console.WriteLine("Goodbye...");
            Thread.Sleep(5000);            
            System.Environment.Exit(0);   

        }       
        public static void MinimizePlayer()
        {
            Process[] ps = Process.GetProcesses();
            foreach(Process p in ps)
            {
                if(p.ProcessName.Contains("wmplayer"))
                {
                    IntPtr h = p.MainWindowHandle;

                    ShowWindow(h, WindowShowStyle.Minimize);
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM