本文整理汇总了C#中System.Windows.Threading.DispatcherFrame类的典型用法代码示例。如果您正苦于以下问题:C# DispatcherFrame类的具体用法?C# DispatcherFrame怎么用?C# DispatcherFrame使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DispatcherFrame类属于System.Windows.Threading命名空间,在下文中一共展示了DispatcherFrame类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
Console.CancelKeyPress +=
(sender, e) =>
{
e.Cancel = true;
cts.Cancel();
};
// Since Console apps do not have a SyncronizationContext, we're leveraging the built-in support
// in WPF to pump the messages via the Dispatcher.
// See the following for additional details:
// http://blogs.msdn.com/b/pfxteam/archive/2012/01/21/10259307.aspx
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/pull/1362
SynchronizationContext previousContext = SynchronizationContext.Current;
try
{
var context = new DispatcherSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(context);
DispatcherFrame dispatcherFrame = new DispatcherFrame();
Task mainTask = MainAsync(args, cts.Token);
mainTask.ContinueWith(task => dispatcherFrame.Continue = false);
Dispatcher.PushFrame(dispatcherFrame);
mainTask.GetAwaiter().GetResult();
}
finally
{
SynchronizationContext.SetSynchronizationContext(previousContext);
}
}
示例2: DoEvents
/// <summary>
/// Processes all UI messages currently in the message queue.
/// </summary>
public static void DoEvents()
{
// Create new nested message pump.
DispatcherFrame nestedFrame = new DispatcherFrame();
// Dispatch a callback to the current message queue, when getting called,
// this callback will end the nested message loop.
// note that the priority of this callback should be lower than the that of UI event messages.
DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background, exitFrameCallback, nestedFrame);
// pump the nested message loop, the nested message loop will
// immediately process the messages left inside the message queue.
Dispatcher.PushFrame(nestedFrame);
// If the "exitFrame" callback doesn't get finished, Abort it.
if (exitOperation.Status != DispatcherOperationStatus.Completed)
{
exitOperation.Abort();
}
}
示例3: DoEvents
public static void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
示例4: CountUploadedFiles
public void CountUploadedFiles(int counter, bool result, string fileName)
{
#region Needed for counter
var dispatcherFrame = new DispatcherFrame(true);
Dispatcher.CurrentDispatcher.BeginInvoke
(
DispatcherPriority.Background,
(SendOrPostCallback)delegate(object arg)
{
var f = arg as DispatcherFrame;
if (f != null) f.Continue = false;
},
dispatcherFrame
);
Dispatcher.PushFrame(dispatcherFrame);
#endregion
if (result)
{
textBlockCounter.Text = counter.ToString(CultureInfo.InvariantCulture) + " / " + _theImages.Count;
if (fileName != "")
labelCopyingStatus.Content = "Copying " + fileName;
else
labelCopyingStatus.Content = "";
}
else
textBlockCounter.Text = counter.ToString(CultureInfo.InvariantCulture);
}
示例5: Fail
public override void Fail(string message, string detailMessage)
{
base.Fail(message, detailMessage); // let base class write the assert to the debug console
string stackTrace = "";
try {
stackTrace = new StackTrace(true).ToString();
} catch {}
lock (ignoredStacks) {
if (ignoredStacks.Contains(stackTrace))
return;
}
if (!dialogIsOpen.Set())
return;
if (!SD.MainThread.InvokeRequired) {
// Use a dispatcher frame that immediately exits after it is pushed
// to detect whether dispatcher processing is suspended.
DispatcherFrame frame = new DispatcherFrame();
frame.Continue = false;
try {
Dispatcher.PushFrame(frame);
} catch (InvalidOperationException) {
// Dispatcher processing is suspended.
// We currently can't show dialogs on the UI thread; so use a new thread instead.
new Thread(() => ShowAssertionDialog(message, detailMessage, stackTrace, false)).Start();
return;
}
}
ShowAssertionDialog(message, detailMessage, stackTrace, true);
}
示例6: ExecuteWithDispatcher
public static void ExecuteWithDispatcher(Action<Dispatcher, Action> testBodyDelegate,
int timeoutMilliseconds = 20000, string timeoutMessage = "Test did not complete in the spefied timeout")
{
var uiThreadDispatcher = Dispatcher.CurrentDispatcher;
//ThreadingHelpers.UISynchronizationContext = new DispatcherSynchronizationContext(uiThreadDispatcher);
var frame = new DispatcherFrame();
// Set-up timer that will call Fail if the test is not completed in specified timeout
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
if (Debugger.IsAttached)
{
timeout = TimeSpan.FromDays(1);
}
Observable.Timer(timeout)
.Subscribe(
_ => { uiThreadDispatcher.BeginInvoke(new Action(() => { Assert.True(false, timeoutMessage); })); });
// Shedule the test body with current dispatcher (UI thread)
uiThreadDispatcher.BeginInvoke(
new Action(() => { testBodyDelegate(uiThreadDispatcher, () => { frame.Continue = false; }); }));
// Run the dispatcher loop that will execute the above logic
Dispatcher.PushFrame(frame);
}
示例7: DispatchFrame
public static void DispatchFrame(DispatcherPriority priority = DispatcherPriority.Background)
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(priority,
new DispatcherOperationCallback((f)=>((DispatcherFrame)f).Continue = false), frame);
Dispatcher.PushFrame(frame);
}
示例8: DoEvents
/// <summary>現在メッセージ待ち行列の中にある全UIメッセージを処理する</summary>
private void DoEvents()
{
// 新しくネスト化されたメッセージ ポンプを作成
DispatcherFrame frame = new DispatcherFrame();
// DispatcherFrame (= 実行ループ) を終了させるコールバック
DispatcherOperationCallback exitFrameCallback = (f) =>
{
// ネスト化されたメッセージ ループを抜ける
((DispatcherFrame)f).Continue = false;
return null;
};
// 非同期で実行する
// 優先度を Background にしているので、このコールバックは
// ほかに処理するメッセージがなくなったら実行される
DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background, exitFrameCallback, frame);
// 実行ループを開始する
Dispatcher.PushFrame(frame);
// コールバックが終了していない場合は中断
if (exitOperation.Status != DispatcherOperationStatus.Completed)
{
exitOperation.Abort();
}
}
示例9: Subscription_push_can_be_dispatched_on_designated_thread_blocking_scenario
public void Subscription_push_can_be_dispatched_on_designated_thread_blocking_scenario()
{
var threadId = -2;
var threadIdFromTest = -1;
IBus bus = null;
var resetEvent = new ManualResetEvent(false);
var uiThread = new Thread(
() =>
{
Helpers.CreateDispatchContext();
var frame = new DispatcherFrame();
threadId = Thread.CurrentThread.ManagedThreadId;
bus = BusSetup.StartWith<RichClientFrontend>().Construct();
bus.Subscribe<MessageB>(
msg =>
{
threadIdFromTest = Thread.CurrentThread.ManagedThreadId;
frame.Continue = false;
},
c => c.DispatchOnUiThread());
resetEvent.Set();
Dispatcher.PushFrame(frame);
});
uiThread.Start();
resetEvent.WaitOne();
bus.Publish(new MessageB());
uiThread.Join();
threadIdFromTest.ShouldBeEqualTo(threadId);
}
示例10: Open
public void Open(FrameworkElement container)
{
if (container != null)
{
_container = container;
// 通过禁用来模拟模态的对话框
_container.IsEnabled = false;
// 保持总在最上
this.Owner = GetOwnerWindow(container);
if (this.Owner != null)
{
this.Owner.Closing += new System.ComponentModel.CancelEventHandler(Owner_Closing);
}
// 通过监听容器的Loaded和Unloaded来显示/隐藏窗口
_container.Loaded += new RoutedEventHandler(Container_Loaded);
_container.Unloaded += new RoutedEventHandler(Container_Unloaded);
}
this.Show();
try
{
ComponentDispatcher.PushModal();
_dispatcherFrame = new DispatcherFrame(true);
Dispatcher.PushFrame(_dispatcherFrame);
}
finally
{
ComponentDispatcher.PopModal();
}
}
示例11: DoEvents
public static void DoEvents(DispatcherPriority priority = DispatcherPriority.Background) {
DispatcherFrame frame = new DispatcherFrame();
DXSplashScreen.SplashContainer.SplashScreen.Dispatcher.BeginInvoke(
priority,
new DispatcherOperationCallback(ExitFrame),
frame);
Dispatcher.PushFrame(frame);
}
示例12: DoEvents
public static void DoEvents(DispatcherPriority nPrio)
{
DispatcherFrame nestedFrame = new DispatcherFrame();
DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(nPrio, exitFrameCallback, nestedFrame);
Dispatcher.PushFrame(nestedFrame);
if (exitOperation.Status != DispatcherOperationStatus.Completed)
exitOperation.Abort();
}
示例13: ProcessMessages
/// <summary>
/// Waits until all pending messages up to the specified priority are processed.
/// </summary>
/// <param name="dispatcher">The dispatcher to wait on.</param>
/// <param name="priority">The priority up to which all messages should be processed.</param>
public static void ProcessMessages([NotNull] this Dispatcher dispatcher, DispatcherPriority priority)
{
Contract.Requires(dispatcher != null);
var frame = new DispatcherFrame();
dispatcher.BeginInvoke(priority, () => frame.Continue = false);
Dispatcher.PushFrame(frame);
}
示例14: WaitWithPumping
public static void WaitWithPumping(this Task task)
{
if (task == null) throw new ArgumentNullException("task");
var nestedFrame = new DispatcherFrame();
task.ContinueWith(_ => nestedFrame.Continue = false);
Dispatcher.PushFrame(nestedFrame);
task.Wait();
}
示例15: DoEvents
public void DoEvents() {
var disp = GetDispatcher();
if (disp != null) {
DispatcherFrame frame = new DispatcherFrame();
disp.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
}