Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

User Image
You Tube19707
18 discussion posts
Hello. I have a program where i want to have the ability that whenever it´s window is not in focus, it triggers something. I wanted to do that in DF, but i saw there is only a command for "Window Focus". I would need the exact opposite. Can you do something about that?
Thank you very much.
Sep 7, 2018  • #1
Keith Lammers (BFS)'s profile on WallpaperFusion.com
There isn't really a way to do that, because there's no "focus lost" message that we can hook globally. What you could do is have a Scripted Function that checks what the focused process is, and if it's not your specific program, it executes the rest of the code. Then run that from a Window Focused trigger that's set to fire for all applications (*.* in the process filename).

Here's some sample code for the scripted function:

Code

using System;
using System.Drawing;

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("notepad.exe"))
        {
            // do stuff here
        }
    }
}


Hope that helps!
Sep 7, 2018  • #2
User Image
You Tube19707
18 discussion posts
Thanks for the tipp. I will try that.
Can you please take a look in the thread when you have time? :)
I added something to that, in case you missed it. :)

https://www.displayfusion.com/Discussions/View/a-few-requests-from-a-new-user/?ID=d388e1d9-e687-42ce-822b-ddf0d569fb37
Sep 7, 2018  • #3
User Image
You Tube19707
18 discussion posts
Hello again.
I tried some code based on the "Downloadable Scripted Functions".
But i can´t get behind it to have it triggered when it´s NOT in focus.
Here´s my Script so far:

Code

using System;
using System.Drawing;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Trigger target when run by a Trigger rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
using System;
using System.Drawing;

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        //this is where i want to have the Window NOT in Focus, but i can´t get how.
        if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("ExampleProgram.exe"))
           IntPtr mainWindow = BFS.Application.GetMainWindowByFile("ExampleProgram.exe");
           BFS.Window.Focus(mainWindow);
        {
               uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe.exe", "suspendprocess ExampleProgram.exe");
        }
    }
}


Edit:
I should also say that when i save it, DF says the code is invalid.
Sep 8, 2018 (modified Sep 8, 2018)  • #4
Keith Lammers (BFS)'s profile on WallpaperFusion.com
There are a few issues here, but I'm a bit confused. Are you wanting it to run that nircmd command when a certain application loses focus?
Sep 10, 2018  • #5
User Image
You Tube19707
18 discussion posts
Quote:
There are a few issues here, but I'm a bit confused. Are you wanting it to run that nircmd command when a certain application loses focus?


That´s because "Nircmd" has a command to Suspend a Process.
I have a Program that is using 20-25 CPU when using. I have it running in the background the whole time.
So i found this program "Nircmd", because it has the perfect two commands for that case.

nircmd.exe resumeprocess "Example.exe"
nircmd.exe suspendprocess "Example.exe"

This works so good and flawless. Now i know that DF can do Triggering with the Windows (as you also stated before).
So i wanted to have the following Trigger.
When the window of this programs (Example.exe) is NOT in focus, it triggers

nircmd.exe suspendprocess"Example.exe".

When i alttab to it back, it triggers
nircmd.exe resumeprocess "Example.exe"
for resuming a process

So now i can use it all the time and have it running in the background without heavy CPU Load. Please don´t ask why i have to run this all the time. :)
But again, i can´t really find a way to have all this in a trigger in the first place.
Sep 10, 2018 (modified Sep 10, 2018)  • #6
Keith Lammers (BFS)'s profile on WallpaperFusion.com
Ah ok, your code should look like this then (make sure to delete all code from the scripted function before pasting this in):

Code

using System;
using System.Drawing;

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        // Check if the window is not in focus
        if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("ExampleProgram.exe"))
        {
               uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe.exe", "suspendprocess ExampleProgram.exe");
        }
    }
}


Then you create a Trigger rule that looks like the attached screenshot.

