模仿的MessageBox - 等待用户的选择 [英] Imitate MessageBox - waiting for user's choice

查看:90
本文介绍了模仿的MessageBox - 等待用户的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Windows Phone的合作主要是和我试图创建一个类似于MessageBox的东西 - 小窗口,apperas,并等待用户的选择(即调用窗口等待的线程)。我发现三种方式我怎么能achive这一目标:


第一个的 - TaskCompletionSource

在这种情况下,我的任务是这样的:

I'm working mostly with Windows Phone and I'm trying to create something similar to MessageBox - small window that apperas and waits for user's choice (the Thread that invoked window waits). I found three ways how I can achive this goal:

FIRST - TaskCompletionSource
In this case my Task looks like this:

TaskCompletionSource<bool> taskComplete = new TaskCompletionSource<bool>();
private async Task myTask1()
{
   window.Show(); // Show window
   await taskComplete.Task;
   //some job run after User's choice
   MessageBox.Show("Job finished");
}

和我窗口的关闭事件:

private void WindowClosedEvent1(object sender, EventArgs e)
{
   taskComplete.SetResult(true);
}

第二个的 - SemaphoreSlim

我的任务和事件:

SECOND - SemaphoreSlim
My Task and event:

private SemaphoreSlim mySemaphore = new SemaphoreSlim(0, 1);
private async Task myTask2()
{
    window.Show(); // Show window
    await mySemaphore.WaitAsync();
    //some job run after User's choice
    MessageBox.Show("Job finished");
}

private void WindowClosedEvent2(object sender, EventArgs e)
{
    mySemaphore.Release();
}

第三个​​的 - 的EventWaitHandle:

我的任务和事件:

THIRD - EventWaitHandle:
My Task and Event:

EventWaitHandle waitForUser = new EventWaitHandle(false, EventResetMode.AutoReset, "myEventName");
private async Task myTask3()
{
    window.Show(); // Show window
    await Task.Run(() => waitForUser.WaitOne());
    //some job run after User's choice
    MessageBox.Show("Job finished");
}

private void WindowClosedEvent3(object sender, EventArgs e)
{
    waitForUser.Set();
}

这三种方法的工作,但我不能决定使用哪一种。我主要认为1)或2),将是最好的选择。可以在任何这三种方法导致在某些情况下的麻烦?有没有人尝试过​​这样的事?

All three methods work, but I cannot decide which to use. I'm mostly thinking that 1) or 2) will be the best choice. Can any of those three methods cause troubles in some circumstances? Has anybody tried something like this?

推荐答案

变体#1 TaskCompletionSource 就够了这一点,这就是 TaskCompletionSource 是。 #2,#3是不必要的复杂,尤其是#3 Task.Run(..),这是浪费池线程阻塞等待。

The variant #1 with TaskCompletionSource is enough for this, that's what TaskCompletionSource is for. #2 and #3 are unnecessary complex, especially #3 with Task.Run(..), which is wasting a pool thread for a blocking wait.

由于对弹出在WP没有模式,三种方法可能有一个主要问题:重入。例如,如果 myTask2()是在从再次单击该按钮点击一个按钮,没有任何prevents用户,这将启动另一个名为 myTask2( )。您的应用程序的用户界面的工作流程应该考虑到这一点。这是讨论的<一个href=\"http://social.msdn.microsoft.com/Forums/windowsapps/en-US/e625607f-788f-45d0-b9b6-fc4dbe96ac64/modal-popup?forum=winappswithcsharp\"相对=nofollow>这里。

Because there is no modality for Popup in WP, all three approaches potentially have one major issue: re-entrancy. E.g., if myTask2() is called upon a button click, nothing prevents user from clicking that button again, which would start another myTask2(). The UI workflow of your app should account for this. This is discussed here.

这篇关于模仿的MessageBox - 等待用户的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