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
}