Hope that helps!
• Attachment: Trigger.png [55,018 bytes]
Trigger.png
Trigger.png
Sep 11, 2018  • #7
User Image
You Tube19707
18 discussion posts
Thank you so much.
The Script now works flawlessy without any problems.

Whenever i move my mouse away from "Example.exe"´s Window, Nircmd jumps in and suspence the Process to Zero.
But......
I hoped this would be the last help i wanted from you.
The last thing i need is to have, whenever my mouse goes back to "Example.exe"´s Window and Focus on it, Nircmd jumps in with the "Process Resume" Command to resume the Program out of it´s frozen State.

I have this script here based on your previous script so far:

Code

using System;
using System.Drawing;

public static class DisplayFusionFunction
{
public static void Run(IntPtr windowHandle)
{
// Check if the window is not in focus
if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("Example.exe"))
{
uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe", "suspendprocess Example.exe");
}
// Until here it works, but how do i go along then from here? I want to have a second function where it´s looking when the 
// window is focused again.
if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("Example.exe"))
{
uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe", "resumeprocess Example.exe");
}
}
}


The problem also is that of course after you frozen the command with the "Suspendprocess" Command, after a while the Windows Ghosting jumps in. And from that point i don´t know how to go along. Because of the Frozen State, the Programs Window do not notice that it´s focused.
A Workaround would be instead of having the Example.exe in the Script, instead it uses the Program´s Window Title. So when using the Title "ExampleTitle (Not Responding), the Trigger will know which exactly Window i mean.

Okay, i believe this will be hard to understand what i want here. Please say it to me if something is not clear. :)
I already readed the Help File for the scripts, but can´t find it. But i also saw that command for "Window Hung", which could maybe also be some help in the Script.
Sep 11, 2018 (modified Sep 13, 2018)  • #8
Keith Lammers (BFS)'s profile on WallpaperFusion.com
You just need to take the ! out of line 15. That exclamation means "if it's false". So you're script would look like this now (I've added comments to clarify the steps):

Code

using System;
using System.Drawing;

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        // Check if the window is not in focus
        if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("Example.exe"))
        {
        // Suspend the process
        uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe", "suspendprocess Example.exe");
        }
        // Check if the window is in focus
        if (BFS.Application.GetMainFileByWindow(windowHandle).Contains("Example.exe"))
        {
        // Resume the process
        uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe", "resumeprocess Example.exe");
        }
    }
}
Sep 13, 2018 (modified Sep 13, 2018)  • #9
User Image
You Tube19707
18 discussion posts
Okay, thank you. Your script works wonderful.
But i have sadly another problem.
Whenever the program get´s suspended, it of course creates the "Ghost" Image over it (White Overlay over the Window) and the window title get´s the "(Not Responding)" text added to the Program´s Title (examle is Picture 01)
And this is where the script won´t work anymore, because it can´t see the Program´s Progress or something.
I thought of maybe using this ("Example: Source" is the title of the program)

Code

if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("Example.exe"))


but instead using

Code

if (BFS.Window.GetText(windowHandle).Contains("Example: Source (Not Responding)"));


or

Code

if (BFS.Window.IsHung(windowHandle).Contains("Example: Source"));


But nothing i tried since 5 hours works here. I don´t get it.
As usual, i always use this Function with the Trigger from Picture 02.

So in Summary i want to do the following:

1.- Start the Program (Example.exe)

2.- When Mouse is not over Example.exe´s Window, then launch "nircmd.exe suspendprocess Example.exe".

3.- Program (Example.exe) get´s frozen and don´t use any unescesarry CPU Resources while it´s not focused.

4.- When Mouse is over Example.exe´s Window, then launch "nircmd.exe resumeprocess Example.exe"

5.- Program (Example.exe) get´s out of the Frozen State and you can work on the Program again. Until it´s not focused, then the whole Function starts from the beginnning.

