获取数据,剪贴板一个BackgroundWorker内 [英] Getting data off the Clipboard inside a BackgroundWorker

查看:182
本文介绍了获取数据,剪贴板一个BackgroundWorker内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台工作,并在DoWork的方法,我有以下几点:

I have a background worker and in the DoWork method I have the following:

 var clipboardData = Application.Current.Dispatcher.Invoke(new Action(() => { Clipboard.GetData(DataFormats.Serializable); }));



为什么这个总是返回null,即使我知道有数据剪贴板上以正确的格式?

Why does this always return null even though I know there is data on the clipboard in the correct format?

推荐答案

尝试把呼叫到一个STA线程:

Try putting the call into an STA thread:

object data = null;
Thread t = new Thread(() =>
{
    data = Clipboard.GetData(DataFormats.Serializable);
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
// 'data' should be set here.

在与OnFinished操作的方法:

Within a method with an "OnFinished" action:

void GetClipboardData(Action<Object> OnFinished)
{
    Thread t = new Thread(() =>
    {
        object data = Clipboard.GetData(DataFormats.Serializable);
        OnFinished(data);
    });
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
}

您会使用这样的:

GetClipboardData((data) =>
{
    // 'data' is set to the clipboard data here.
});

如果你想显示和隐藏的窗口,试试这个:

If you want to show and hide a window, try this:

myWindow.Show();
GetClipboardData((data) =>
{
    // Do something with 'data'.
    myWindow.Close();
});

通过的ShowDialog()

Thread d = new Thread(() =>
{
    myWindow.ShowDialog();
});
d.SetApartmentState(ApartmentState.STA);
d.Start();
GetClipboardData((data) =>
{
    // 'data' is set to the clipboard data here.
   myWindow.Close();
});

这篇关于获取数据,剪贴板一个BackgroundWorker内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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