Maybe I don't understand something, but ...
There is a class (for example, MyClass
) that generates an event. For example, it is called EndedEvent
.
There is also a UserControl
in which an instance of the class Myclass
. UserControl
subscribes to the EndedEvent
event when the button is EndedEvent
. Below I will try to bring in the used pieces of code and describe the interaction.
Dispatcher Wrap:
public static class FastDispatcher { static public void CodeBlock<T>(T Target, Action _codeBlock) where T : System.Windows.Threading.DispatcherObject { Target.Dispatcher.Invoke(_codeBlock); } }
Control:
public partial class MyConrol : UserControl { MyClass myClass; public SocialGetter() { InitializeComponent(); myClass= new MyClass(); ; } private void button_Click(object sender, RoutedEventArgs e) { myClass.EndedEvent+= myClass_EndedEvent; myClass.Start("строка"); } private void myClass_EndedEvent() { FastDispatcher.CodeBlock(this, () => { textBox1.Text = "OK"; myClass.EndedEvent -= myClass_EndedEvent; }); } }
Class:
public class MyClass { public delegate EndedEventDel(); public EndedEventDel EndedEvent; void Start(string str) { ActionDF.EndedEvent += ADF_EndedEvent; ActionDF.Add(str); } private void ADF_EndedEvent() { if (EndedEvent!= null) EndedEvent(this); ActionDF.EndedEvent -= ADF_EndedEvent; } }
Dataflow queue:
public static class ActionDF { public delegate EndedEventDel(); public EndedEventDel EndedEvent; public TransformBlock<string,string> actionblock //допустим void Start(string str) { actionblock = new TransfonmBlock<string,string>(n => { Threed.Sleep(2000); if(EndedEvent!=null) EndedEvent(); return ""; }); } public void Add(string str) { actionblock.Post(str); } }
Then I put on the form 2 of my MyControl
and click on each button. As a result, the OK message is displayed only on the MyControl
in which I pressed the button first. If you make a reply from the event in the control and put it in the event of the button before the subscription, then everything works correctly.
Where can there be a mistake?
Dispatcher.BeginInvoke(new Action(delegate (){ _codeBlock();})).Wait();
if you can justDispatcher.Invoke(_codeBlock)
? - VladD