简体   繁体   English

在FileSystemWatcher的重命名事件中无法访问剪贴板

[英]Unable to access clipboard in FileSystemWatcher's renamed event

I have an object of FileSystemWatcher called 'watcher' instantiated in my main function. 我在主函数中实例化了一个名为'watcher'的FileSystemWatcher对象。 I tried to store the text on clipboard in a string variable during the 'watcher.renamed' event but it always returns empty data? 我在'watcher.renamed'事件期间尝试将文本存储在剪贴板中的字符串变量中,但它始终返回空数据? I checked the value of the variable with the help of a breakpoint, it remains empty. 我借助断点检查了变量的值,该值保持为空。

Here is the code: 这是代码:

private void Form1_Load(object sender, EventArgs e)
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = Application.StartupPath;
        watcher.Filter = Path.GetFileName(Application.StartupPath+ @"\RBsTurn.txt");
        watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
        watcher.EnableRaisingEvents = true;
    }

    void watcher_Renamed(object sender, RenamedEventArgs e)
    {
        string x = Clipboard.GetText();
        MessageBox.Show(x);
    }

This code, always displays an empty text box when the file is renamed.. Please help. 重命名文件后,此代码始终显示一个空文本框。

Clipboard access methods must be initiated from an STA thread in order to function properly. Clipboard访问方法必须从STA线程启动才能正常运行。 Unfortunately, the FileSystemWatcher runs its callbacks on threadpool threads, all of which are part of the MTA. 不幸的是, FileSystemWatcher在线程池线程上运行其回调,所有这些都是MTA的一部分。 As such, trying to access the clipboard isn't going to work in your example. 因此,在您的示例中尝试访问剪贴板将不起作用。

If you need to perform some UI work when your event handler is run, then you'll need to notify the form (or some other piece of the UI) about that. 如果在运行事件处理程序时需要执行一些UI工作,那么您需要将其通知表单(或UI的其他部分)。 You can use the Form object's BeginInvoke() method to post a method to run on the UI thread: 您可以使用Form对象的BeginInvoke()方法来发布要在UI线程上运行的方法:

void watcher_Renamed(object sender, RenamedEventArgs e)
{
    this.BeginInvoke(new Action(() => {
        string x = Clipboard.GetText();
        MessageBox.Show(x);
    }));
}

The trick was to create a new thread in the event handler and set its STA properties 诀窍是在事件处理程序中创建一个新线程并设置其STA属性

Here is the code 这是代码

 private void Form1_Load(object sender, EventArgs e)
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = Application.StartupPath;
        watcher.Filter = Path.GetFileName(Application.StartupPath+ @"\RBsTurn.txt");
        watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
        watcher.EnableRaisingEvents = true;
    }

    void watcher_Renamed(object sender, RenamedEventArgs e)
    {
        Thread th = new Thread(() =>
        {
            Clipboard.Clear();
        });

        th.IsBackground = true;
        th.SetApartmentState(ApartmentState.STA);
        th.Start();
     }

Hope it helps :) 希望能帮助到你 :)

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

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