Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF中的后台工作人员不工作_C#_Wpf_Webbrowser Control_Backgroundworker - Fatal编程技术网

C# WPF中的后台工作人员不工作

C# WPF中的后台工作人员不工作,c#,wpf,webbrowser-control,backgroundworker,C#,Wpf,Webbrowser Control,Backgroundworker,我有一个导航网页的按钮。我试图将导航放在backgroundworker中,但它无法导航。如果我把它放在按钮的点击事件中,它可以正常工作。我在bgw_DoWork中放了一个断点,它表明它到达了创建新wb对象的点,但是它跳过了DoWork中的其余语句。我想这样做,以便在加载图像试图从网页获取数据时显示它。webbrowser对象不是UI的一部分,因为用户不需要查看刮片。它只是将信息下载到一个对象中,然后稍后显示该对象(在BackgroundWorker之外) 使用系统; 使用System.Coll

我有一个导航网页的按钮。我试图将导航放在backgroundworker中,但它无法导航。如果我把它放在按钮的点击事件中,它可以正常工作。我在bgw_DoWork中放了一个断点,它表明它到达了创建新wb对象的点,但是它跳过了DoWork中的其余语句。我想这样做,以便在加载图像试图从网页获取数据时显示它。webbrowser对象不是UI的一部分,因为用户不需要查看刮片。它只是将信息下载到一个对象中,然后稍后显示该对象(在BackgroundWorker之外)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统线程;
使用System.Windows;
使用System.Windows.Threading;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
运用系统反思;
使用系统组件模型;
使用mshtml;
命名空间WPF1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
韦伯浏览器wb;
private readonly BackgroundWorker bgw=new BackgroundWorker();
公共整数i=0;
公共主窗口()
{
初始化组件();
bgw.DoWork+=bgw_DoWork;
bgw.RunWorkerCompleted+=bgw_RunWorkerCompleted;
}
私有void GetSICData_单击(对象发送方,路由目标)
{
//mainTabControl.SelectedIndex=1;
//wb.Navigated+=wb_Navigated;
//wb.LoadCompleted+=wb_LoadCompleted;
//wb.导航(“http://www.google.com");
bgw.RunWorkerAsync();
}
私有void bgw_DoWork(对象发送方,DoWorkEventArgs e)
{
wb=新的WebBrowser();
wb.Navigated+=wb_Navigated;
wb.LoadCompleted+=wb_LoadCompleted;
wb.导航(“http://www.google.com");
}
私有void bgw_RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
MessageBox.Show(“后台完成”);
}
无效wb_加载已完成(对象发送方,导航目标e)
{
HTMLDocument doc=(HTMLDocument)wb.Document;
doc.getElementById(“ctl03_TextBox1”).innerText=“2334882”;
wb.LoadCompleted-=wb_LoadCompleted;
wb.LoadCompleted+=wb_LoadCompleted_2;
doc.getElementById(“ImageButton1”)。单击();
}
无效wb_加载完成_2(对象发送方,导航目标e)
{
MessageBox.Show(“它成功了!”);
}
已导航的无效wb_(对象发送方,导航目标)
{
}
}
}

某些UI操作需要在UI线程上调用。试一试

    private void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        wb.Navigated += wb_Navigated;
        wb.LoadCompleted += wb_LoadCompleted;

        wb.Dispatcher.Invoke(() =>
        {

            wb.Navigate("http://www.google.com");
        });
    }

您不能在后台线程上创建UI控件,在这种情况下,您不需要创建UI控件,因为您需要的所有
WebBrowser
方法都是异步执行的

以指定Uri异步导航到文档

顺便说一句,在加载
WebBrowser
控件后,不要忘记使用
Navigate
方法,因此可以使用
IsLoaded
检查或调用Navigate from
loaded
事件…

听起来您真正想要的是。您可以使用多种方法将整个网页作为文件或
字符串下载。最好的一点是,您还可以异步执行此操作,因此它将自动在后台线程上执行

使用这些异步方法时,需要处理相关的
Download…Completed
事件以检索结果。从MSDN上的页面:

public static void DownloadStringInBackground2 (string address)
{
    WebClient client = new WebClient ();
    Uri uri = new Uri(address);

    // Specify that the DownloadStringCallback2 method gets called 
    // when the download completes.
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(
DownloadStringCallback2);
    client.DownloadStringAsync (uri);
}

private static void DownloadStringCallback2 (Object sender, 
DownloadStringCompletedEventArgs e)
{
    // If the request was not canceled and did not throw 
    // an exception, display the resource. 
    if (!e.Cancelled && e.Error == null)
    {
        string textString = (string)e.Result;

        Console.WriteLine (textString);
    }
}

查看MSDN网站上的和页面以了解更多信息。

您无法在后台线程中控制UI元素有没有办法创建不属于UI的webbrowser对象?@user3175176如果您不想将webbrowser显示为UI的一部分,您可能不应该使用它,相反,你可能应该完全使用其他类来下载网站的内容而不显示它们。我会调查的。我只是想,既然WebBrowser是内置的,那么它将是最容易使用的。你对其他类的使用有什么建议吗?@EricJ.,虽然这与你最初的“可能重复”帖子非常相似,但问题作者的问题和评论似乎表明,他们实际上只是想在后台下载一个网页。所以这个问题,虽然措辞有点拙劣,是关于一个不同的主题。也许问题作者可以编辑使其更清晰,你可以删除你的投票结果?如果你只调用其中的UI线程,那么首先使用BGW是没有意义的。是的,你是对的。我只是想说明一点,但应该更多地关注示例代码。无论如何,WebClient听起来是个更好的主意。
public static void DownloadStringInBackground2 (string address)
{
    WebClient client = new WebClient ();
    Uri uri = new Uri(address);

    // Specify that the DownloadStringCallback2 method gets called 
    // when the download completes.
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(
DownloadStringCallback2);
    client.DownloadStringAsync (uri);
}

private static void DownloadStringCallback2 (Object sender, 
DownloadStringCompletedEventArgs e)
{
    // If the request was not canceled and did not throw 
    // an exception, display the resource. 
    if (!e.Cancelled && e.Error == null)
    {
        string textString = (string)e.Result;

        Console.WriteLine (textString);
    }
}