0

I am using

for (int i = 0; i < 10; i++)
{
    mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -10, 0);
    System.Threading.Thread.Sleep(1000);
}

to scroll down a bit then wait a second the scroll and so on. However, it seems to wait 10 seconds then scroll down -100.

I am not sure if this is relevant but I am trying to get it to scroll down a page in a web browser as part of a windows form (once you scroll to the bottom the page loads more and you can scroll again).

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
KangarooChief
  • 381
  • 3
  • 14

1 Answers1

1

the problem is that your GUI freezes and is not updated for the duration of the for loop, because it is running on the same thread. To See the changes every second you need to run it on a background thread.

You can also use a System.Timers.Timer to do that job. Set it up as

System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000;    
timer.AutoReset = true;

and hook up the Elapsed event in which you handle the scrolling

timer.Elapsed += Timer_Elapsed;
timer.Start();

you would also need a counter like in your for-loop. Count down until 0 with it in the event handler

int counter = 10;

private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    if(counter > 0)
    {
        mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -10, 0);
        counter--;
    }
    else
    {
        System.Timers.Timer t = sender as System.Timers.Timer;
        t.Stop();
    }
}

This way you can stop the timer after 10 iterations

As suggested by Jakub Dąbek you can also use System.Windows.Forms.Timer. This would look like this:

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += Timer_Tick;
timer.Start();

private void Timer_Tick(object sender, EventArgs e)
{
    // here the same code as above    
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • @KangarooChief you can use a timer. I can give you an example in a minute – Mong Zhu Aug 15 '17 at 14:26
  • For Windows Forms apps you should use [`System.Windows.Forms.Timer`](https://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v=vs.110).aspx) – Jakub Dąbek Aug 15 '17 at 14:33
  • I'm not sure if I have done it right I put `System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 1000; ` at the start of my method, `timer.Elapsed += Timer_Elapsed;` in my for loop and `private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -10, 0); }` as a separate method but it doesn't seem to scroll now. – KangarooChief Aug 15 '17 at 14:33
  • @KangarooChief sorry forgot to mention that you have to start the timer. I made an edit and included the loop with a counter. but you have also to set the `timer.AutoReset = true;` – Mong Zhu Aug 15 '17 at 14:35
  • @JakubDąbek what is the advantage of the Forms timer? – Mong Zhu Aug 15 '17 at 14:36
  • It executes the delegate on the UI thread - see https://stackoverflow.com/a/12977177/7931009 – Jakub Dąbek Aug 15 '17 at 14:39
  • @MongZhu Thank you very much. Its working now (although I just wanted to point out that you are missing a `;` after `timer.Start()` in case some one else wanted to use your solution). – KangarooChief Aug 15 '17 at 14:39
  • @JakubDąbek included your suggestion into my answer. Thanx for the hint – Mong Zhu Aug 15 '17 at 14:43
  • @KangarooChief you are welcome. Thank you for pointing out. I changed it – Mong Zhu Aug 15 '17 at 14:43
  • @KangarooChief can you in return post the implementation of `mouse_event` or even where you have it from? – Mong Zhu Aug 15 '17 at 14:56
  • @MongZhu Sorry didn't see you comment. Good to see you found it. – KangarooChief Aug 15 '17 at 15:14