简体   繁体   English

c#的消息框数量限制

[英]c# Limit amount of messageboxes

Hello I've runned in to a problem again that I can't solve on my own. 您好,我又遇到了自己无法解决的问题。

I have a FileSystemWatcher named filOvervakare and uses this code to trigger a method. 我有一个名为filOvervakareFileSystemWatcher并使用此代码来触发方法。

filOvervakare.NotifyFilter = NotifyFilters.Size;
filOvervakare.NotifyFilter = NotifyFilters.LastWrite;

filOvervakare.Changed += new FileSystemEventHandler(filOvervakare_Changed);

This is the method: 这是方法:

void filOvervakare_Changed(object sender, FileSystemEventArgs e)
{
    if (MessageBox.Show("Vill du ladda upp filen " + e.Name + "?", "En fil har ändrats", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        //code code code           
    }
}

Everytime a file is changed it is supposed to show the messagebox once. 每次更改文件时,都应该显示一次消息框。 The problem is that instead of getting one messagebox it pops up like 5-6 of them and I have no idea how to fix this and I hope some of you might have a good solution. 问题是,它没有像出现5-6个消息框那样弹出一个消息框,而且我不知道如何解决此问题,我希望你们中的一些人可能有一个好的解决方案。 :) :)

Thanks! 谢谢!

// Morgan // 摩根

This is by design, the event handler is called on a different thread for each notification. 这是设计使然,事件处理程序在每个通知的不同线程上调用。 A quick fix is to set the FileSystemWatcher.SynchronizingObject property: 一个快速修复是设置FileSystemWatcher.SynchronizingObject属性:

    public Form1() {
        InitializeComponent();
        fileSystemWatcher1.SynchronizingObject = this;
    }

But that's not a really good idea, the FSW is liable to miss notifications because it is blocked, waiting for you to click the OK button. 但这不是一个好主意,FSW可能会丢失通知,因为它已被阻止,等待您单击“确定”按钮。 Displaying a message box in the notification event is just not a good idea, you want to process the notifications as quickly as possible. 在通知事件中显示消息框并不是一个好主意,您希望尽快处理通知。

You could use a boolean to tell you whether you have a message box open. 您可以使用布尔值来告诉您是否打开了消息框。

private bool messageBoxIsOpen;

void filOvervakare_Changed(object sender, FileSystemEventArgs e)
{
    if (this.messageBoxIsOpen)
    {
        return;
    }

    this.messageBoxIsOpen = true;
    if (MessageBox.Show(
        "Vill du ladda upp filen " + e.Name + "?", 
        "En fil har ändrats", 
        MessageBoxButtons.YesNo, 
        MessageBoxIcon.Question) == DialogResult.Yes)
    {
       //code code code           
    }

    this.messageBoxIsOpen = false;
}

There are a few events, something like LastAccess, LastWrite etc that the file system watcher fires events on. 有一些事件,例如LastAccess,LastWrite等,文件系统监视程序将在这些事件上触发事件。 You could check the event args for why the event was fired before displaying the message box or set the NotifyFilter property. 您可以在显示消息框之前检查事件args,以了解引发该事件的原因,或者设置NotifyFilter属性。

You can save the last changed file name in a dummy variable, and when the changed event is raised, don't show the messagebox unless the file name is different to the saved variable. 您可以将上次更改的文件名保存在虚拟变量中,并且在引发更改事件时,除非文件名与保存的变量不同,否则不要显示消息框。

string lastChangedFileName = "";
void filOvervakare_Changed(object sender, FileSystemEventArgs e)
    {

if(lastChangedFileName != e.Name)
{
        if (MessageBox.Show("Vill du ladda upp filen " + e.Name + "?", "En fil har ändrats", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
           //code code code           
        }
}
lastChangedFileName = e.Name;

    }

The easiest way to fix this is to declare a private bool, like so: 解决此问题的最简单方法是声明一个私有布尔值,如下所示:

private bool m_IsBoxShown;

In your constructor, set the value to false. 在构造函数中,将值设置为false。 Change your code above to read like this: void filOvervakare_Changed(object sender, FileSystemEventArgs e) 将上面的代码更改为如下所示:void filOvervakare_Changed(object sender,FileSystemEventArgs e)
{ {
if (m_IsBoxShown == false) { m_IsBoxShown=true; 如果(m_IsBoxShown ==假){m_IsBoxShown = true; if (MessageBox.Show("Vill du ladda upp filen " + e.Name + "?", "En fil har ändrats", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 如果(MessageBox.Show(“ Vill du ladda upp filen” + e.Name +“?”,“ En fil harändrats”,MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes)
{ {
m_IsBoxShown=false; m_IsBoxShown = false; //code code code //代码代码
} else { m_IsBoxShown=false; } else {m_IsBoxShown = false; } } } }}}

What I would recommend is having a short delay, say 10-100ms before showing the MessageBox. 我建议在显示MessageBox之前有一个短的延迟,例如10-100ms。 That way when a file changes a few times very quickly you only get one MessageBox. 这样,当文件快速更改几次后,您只会得到一个MessageBox。

In other words, when a notification comes in, start the timer. 换句话说,收到通知时,请启动计时器。 If the timer is already started, ignore the notification. 如果计时器已经启动,请忽略该通知。 When the timer triggers, stop the timer and show the MessageBox. 当计时器触发时,停止计时器并显示MessageBox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM