Tag: 调度员

PresentationFramework.dll中出现未处理的“System.Windows.Markup.XamlParseException”类型exception

我正在使用C#/ WPF中的一个小应用程序,该应用程序由来自串行端口的数据提供。 它还会读取包含一些常量的文本文件以便计算某些内容。 事件处理程序在到达时处理传入的数据: _serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Receive); 这是Receive处理程序,以及在Dispatcher中创建的委托,以进一步更新UI。 private delegate void UpdateUiTextDelegate(string text); private void Receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { // collect characters received to our ‘buffer’ (string) try { // stops long running output timer if enabled if (dispatcherTimer.IsEnabled) { dispatcherTimer.Stop(); } message = _serialPort.ReadLine(); dispatcherTimer.Start(); Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(updateUI), message); } catch (Exception […]

UI在重度计算时冻结

我正在将大量文件加载到内存中,但在此计算中,我的应用程序已冻结。 知道我的代码有什么问题吗? public void Drop(DragEventArgs args) { BackgroundWorker worker = new BackgroundWorker(); string fileName = IsSingleTextFile(args); if (fileName == null) return; worker.DoWork += (o, ea) => { try { StreamReader fileToLoad = new StreamReader(fileName); string filecontent = fileToLoad.ReadToEnd(); fileToLoad.Close(); // providing the information to the UI thread Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => SfmLogFile = filecontent)); } […]

C#,WPF,需要时自动调用Dispatcher.Invoke?

我有一个嵌入了Geospace地图的程序。 地图的事件处理在单独的线程上处理,以使地图保持响应(例如,单击地图时触发的事件)。 我遇到的问题是当地图触发一个事件时,我的程序需要更新它的gui中的一些东西,并且还回调到地图以处理在地图上放置图片。 我尝试在this.Dispatcher.Invoke中包装整个事件处理程序方法,这使我回到主UI线程。 这对于更新我的GUI很有用,但是当我回调到地图时,我仍然在UI线程上,这可能会导致地图中出现一些问题。 基本上,为了使这项工作,每次我想要更改我的gui上的控件时,我将不得不运行dispatcher.invoke。 有没有办法我可以自动执行此操作而不在dispatcher.invoke中包装每个调用? 我希望这一切都有道理。 下面是我正在谈论的事件的一些示例代码.. private void Map_OnMapClicked(object sender, MapClickedEventArgs e) { this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { // Do something to update my gui })); Map.DoSomethingInTheMap(); this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { // Do something to update my gui })); //etc etc etc }

没有调度员的定义

我收到了错误 ‘socketServer.Form1’不包含’Dispatcher’的定义,并且没有扩展方法’Dispatcher’接受类型为’socketServer.Form1’的第一个参数’ 从 private void tbAux_SelectionChanged(object sender, EventArgs e) { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() { textBox.Text = tbAux.Text; } ); } 根据文档 , Dispatcher类是我正在使用的命名空间System.Windows.Threading一部分。 我错过了另一个参考? 如果它是相关的,我在使用服务器/客户端套接字收到“跨线程操作无效”的错误后添加了这个。

wp8的任务队列?

在wp8中对复杂任务进行排队的正确方法是什么? 任务包括以下内容: 通过更新模型变量显示ProgressIndicator 获取或存储数据到wcf服务( UploadStringAsync ) 使用UploadStringCompleted的结果更新潜在的数据绑定模型。 通过更新模型变量隐藏ProgressIndicator 目前我一直在使用一个拥有命令对象队列的类,运行一个在添加项目时启动的线程(如果它尚未运行)。 但是,我有等待代码停止运行的任务或子任务的问题。 以前我曾经使用异步等待,但是行为的几个级别变得越来越难以预测。 我想要的是能够创建和排队命令对象的主线程。 命令对象应该一次运行一个,而不是在前一个完全完成之前启动一个新对象。 如果需要,命令对象应该能够使用调度程序访问主线程。

如何正确使用Dispatcher.BeginInvoke?

我几乎到处搜索互联网,我搜索了很多次,发现了很多结果,但我仍然无法找到问题的解决方案。 我正忙着将旧的WinForms应用程序转换为新的WPF应用程序,但我遇到了一些命令的问题。 在Winforms应用程序中,他们使用Control.BeginInvoke()并将其存储在IAsyncResult对象中。 我已经读过Dispatcher.BeginInvoke()是与WinForms的Control.BeginInvoke()相当的WPF但我在使用时遇到此错误 Dispatcher.BeginInvoke():“无法将类型’System.Windows.Threading.DispatcherOperation’隐式转换为’System.IAsyncResult’。存在显式转换(您是否错过了转换?)”。 任何帮助将不胜感激。 这是我试图转换的代码。 这是最初的WinForms代码。 我可以转换除BeginInvoke部分之外的所有内容。 private eSkan.api.TeSkanAPI feSkanAPI = null; private void MessageFilter_AddRemove_Invoked(bool AddFilter, IMessageFilter Filter) { if (AddFilter){ Application.AddMessageFilter(Filter); } else { Application.RemoveMessageFilter(Filter); } } private void MessageFilter_AddRemove(bool AddFilter, IMessageFilter Filter) { { IAsyncResult sr = BeginInvoke((ESKAN_ADD_REMOVE_MESSAGEFILTER)MessageFilter_AddRemove_Invoked, AddFilter, Filter); sr.AsyncWaitHandle.WaitOne(2000); } } 下面是我到目前为止转换的代码,包括我正在努力的BeginInvoke部分。 private void MessageFilter_AddRemove_Invoked(bool addFilter, System.Windows.Forms.IMessageFilter filter) { […]

在线程中获取正确的WPF调度程序

在对象的构造函数中,我需要创建一个WPF mediaElement对象: m_videoMedia = new MediaElement(); 但是这个类也可以从其他线程实例化,所以我需要使用 Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { m_videoMedia = new MediaElement(); })); 但是如何在该构造函数中获得正确的调度程序实例:s

Dispatcher.BeginInvoke问题

我得到“此代码的非静态字段,方法或属性’System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)’”需要对象引用。 private void ResponseCompleted(IAsyncResult result) { HttpWebRequest request = result.AsyncState as HttpWebRequest; HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { Dispatcher.BeginInvoke( () => { try { XDocument resultsXml = XDocument.Load(sr); QueryCompleted(new QueryCompletedEventArgs(resultsXml)); } catch (XmlException e) { XDocument errorXml = new XDocument(new XElement(“error”, e.Message)); QueryCompleted(new QueryCompletedEventArgs(errorXml)); } }); } } […]

Dispatcher.CurrentDispatcher.BeginInvoke不调用

我有一个FileSystemWatcher ,当在UI线程的不同线程上引发监视文件更改时,由此引发的事件。 为了避免和交叉线程的声音乐趣,我试图使用 public void RaisePathChanged(object sender, RenamedEventArgs e) { Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { // Some code to handle the file state change here. })); } 这个编译得很好,并且应该触发RaisePathChanged 。 但是,委托Action(() => { /*Here*/ })的代码永远不会被调用/调用,代码只是被跳过。 为什么要跳过代码,我该如何修复它?这是确保代码在WPF中创建它的线程上运行的最佳方法吗? 谢谢你的时间。

使用WPF进行C#打印

我的应用程序打印(到打印机)屏幕上显示的信息(使用Canvas控件)N次。 这个过程是 用户单击按钮(称为“打印”)。 使用文本更新Canvas(通常来自数据库但是对于下面的代码,它是硬编码的) 打印到打印机 使用新文本更新canvas(再次从数据库更新,但对于下面的代码,它是硬编码的)打印到打印机 但是,我无法按照上述过程中的说明进行操作 – 打印机仅打印上次更新。 为了使这个问题可以复制,我附上下面的代码 我的XAML 和我的代码背后 using System; using System.Windows; using System.Printing; using System.Windows.Threading; using System.Windows.Controls; namespace WpfApplication4 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { PrintDialog dialog = new […]