0

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
Cubi73
  • 1,891
  • 3
  • 31
  • 52
  • what happened when you tried the dispatcher where you concluded that it didn't work? – Gayot Fow Apr 14 '14 at 01:37
  • Nothing. I tried to add an item to a ListBox using the dispatcher, but no new ListBoxItem appeared. I also tried to change the content of a Button using the dispatcher, but this also didn't work. There was no error message and no change con the controls. – Cubi73 Apr 14 '14 at 11:24
  • Did the underlying collection view have the item? I will need to see some code and the general context. – Gayot Fow Apr 14 '14 at 11:38
  • Yes. The underlying collection has the item, but it doesn't appear on the screen. But there's another problem: When I call join to wait for the thread to finish adding the item and I then try to retrieve the count of items, Ruby says: "VerifyAccess, The object is held by another thread." – Cubi73 Apr 14 '14 at 11:52
  • I do this all the time, but in c#, and I don't yet have the knowledge of Ruby to tell the intricacies. I could attempt a parallel implementation in c# if that's of any use and if nobody else comes along. – Gayot Fow Apr 14 '14 at 18:00
  • I also do this all the time in C# and it works, but I cannot figure out, why it isn't working in Ruby. – Cubi73 Apr 14 '14 at 18:58

0 Answers0