Pretty simple way, and perhaps overly complex way to do it. Spawn a thread with a while loop which continuously adds to your GUI thread's dispatch the task you want to do continuously. It does this aslong you are holding down the mouse button, when you let go, it stops adding jobs to do, and any jobs queued will fail because of a check before it actually does any work
bool isGoing = false;
private void MouseMouseDown(object sender, MouseButtonEventArgs e)
{
isGoing = true;
new Thread(new ThreadStart(() => { while (isGoing) { Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() => { if (isGoing)
{
//Whatever you want to happen continuously ie. move your image
}}));}})).Start();
}
private void MouseUp(object sender, MouseButtonEventArgs e)
{
isGoing = false;
}
This will get rid of your error, and be continuous.
Note:
I did this with mouse presses, but it will work exactly the same with keypresses
The speed of your image moving will depend on how good the computer is, I recommend either, using a timer, or even better, use delta time calculations when moving the image. that way you can move the image X/per second, but keep the position updating every possible chance, which results in the smoothest and most consistent motion.