Tag: dispatcher

为什么调度程序BeginInvoke失败,其中Control BeginInvoke在C#Windows窗体应用程序中成功?

我最初尝试使用Dispatcher类BeginInvoke方法在我的C#Windows窗体应用程序中的主UI线程上显示一个消息框。 当我使用该方法时,消息框没有出现 。 我在传递给BeginInvoke()的委托的主体内部设置了一个断点,它从未被命中。 我尝试使用Action委托和MethodInvoker委托。 两种情况都没有运气。 当我使用属于Form对象的BeginInvoke方法时,它工作正常 。 为什么Dispatch版本无声地失败(没有exception或错误消息)? 以下是两个不同的版本。 Dispatcher dispatcher = Dispatcher.CurrentDispatcher; // THIS FAILED. CONTEXT: Executing on worker thread. MethodInvoker theMethod = new MethodInvoker(delegate() { string msg = “Show this message on the main UI thread.”; MessageBox.Show(msg, “Message”); }); dispatcher.BeginInvoke(theMethod); this.BeginInvoke(theMethod); // ————————————————— // THIS WORKED. CONTEXT: Executing on worker thread. MethodInvoker […]

如何在Nunit中调用WPF Dispatcher?

我想测试一个呈现具有数据字段值的文本块的应用程序。 渲染完成后,我想得到实际宽度和实际高度。 一切正常。 当我尝试测试应用程序时,首先出现问题。 我无法从测试项目中调用调度程序。 以下是代码。 this.Loaded += (s, e) => { TextBlock textBlock1 = new TextBlock(); //// Text block value is assigned from data base field. textBlock1.Text = strValueFromDataBaseField; //// Setting the wrap behavior. textBlock1.TextWrapping = TextWrapping.WrapWithOverflow; //// Adding the text block to the layout canvas. this.layoutCanvas.Children.Add(textBlock1); this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => { //// After rendering […]

使用ObservableCollection未正确更新ListView

我目前正在使用一个可观察的集合来存储我的ListView数据对象。 将新对象添加到集合中工作正常,listView正确更新。 但是,当我尝试更改集合中对象的某个属性时,listView将无法正确更新。 例如,我有一个可观察的集合DataCollection。 我试试 _DataCollections.ElementAt(count).Status = “Active”; 由于按下按钮,我在长时间操作之前执行此更改。 listView不会反映更改。 所以我添加myListView.Items.Refresh() 这是有效的,但是在button_click方法完成之前,listView不会刷新,这在那时是不行的。 例如: button1_Click(…) { _DataCollections.ElementAt(count).Status = “Active”; myListView.Items.Refresh(); ExecuteLongOperation(); _DataCollections.ElementAt(count).Status = “Finished”; myListView.Items.Refresh(); } 状态永远不会转到“活动”,它将在方法完成后直接进入“完成”。 我也尝试使用这样的调度程序: button1_Click(…) { this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, (NoArgDelegate)delegate { _DataCollection.ElementAt(count).Status = “Active”; myListView.Items.Refresh(); }); ExecuteLongOperation(); this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, (NoArgDelegate)delegate { _DataCollection.ElementAt(count).Status = “Finished”; myListView.Items.Refresh(); }); } 但是,这似乎也不正常。 任何提示或想法将不胜感激。

如何在WPF中执行操作之前放置延迟

我尝试使用以下代码在导航到下一个窗口之前延迟2秒。 但线程首先调用,文本块显示一微秒,然后进入下一页。 我听说调度员会这样做。 这是我的片段: tbkLabel.Text = “two mins delay”; Thread.Sleep(2000); Page2 _page2 = new Page2(); _page2.Show();

可移植类库,相当于Dispatcher.Invoke或Dispatcher.RunAsync

在.NET,Windows 8和Windows Phone 7中,我的代码与此类似: public static void InvokeIfRequired(this Dispatcher dispatcher, Action action) { if (dispatcher.CheckAccess()) { action(); } else { dispatcher.Invoke(action); } } 我如何在便携式类库中做一些事情? 有一个平台无关的实现这将是很好的。 我的想法是使用WP7中没有的TPL,但肯定会很快。 // PortableDispatcher must be created on the UI thread and then made accessible // maybe as a property in my ViewModel base class. public class PortableDispatcher { private TaskScheduler […]

Dispatcher Invoke(…)vs BeginInvoke(…)混淆

我很困惑为什么我不能让这个测试计数器应用程序使用Count()方法在我的Dispatcher上使用“BeginInvoke”的2个(或更多个)同时运行的反文本框。 您可以通过Invoke替换BeginInvoke来解决问题。 但这并不能解决我的困惑。 这是我正在谈论的示例代码: public class CounterTextBox : TextBox { private int _number; public void Start() { (new Action(Count)).BeginInvoke(null, null); } private void Count() { while (true) { if (_number++ > 10000) _number = 0; this.Dispatcher.BeginInvoke(new Action(UpdateText), System.Windows.Threading.DispatcherPriority.Background, null); } } private void UpdateText() { this.Text = “” + _number; } }