c# 限制消息框数量

发布于 2024-08-23 08:47:31 字数 735 浏览 3 评论 0原文

你好,我又遇到了一个我自己无法解决的问题。

我有一个名为 filOvervakare 的 FileSystemWatcher 并使用此代码来触发方法。

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

filOvervakare.Changed += new FileSystemEventHandler(filOvervakare_Changed);

这是方法:

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           
    }
}

每次更改文件时,都应该显示消息框一次。问题是,它不是弹出一个消息框,而是弹出 5-6 个消息框,我不知道如何解决这个问题,我希望你们中的一些人可能有一个好的解决方案。 :)

谢谢!

// 摩根

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.

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. :)

Thanks!

// Morgan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风追烟花雨 2024-08-30 08:47:31

这是设计使然,每个通知的事件处理程序都会在不同的线程上调用。快速修复方法是设置 FileSystemWatcher.SynchronizingObject 属性:

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

但这并不是一个好主意,FSW 很可能会错过通知,因为它被阻止,等待您单击“确定”按钮。在通知事件中显示消息框并不是一个好主意,您希望尽快处理通知。

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:

    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. Displaying a message box in the notification event is just not a good idea, you want to process the notifications as quickly as possible.

猥︴琐丶欲为 2024-08-30 08:47:31

您可以使用布尔值来告诉您是否打开了消息框。

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;
}

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;
}
骄兵必败 2024-08-30 08:47:31

文件系统观察程序会触发一些事件,例如 LastAccess、LastWrite 等。您可以在显示消息框之前检查事件参数以了解触发事件的原因或设置 NotifyFilter 属性。

There are a few events, something like LastAccess, LastWrite etc that the file system watcher fires events on. You could check the event args for why the event was fired before displaying the message box or set the NotifyFilter property.

西瑶 2024-08-30 08:47:31

您可以将最后更改的文件名保存在虚拟变量中,并且当引发更改事件时,不显示消息框,除非文件名与保存的变量不同。

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;

    }

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;

    }
但可醉心 2024-08-30 08:47:31

解决此问题的最简单方法是声明一个私有 bool,如下所示:

private bool m_IsBoxShown;

在构造函数中,将值设置为 false。将上面的代码更改为如下所示:
void filOvervakare_Changed(对象发送者,FileSystemEventArgs e)
{
如果(m_IsBoxShown == false)
{
m_IsBoxShown=true;
if (MessageBox.Show("Vill du ladda upp filen " + e.Name + "?", "En fil har ändrats", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
m_IsBoxShown=假;
//代码代码代码
} 其他 { m_IsBoxShown=false; }
}
}

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. Change your code above to read like this:
void filOvervakare_Changed(object sender, FileSystemEventArgs e)
{
if (m_IsBoxShown == false)
{
m_IsBoxShown=true;
if (MessageBox.Show("Vill du ladda upp filen " + e.Name + "?", "En fil har ändrats", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
m_IsBoxShown=false;
//code code code
} else { m_IsBoxShown=false; }
}
}

弥枳 2024-08-30 08:47:31

我建议在显示 MessageBox 之前有一个短暂的延迟,例如 10-100 毫秒。这样,当文件很快更改几次时,您只会得到一个 MessageBox。

换句话说,当通知到来时,启动计时器。如果计时器已启动,请忽略该通知。当计时器触发时,停止计时器并显示 MessageBox。

What I would recommend is having a short delay, say 10-100ms before showing the MessageBox. That way when a file changes a few times very quickly you only get one 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文