In IronRuby I can simply change properties of all controls. But as soon as I try to change them from within a other thread, it doesn't work. I also tried to call invoke
from the control's dispatcher
property, but this didn't work, too. So my question is: Is it possible to execute code on the main thread (or ui thread) from within the second thread? And if so, how?
Here is a little sample code. The first button works, becuase it doesn't use a thread, but the two other buttons don't work...
require "WindowsBase"
require "PresentationFramework"
require "PresentationCore"
require "System.Core"
require "System.Windows.Forms"
include System
include System::Windows
include System::Windows::Controls
include System::Windows::Controls::Primitives
include System::Windows::Documents
wnd = Window.new
wnd.size_to_content = SizeToContent.WidthAndHeight
wnd.height = 1
wnd.width = 1
grid = UniformGrid.new
grid.columns = 1
grid.rows = 3
wnd.content = grid
bnNoThread = Button.new
bnNoThread.click {|sender, e| bnNoThread.content = "It worked"}
bnNoThread.content = "Try it without thread"
bnNoThread.margin = Thickness.new(10)
bnNoThread.padding = Thickness.new(5)
grid.children.add(bnNoThread)
bnThreadWithoutDispatcher = Button.new
bnThreadWithoutDispatcher.click {|sender, e| Thread.new {bnThreadWithoutDispatcher.content = "It worked?"}}
bnThreadWithoutDispatcher.content = "With thread and without dispatcher"
bnThreadWithoutDispatcher.margin = Thickness.new(10)
bnThreadWithoutDispatcher.padding = Thickness.new(5)
grid.children.add(bnThreadWithoutDispatcher)
bnThreadWithDispatcher = Button.new
bnThreadWithDispatcher.click {|sender, e| Thread.new {bnThreadWithDispatcher.dispatcher.invoke {bnThreadWithDispatcher.content = "It worked?"}}}
bnThreadWithDispatcher.content = "With thread and without dispatcher"
bnThreadWithDispatcher.margin = Thickness.new(10)
bnThreadWithDispatcher.padding = Thickness.new(5)
grid.children.add(bnThreadWithDispatcher)
wnd.show_dialog