0

I have a class, that is responsible for the logic of a classic snake game logic, that uses System.Timers.Timer. Because the exact same class is used for a Windows Forms version of the exact same project, I don't want to change that.

How can I connect a WPF window's method to an event that was designed to be used for WinForms (ISynchronizeInvoke)? I'm searching quite a while to achieve this without rewrite my logic class.

4
  • 1
    It is pretty unclear how you used ISynchronizeInvoke. But you could just write a little helper class that implements it and uses Dispatcher to implement the four methods. It uses the same names, except that InvokeRequired is Dispatcher.CheckAccess(). Commented Dec 9, 2012 at 20:50
  • this was a missing link for me but may not be enough. I may need to open another question, or refine this one... Commented Dec 10, 2012 at 11:37
  • How do I implement the IAsyncResult interface for this use? Commented Dec 10, 2012 at 11:45
  • Helper class provided here - stackoverflow.com/a/50175680/353147 Commented Nov 7, 2021 at 0:57

1 Answer 1

2

You can use the Dispatcher to invoke to the UI thread

private void ThreadingTimerTick(object state)// or whatever your method is.
{
   Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
   {
     // do stuff
   });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.