本文介绍了弹出一个MessageBox在WPF与BackgroundWorker的主要应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个WPF应用程序,我使用的是BackgroundWorker的定期检查服务器上的条件。 。虽然这工作得很好,我想弹出一个消息框notifing用户如果检查期间出现故障



下面是我有:

 公共静态无效StartWorker()
{
=工作者新的BackgroundWorker();

worker.DoWork + = DoSomeWork;
worker.RunWorkerAsync();
}

私有静态无效DoSomeWork(对象发件人,DoWorkEventArgs E)
{
,而(!worker.CancellationPending)
{
螺纹。睡眠(5000);

VAR isOkay =校验条件();

如果
MessageBox.Show(我应该阻止主窗口)(isOkay!);
}
}



不过这消息框不会阻塞主窗口。我还可以点击我的WPF应用程序和改变任何东西我与周围的MessageBox的喜欢。



我要如何解决这个问题?谢谢,






编辑:



有关的参考,这是我落得这样做:

 公共静态无效StartWorker()
{
工人=新的BackgroundWorker( );

worker.DoWork + = DoSomeWork;
worker.ProgressChanged + = ShowWarning;
worker.RunWorkerAsync();
}

私有静态无效DoSomeWork(对象发件人,DoWorkEventArgs E)
{
,而(!worker.CancellationPending)
{
螺纹。睡眠(5000);

VAR isOkay =校验条件();

如果(!isOkay)
worker.ReportProgress(1);
}
}

私有静态无效ShowWarning(对象发件人,ProgressChangedEventArgs E)
{
MessageBox.Show(我阻止主窗口) ;
}


解决方案

呼叫 ReportProgress 并通过这个 MessageBox.Show


In a WPF app, I am using a BackgroundWorker to periodically check for a condition on the server. While that works fine, I want to pop a MessageBox notifing the users if something fails during the check.

Here's what I have:

public static void StartWorker()
{
    worker = new BackgroundWorker();

    worker.DoWork += DoSomeWork;
    worker.RunWorkerAsync();
}

private static void DoSomeWork(object sender, DoWorkEventArgs e)
{
    while (!worker.CancellationPending)
    {
        Thread.Sleep(5000);

        var isOkay = CheckCondition();

        if(!isOkay)
           MessageBox.Show("I should block the main window");                
    }   
}

But this MessageBox does not block the main window. I can still click on my WPF app and change anything I like with the MessageBox around.

How do I solve this? Thanks,


EDIT:

For reference, this is what I ended up doing:

public static void StartWorker()
{
    worker = new BackgroundWorker();

    worker.DoWork += DoSomeWork;
    worker.ProgressChanged += ShowWarning;
    worker.RunWorkerAsync();
}

private static void DoSomeWork(object sender, DoWorkEventArgs e)
{
    while (!worker.CancellationPending)
    {
        Thread.Sleep(5000);

        var isOkay = CheckCondition();

        if(!isOkay)
           worker.ReportProgress(1);                
    }   
}

private static void ShowWarning(object sender, ProgressChangedEventArgs e)
{
    MessageBox.Show("I block the main window");
}
解决方案

Call ReportProgress and pass this to MessageBox.Show.

这篇关于弹出一个MessageBox在WPF与BackgroundWorker的主要应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 08:48