At Point 4, this is where it seems not to work, because the program get´s not unfrozed anymore. So maybe using the Window Title or the Function Windows Hung as i described above instead of only using the Programs Exe could be working.

Sorry for all that questions, but as i said before, i already tried that since 5 hours myself searching through Example Scrips and whatnot.
Thank you very much.
• Attachment [protected]: Picture 01.jpg [871,955 bytes]
• Attachment [protected]: Picture 02.jpg [145,507 bytes]
Sep 14, 2018 (modified Sep 14, 2018)  • #10
Keith Lammers (BFS)'s profile on WallpaperFusion.com
I'm trying to test this out here, but I can't get nircmd to work, even manually. I run "nircmd.exe suspendprocess notepad.exe" but it doesn't seem to do anything at all. Am I missing anything here?
Sep 14, 2018  • #11
User Image
You Tube19707
18 discussion posts
Quote:
I'm trying to test this out here, but I can't get nircmd to work, even manually. I run "nircmd.exe suspendprocess notepad.exe" but it doesn't seem to do anything at all. Am I missing anything here?


No, the command seems okay.
But you know that nircmd is a external tool and not build in Windows?

http://www.nirsoft.net/utils/nircmd.html

And also maybe put the target into Quotation marks to be sure:
nircmd.exe suspendprocess "notepad.exe"
Sep 14, 2018  • #12
Keith Lammers (BFS)'s profile on WallpaperFusion.com
Yep, I have it :)

I've tried with and without quotes as well.

Maybe it would make more sense to just have two scripts, one for suspending the process and one for resuming it, and then you can just run them using hotkeys? It sounds like maybe the suspended window doesn't send any Window Focused messages :(
Sep 14, 2018  • #13
User Image
You Tube19707
18 discussion posts
Quote:
Yep, I have it :)

I've tried with and without quotes as well.

Maybe it would make more sense to just have two scripts, one for suspending the process and one for resuming it, and then you can just run them using hotkeys? It sounds like maybe the suspended window doesn't send any Window Focused messages :(


Hmm. But do you get it suspended or not?
I have already a hotkey alternative with Autohotkey. But a seamless Way is always better. But for what is the

Code

if (BFS.Window.IsHung(windowHandle).Contains("Example: Source"));


then here when i can not use it?
It seems that this should be the code we could use for that issue. Because in the Window Title it also says (Not Responding). Display Fusion should see it.
Sep 14, 2018  • #14
Keith Lammers (BFS)'s profile on WallpaperFusion.com
That will only return a true/false. "e.g. is the window hung?"

It's not for finding a hung window. You could try this to identify whether the focused window is hung:

Code

BFS.Window.IsHung(BFS.Window.GetFocusedWindow)


Or the whole script like this:

Code

using System;
using System.Drawing;

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        // Check if the window is not in focus
        if (!BFS.Application.GetMainFileByWindow(windowHandle).Contains("Example.exe"))
        {
        // Suspend the process
        uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe", "suspendprocess Example.exe");
        }
        // Check if the window is in focus
        if (BFS.Window.IsHung(BFS.Window.GetFocusedWindow()))
        {
        // Resume the process
        uint appID = BFS.Application.Start("A:\\PROGRAMS\\_NirCmdc\\nircmd.exe", "resumeprocess Example.exe");
        }
    }
}
Sep 14, 2018  • #15
User Image
You Tube19707
18 discussion posts
Thanks for the quick help.
But that didn´t help either. The program stays frozen. Still don´t understand why, but whatever.
I think i will go with the hotkey way.
Sep 14, 2018 (modified Sep 14, 2018)  • #16
Keith Lammers (BFS)'s profile on WallpaperFusion.com
Yeah, sounds like that is probably the way to go. I couldn't even get nircmd to actually suspend any apps here, so I'm a bit stuck anyways :(
Sep 17, 2018  • #17
Subscribe to this discussion topic using RSS
Was this helpful?  Login to Vote(-)  Login to Vote(-)