Tag: 调度程序

如何在C#中使用TaskScheduler设置“仅在登录时运行”和“运行为”?

我有使用C#TaskManager对象创建任务的代码。 在Windows 7上它工作正常但在Windows XP(可能是其他Windows)上根本不起作用,因为该任务的默认用户是系统,因此没有用于显示GUI的会话。 如果我在控制面板小部件中手动修改创建的任务,将作业设置为仅在用户登录时运行且仅针对特定用户,则一切正常。 但是,尽管经过了数小时的搜索,我看不到在C#对象中设置这些选项的选项。 有人知道现有对象的解决方案吗? 我不想重写所有内容来手动运行调度程序EXE并通过命令行传入内容。 Q

“必须在与DependencyObject相同的线程上创建DependencySource”时创建GridView

我有一个线程问题。当我想在另一个线程中将一个GridView设置为ListView作为View时。它显示一条消息,其中显示: 必须在与DependencyObject相同的Thread上创建DependencySource。 // Create grid view GridView grid = new GridView(); // Add column // Name grid.Columns.Add((GridViewColumn)myresourcedictionary[“gridDirFileName”]); // Type grid.Columns.Add((GridViewColumn)myresourcedictionary[“gridDirFileType”]); // Data Modified grid.Columns.Add((GridViewColumn)myresourcedictionary[“gridDirFileDataModified”]); // Size grid.Columns.Add((GridViewColumn)myresourcedictionary[“gridDirFileSize”]); // Edit view Application.Current.Dispatcher.Invoke(new Action(() => ListViewOp.View = grid)); 我在做什么?

WPF Dispatcher.Invoke’hanging’

