Question

Hi I am trying to fix that when I open a speific program aero should been disable and when the speific program close I want the aero be enabled again.

My code:

    {
    const uint DWM_EC_DISABLECOMPOSITION = 0;
    const uint DWM_EC_ENABLECOMPOSITION = 1;

    [DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")]
    extern static uint DwmEnableComposition(uint compositionAction);

    public Form1()
    {
        InitializeComponent();
    }
    int count = 1;
    public static bool EnableComposition(bool enable)
    {
        try
        {
            if (enable)
            {
                DwmEnableComposition(DWM_EC_ENABLECOMPOSITION);
            }
            else
            {
                DwmEnableComposition(DWM_EC_DISABLECOMPOSITION);
            }

            return true;
        }
        catch
        {
            return false;
        }
    }


    private void timer1_Tick(object sender, EventArgs e)
    {
        Process[] procs = Process.GetProcesses();

        foreach (Process proc in procs)
        {
            string chrome = "chrome";
            string list;
            list = proc.ProcessName;
            if (list.Contains(chrome))
            {
                EnableComposition(false);

            }
            else if(!list.Contains(chrome))
            {

                EnableComposition(true);
            }

        }


    }

}

Problem: If the program is open it runs both true and false in the if statement.

What have I do wrong?

Thanks In Advance.

Était-ce utile?

La solution

Your for loop is not correct. You are checking each process name one by one. So it depends on which process happens to come last. If "chrome" is in the middle of the process list, you will call EnableComposition(false) and on the next iteration through the for loop you will call EnableComposition(true).

Something like this should work instead:

    bool processFound = false;
    foreach (Process proc in procs)
    {
        if (proc.ProcessName.Contains("chrome"))
        {
            processFound = true;
        }
    }

    if (processFound)
    {
        EnableComposition(false);
    }
    else
    {
        EnableComposition(true);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top