My Plan

  • Have a system that holds some objects
  • Have a winforms-form that displays some key values of these objects
  • When certain changes occur(object gets add / removed / datat change): Update GUI

My Idea

  • subscribe my form to events in the objects
  • update form when objects chamge (/raise events)

My Troubles

  • winforms (for good reasons) does not allow for changes coming from foreign threads
  • an eventHandler is in fact run in the thread of the object calling
  • hence .net throws an exception and I'm done.

Is there a way to manipulate a form through events not raised by itself?

Is there a way I can "nudge" my form to say "hey, better check your data, there's something new", without having to constantly poll?

  • 2
    You should be using Invoke/BeginInvoke on your winforms controls in order to update them from another thread. Example: stackoverflow.com/questions/10775367/… – Rotem May 19 '14 at 14:11
  • @Rotem, brilliant, never knew of this before. – Mark May 19 '14 at 14:13
  • Any reason your not using WPF? Lots of cool stuff with binding that will automatically do this for you... to some extent anyway. – Jay May 27 '14 at 4:24
  • @Jay none except for that I never used it a lot,but there's always time to learn new stuff – Mark May 27 '14 at 9:23
  • Your "alternatively" is the right solution. Start a task, and have it shoot the events up to the GUI where they will be processed. Overall, you want to separate all of the GUI (your consumer) code from the producer (the processing going on underneath producing the events). – Snoop Mar 12 '16 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.