我有一个有点复杂的WPF应用程序,当尝试使用调度程序在UI线程上调用调用时,似乎是“挂起”或卡在Wait调用中。 一般过程是: 处理按钮上的单击事件 创建一个新线程(STA),其中:创建演示者和UI的新实例,然后调用Disconnect方法 断开然后在名为Name的UI上设置属性 然后,Name的setter使用以下代码来设置属性: if(this.Dispatcher.Thread != Thread.CurrentThread) { this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate{ this.Name = value; // Call same setter, but on the UI thread }); return; } SetValue(nameProperty, value); // I have also tried a member variable and setting the textbox.text property directly. 我的问题是,当调用 dispatcher invoke方法时,它似乎每次都挂起,并且callstack指示它处于hibernate状态,等待或加入Invoke实现。 那么,有什么我做错了,我错过了,显而易见或没有,或者有更好的方法调用UI线程来设置这个属性(和其他)? 编辑:解决方案是在线程委托结束时调用System.Windows.Threading.Dispatcher.Run()(例如,正在执行工作的地方) – 感谢所有帮助过的人。

C#/ WPF从另一个类中创建的另一个线程更新UI

我来自一个嵌入式C背景,我正在研究我的第一个C#应用程序,我已经碰壁了,我的研究还没有完成,所以我想我会问这里。 简单的app,到目前为止。 我有一个MainWindow,在一堆其他东西中,在按钮点击时启动一个TCPClient线程: public partial class MainWindow : Window { …. TCPConnection myCon = new TCPConnection(); …. private void connectButton_Click(object sender, RoutedEventArgs e) { networkListBox.Items.Add(“Connecting…”); myCon.Connect(“localhost”, updateNetworkListBox); } } …. public void updateNetworkListBox(string message) { networkListBox.Items.Add(message); } 在TCPConnection.cs中: public class TCPConnection { …. public void Connect(string server, ReportDelegate reportDelegate) { this.server = server; clientThread = […]

Dispatcher.BeginInvoke在一种情况下没有被执行

我正在尝试修复现有应用程序中的错误,在单个案例中,调度事件未执行。 在我们的应用程序中,有几个服务实现了一个公共抽象类(AbstractService)。 当服务保存实体并且必须在UI线程中引发通知时,我遇到的问题就出现了。 AbstractService的相关部分: public abstract class AbstractService : IDataProvider, IAbstractService where TEntity : AbstractEntity, new() where TData : AbstractData where TInfo : IEntityInfo { […] protected virtual void NotifyEntityChanged(NotifyEventItem[] pNotifyEventItems) { } private void NotifyPersPlanChanged() { if (PersPlanChanged != null) PersPlanChanged(this, new NotifyEventArgs(null, PersistanceState.Reset)); Debug.WriteLine(string.Format(“{0} {1} Call {2} to NotifyPersPlanChanged”, DateTime.Now.ToLongTimeString(), GetType().Name, m_counterNotifyPersPlanChanged++)); } […]

将DispatcherTimer与Windows服务一起使用

为什么我的DispatcherTimer不能与Windows服务一起使用。 我想要使​​用DispatcherTimer检查Windows服务许可证背后的目的 public OIMService() { InitializeComponent(); _dispatcherTimer = new DispatcherTimer(); if (!System.Diagnostics.EventLog.SourceExists(“OIM_Log”)) { EventLog.CreateEventSource(“OIM_Log”, “OIMLog”); } EventLog.Source = “OIM_Log”; EventLog.Log = “OIMLog”; _helpers = new ValidationHelpers(); StartTimer(); } protected override void OnStart(string[] args) { System.Diagnostics.Debugger.Launch(); if (_helpers.IsValid()) { ConfigureServer(); StartTimer(); } else { this.Stop(); } } private void StartTimer() { const int time = 10; […]

关于在WPF中刷新UI的困惑

我听说这会刷新用户界面,但为什么会这样呢? Dispatcher调用此空操作,即它没有调用InvalidateMeasure() ,这将触发UI重新测量并重新排列并在操作中重新呈现。 这里的度量和安排流程在哪里update/refresh UI? private static Action EmptyDelegate = delegate() { }; public static void Refresh(UIElement uiElement) { uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate); } 有帮助吗? EDITED:我想知道为什么UI呈现的细节。 well that triggers ui update答案well that triggers ui update没有帮助我更多的人。

如何给Dispatcher.BeginInvoke提供回调函数

当函数以Dispatcher.BeginInvoke启动完成时,我需要使用回调函数来执行一些后处理任务。 但是我在Dispatcher.BeginInvoke中找不到任何参数来接受回调。 是否可以为Dispatcher.BeginInvoke提供回调函数?

我的异步任务总是阻止UI

在WPF 4.5应用程序中,我不明白为什么在使用await + a task时UI被阻止: private async void Button_Click(object sender, RoutedEventArgs e) { // Task.Delay works great //await Task.Delay(5000); double value = await JobAsync(25.0); MessageBox.Show(“finished : ” + value.ToString()); } private async Task JobAsync(double value) { for (int i = 0; i < 30000000; i++) value += Math.Log(Math.Sqrt(Math.Pow(value, 0.75))); return value; } await Task.Delay运行良好,但等待JobAsync阻止UI。 为什么? […]

了解WPF Dispatcher.BeginInvoke

我的印象是dispatcher将遵循排队的操作的优先级,并根据优先级或操作添加到队列的顺序(如果相同的优先级)执行操作,直到我被告知这不是WPF UI dispatcher 。 有人告诉我,如果UI线程上的操作持续时间较长,则表示数据库读取UI调度程序,则尝试执行队列中的下一组操作。 我无法接受它,所以决定编写一个示例WPF应用程序,其中包含一个按钮和三个矩形,点击按钮,矩形填充不同的颜色。 并在代码隐藏 private void OnFillColorsClick(object sender, RoutedEventArgs e) { var dispatcher = Application.Current.MainWindow.Dispatcher; dispatcher.BeginInvoke(new Action(() => { //dispatcher.BeginInvoke(new Action(SetBrushOneColor), (DispatcherPriority)4); //dispatcher.BeginInvoke(new Action(SetBrushTwoColor), (DispatcherPriority)5); //dispatcher.BeginInvoke(new Action(SetBrushThreeColor), (DispatcherPriority)6); dispatcher.BeginInvoke(new Action(SetBrushOneColor)); dispatcher.BeginInvoke(new Action(SetBrushTwoColor)); dispatcher.BeginInvoke(new Action(SetBrushThreeColor)); }), (DispatcherPriority)10); } private void SetBrushOneColor() { Thread.Sleep(10 * 1000); Order = “One”; //MessageBox.Show(“One”); BrushOne = Brushes.Red; } […]