-3

So I want a program to do a function when the toggle is toggled and the program is not in focus. This is the current code.

private void Toggle_CheckedChanged(object sender, EventArgs e)
     {
          while (Toggle.Checked = true) 
             {
                FUNCTION
             }
     }

I would like it to be like this

private void Toggle_CheckedChanged(object sender, EventArgs e)
         {
              while (Toggle.Checked = true) & activewindow = false
                 {
                    FUNCTION
                 }
         }

I have already tried implementing different functions, but it was flagged as 'method group'.

Does anyone know how to do this?

User
  • 35
  • 8
  • 1
    Does this answer your question? [Determine if current application is activated (has focus)](https://stackoverflow.com/questions/7162834/determine-if-current-application-is-activated-has-focus) – 41686d6564 stands w. Palestine Jul 11 '20 at 02:04
  • Also, don't you want that `while` to be an `if`? – 41686d6564 stands w. Palestine Jul 11 '20 at 02:05
  • Cannot assign to 'ApplicationIsActivated' because it is a 'method group' and also I want it to be while because I want it to loop whilst it is running. – User Jul 11 '20 at 02:07
  • What's checking the box in the app? If it's a user, the focus is going to be on the application when they take the action of checking the element. Also, running a while loop inside of this method will block the main thread and freeze your app. – Ryan Wilson Jul 11 '20 at 02:07
  • It's a user, but once the toggle is clicked and the user isn't on the active window, it will run the function. – User Jul 11 '20 at 02:09
  • Try `Toggle.Checked && !ApplicationIsActivated()`, which means `Toggle.Checked == true && ApplicationIsActivated() == false`. _"also I want it to be while because I want it to loop whilst it is running"_ Why are you putting it under `Toggle_CheckedChanged` then? That code will be executed only when the `Checked` property of `Toggle` is changed. – 41686d6564 stands w. Palestine Jul 11 '20 at 02:10
  • @User I think you need to rethink your design on this. I'd run a timer in your application class which checks the value of the element to be checked, if it is checked when the timer fires, check to see if the application is not focused and call your method. – Ryan Wilson Jul 11 '20 at 02:11
  • I'm trying to make it that when the user clicks the toggle, it checks if it is true, then if it is true and the application isn't in focus, it will run the script. – User Jul 11 '20 at 02:13
  • @User As I stated above, the user will focus on the app when they check the box, the event will fire so quickly that I doubt the user will be able to remove focus on the app quickly enough for the conditions to be met. Here's some info on timers: (https://stackoverflow.com/questions/15879476/creating-a-background-timer-to-run-asynchronously) – Ryan Wilson Jul 11 '20 at 02:16
  • How exactly does one toggle the checkbox **and** the window **not** be `active`? –  Jul 11 '20 at 02:18
  • @Ryan Wilson I want it to loop until the toggle is toggled off OR the window is active. The event won’t just run once so it won’t be fired too quickly. – User Jul 11 '20 at 04:09

1 Answers1

1

You can use async/await to accomplish this:

private async void Toggle_CheckedChanged(object sender, EventArgs e)
{
    while(Toggle.Checked)
    {
        await Task.Delay(250); // you need some kind of delay in the loop...maybe?
        if (!ApplicationIsActivated())
        {
            await Task.Run(() =>
            {
                FUNCTION();
            });
        }
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40