public void Start(delegate_action action) { _action = action; _q = new WqlEventQuery("__InstanceOperationEvent", "TargetInstance ISA 'Win32_USBControllerDevice' "); _q.WithinInterval = TimeSpan.FromSeconds(1); _w = new ManagementEventWatcher(_q); _w.EventArrived += new EventArrivedEventHandler(onEventArrived); _w.Start(); }
static void ProcessDropDownItemsFont(ToolStripMenuItem menu, delegate_action action) { // 修改所有事项的字体,如果字体名不一样的话 foreach (ToolStripItem item in menu.DropDownItems) { action(item); if (item is ToolStripMenuItem) { ProcessDropDownItemsFont(item as ToolStripMenuItem, action); } } }
static void ProcessSplitContainer(SplitContainer container, delegate_action action) { action(container.Panel1); foreach (Control control in container.Panel1.Controls) { ProcessChildren(control, action); } action(container.Panel2); foreach (Control control in container.Panel2.Controls) { ProcessChildren(control, action); } }
static void ProcessToolStrip(ToolStrip tool, delegate_action action) { List <ToolStripItem> items = new List <ToolStripItem>(); foreach (ToolStripItem item in tool.Items) { items.Add(item); } foreach (ToolStripItem item in items) { action(item); if (item is ToolStripMenuItem) { ProcessDropDownItemsFont(item as ToolStripMenuItem, action); } } }
static void ProcessChildren(Control parent, delegate_action action) { // 修改所有下级控件的字体,如果字体名不一样的话 foreach (Control sub in parent.Controls) { action(sub); if (sub is ToolStrip) { ProcessToolStrip((ToolStrip)sub, action); } if (sub is SplitContainer) { ProcessSplitContainer(sub as SplitContainer, action); } // 递归 ProcessChildren(sub, action); } }
public static void ProcessControl(Control control, delegate_action action) { action(control); ProcessChildren(control, action); }
// TODO: 遇到 Server 连接失败情况,下次循环处理的等待时间要变长。也就是说只有 ListTags() API 成功情况下才能马上立即重新请求 // 启动后台任务。 // patameters: // new_action GetChannel() 遇到需要 new channel 时候调用的回调函数 // skip_action 循环中设置的一个检查点,若回调函数返回 true 则 continue // loop_action 循环中每一轮需要用 channel 做的事情 public void Start( delegate_action new_action, delegate_skip skip_func, delegate_action loop_action, CancellationToken token) { _new_action = new_action; Task.Run(async() => { TimeSpan wait_time = this.ShortWaitTime; while (token.IsCancellationRequested == false) { // TODO: 中间进行配置的时候,确保暂停在这个位置 // 延时 try { await Task.Delay(// TimeSpan.FromMilliseconds(1000), wait_time, token); } catch { return; } // Loop?.Invoke(this, new EventArgs()); if (skip_func == null) { if (string.IsNullOrEmpty(this.Url)) { SetError?.Invoke(null, new SetErrorEventArgs { Error = null }); continue; } } else { if (skip_func?.Invoke() == true) { continue; } } //if (this.State == "pause") // continue; if (loop_action == null) { continue; } bool error = false; this.Lock.EnterReadLock(); // 锁定范围以外,可以对通道进行 Clear() try { try { var channel = GetChannel(); try { loop_action(channel); wait_time = this.ShortWaitTime; } finally { ReturnChannel(channel); } } catch (Exception ex) { error = true; wait_time = this.LongWaitTime; SetError?.Invoke(ex, new SetErrorEventArgs { Error = $"{this.Name} 出现异常: {ExceptionUtil.GetAutoText(ex)}" }); } } finally { this.Lock.ExitReadLock(); } // 出过错以后就要清理通道集合 if (error) { this.Clear(); } } // App.CurrentApp.Speak("退出后台循环"); }); }