草庐IT

libtest_dispatch

全部标签

c# - 使用 Dispatcher 时代码和表示的真正分离

在我的WPF中,尝试将我的类逻辑与任何接口(interface)相关数据分开,并且只提供用于绑定(bind)的ObservableCollection属性。问题是,当我从其他线程访问那些绑定(bind)的OC时,我需要通过调度程序进行访问。结果是,每当其中一种方法尝试更新OC时,我都需要添加隐藏在我的类中的许多Dispatcher.Invoke()调用。我怎样才能以更干净和分离的方式做到这一点,以便从我的方法中抽象出调度程序调用? 最佳答案 我没有Elixir。但是,如果您确定并准备好承担隐式UI委托(delegate)的责任,您始

c# - 等效于 Dispatcher.Invoke 或 Dispatcher.RunAsync 的可移植类库

在.NET、Windows8和WindowsPhone7中,我有类似这样的代码:publicstaticvoidInvokeIfRequired(thisDispatcherdispatcher,Actionaction){if(dispatcher.CheckAccess()){action();}else{dispatcher.Invoke(action);}}我将如何在可移植类库中做一些事情?最好有一个与平台无关的实现。我的想法是使用WP7中不可用但肯定会很快的TPL。//PortableDispatchermustbecreatedontheUIthreadandthenmad

c# - Dispatcher.Invoke with anonymous delegate 在 Silverlight 中有效但在 WPF 中无效

在Silverlight4中,我有一个自定义服务类,它有一个异步的Completed事件。在Completed事件中,我获取返回的数据并通过如下方式调用填充方法:privatevoidservice_Completed(objectsender,CompletedEventArgsargs){Dispatcher.BeginInvoke(()=>populateInbox(args.Jobs));}privatevoidpopulateInbox(Listjobs){inbox.DataContext=jobs;}BeginInvoke在SL4中工作,但是当我将它移植到WPF时,出现以

c# - 为什么每个 Dispatcher.BeginInvoke 回调都有一个唯一的同步上下文?

我刚刚注意到,在.NET4.5中,每个Dispatcher.BeginInvoke/InvokeAsync回调都在其自己非常独特的同步上下文(的实例)上执行DispatcherSynchronizationContext).这种变化背后的原因是什么?以下简单的WPF应用说明了这一点:usingSystem;usingSystem.Diagnostics;usingSystem.Threading;usingSystem.Windows;usingSystem.Windows.Threading;namespaceWpfApplication{publicpartialclassMain

c# - Dispatcher.BeginInvoke 问题

对于此代码,我收到“非静态字段、方法或属性‘System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)’需要对象引用”。privatevoidResponseCompleted(IAsyncResultresult){HttpWebRequestrequest=result.AsyncStateasHttpWebRequest;HttpWebResponseresponse=request.EndGetResponse(result)asHttpWebResponse;using(StreamReadersr=newSt

c# - WPF Dispatcher BeginInvoke 排序保证

如果我打电话Dispatcher.BeginInvoke(operation,DispatcherPriority.Loaded)来自20多个线程的1,000,000次,这1,000,000次操作是否保证按BeginInvoke调用的顺序由UI线程执行? 最佳答案 msdn说IfmultipleBeginInvokecallsaremadeatthesameDispatcherPriority,theywillbeexecutedintheorderthecallsweremade但是如果您从多个线程访问Dispatcher那么An

c# - Dispatcher.BeginInvoke ,尝试使用 lambda 从文本 block 获取字符串集,但出现转换错误

我试图从按钮调用选定的列表框项目,而不是wpf中的listbox.selecteditemchanged方法。所以当我尝试stringyadda=listbox.SelectedItem.ToString();我得到一个异常(exception):Thecallingthreadcannotaccessthisobjectbecauseadifferentthreadownsit.所以,我想做的是:Dispatcher.BeginInvoke(()=>{lbxSelectedItem=(lbxFileList.SelectedItemasTextBlock).Text;});这也不起作

c# - 正确使用(或不使用)Dispatcher.CheckAccess()

在Winforms中,所有控件都有一个InvokeRequired属性,如果我必须调用.[Begin]Invoke控件以修改它,它会返回true。在WPF中,DispatcherObject.CheckAccess()和Dispatcher.CheckAccess()中有一个明显相似的构造,但我被EditorBrowsable(EditorBrowsableState.Never)属性。当我禁用这样的编辑器浏览时,我用它来表示“你不应该不这样做。不,真的。如果这需要解决你眼前的问题,你错误地设计了你的解决方案首要问题。”另一方面,我发现的唯一替代方案(事实上,也是我最初的解决方案)是T

c# - Dispatcher线程和UI线程有什么区别

UI线程和Dispatcher线程在WPF中是一样的还是有什么区别? 最佳答案 ADispatcher负责管理线程的工作。UI线程是渲染UI的线程。TheUIthreadqueuesworkitemsinsideanobjectcalledaDispatcher.TheDispatcherselectsworkitemsonaprioritybasisandrunseachonetocompletion.EveryUIthreadmusthaveatleastoneDispatcher,andeachDispatchercanexe

c# - 了解 WPF Dispatcher.BeginInvoke

我的印象是dispatcher会遵循优先级排队的操作并根据优先级执行操作或将操作添加到队列的顺序(如果优先级相同)直到我被告知WPFUI调度程序不是这种情况。有人告诉我,如果UI线程上的操作需要更长的持续时间,比如数据库读取UI调度程序简单地尝试执行队列中的下一组操作。我无法接受它,所以决定编写一个示例WPF应用程序,其中包含一个按钮和三个矩形,单击按钮时,矩形会填充不同的颜色。在代码隐藏中privatevoidOnFillColorsClick(objectsender,RoutedEventArgse){vardispatcher=Application.Current.MainW