I’m using the following code in my WPF Project :
Task.Factory.StartNew(
() =>
{
if (Keyboard.Modifiers == ModifierKeys.Alt) // <- Here
Thread.Sleep(1000);
})
.ContinueWith(t =>
{
// do somthing
});
and i’ll get the InvalidOperationException on :
if (Keyboard.Modifiers == ModifierKeys.Alt)
and it tells me :
The calling thread must be STA, because many UI components require
this.
I just don’t get it !! It seems to be a simple code, but why ?
You need to go onto the UI thread to get access to the Keyboard. Try this: