Page 2 of 2
Re: How to Write From one Thread to Another ?
Posted: Tue Aug 23, 2016 9:38 am
by JohnD|Dyalog
PGilbert wrote:Found it:
∇Dispatch[⎕]∇
[0] obj Dispatch action;ActionObj;delegate;⎕USING
[1]
[2] action←⎕OR ⎕FX'RunBackground'action
[3]
[4] ⎕USING←'System,System.dll' 'System.Windows.Threading,WindowsBase.dll'
[5]
[6] {}obj.Dispatcher.BeginInvoke(DispatcherPriority.Normal(⎕NEW Action action))
⎕USING ← 'System.Windows,WPF/PresentationFramework.dll'
win ← ⎕NEW Window
win Dispatch& 'win.Title←''hello world'''
win.Title
hello world
The problem that I see is that all actions will be put into the same function named 'RunBackground' and if you do a couple of rapid Dispatch one after each one the 'RunBackground' executed may not be the good one.
Is there a way to fix this ?
Hi Pierre,
I've tried the above code, but I don't see the problem that I think you are describing. Are you able to send a small repro showing "the 'RunBackground' executed may not be the good one."?
Thanks,
John Daintree.
Re: How to Write From one Thread to Another ?
Posted: Tue Aug 23, 2016 3:05 pm
by PGilbert
Hello John, the code by itself is working fine. It is just that the 'RunBackground' function that is created to run is not 'unique' and if another 'Dispatch' is fired when the first one is not finish to run than it may interfere with the good operation of the program.
My solution is to add to the name of 'RunBackground' a timestamp in characters like this: (⍕⎕TS)~' ' ,that will make each 'RunBackground' function 'unique'. Looks like also that a function can erase itself with ⎕EX on the last line, so it will erase itself when finished. The final result looks like this:
∇Dispatch[⎕]∇
[0] obj Dispatch action;⎕USING;dispatchName
[1]
[2] dispatchName←'RunBackground_',(⍕⎕TS)~' '
[3]
[4] action←⎕OR ⎕FX dispatchName action ('⎕EX ''',dispatchName,'''')
[5]
[6] ⎕USING←'System,System.dll' 'System.Windows.Threading,WindowsBase.dll'
[7]
[8] {}obj.Dispatcher.BeginInvoke(DispatcherPriority.Normal(⎕NEW Action action))
Is this the right way to do it or there is a better way ?
Thanks in advance.