如何在单独的窗口中启动Web浏览器 [英] How to start Web Browser in a separate Window

查看:79
本文介绍了如何在单独的窗口中启动Web浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程概念还是新手。

有一个WIndows表单应用程序,它将xmlstring发送到webbrowser,而不是对它执行某些操作。

但是每个call可能有不同的数据,我需要让它为每次调用打开一个新的WebBrowser。

这就是我所拥有的,但它始终在同一个Webbrowser窗口中启动。



Still new to the concept of Threading.
Have a WIndows Form application which sends a xmlstring to the webbrowser which than performs some action on that.
But since every call may has different data, I would need to have it open a new WebBrowser on it's own for each call.
This is what I have, but it always starts in same Webbrowser window.

private void StartNewWindowThread(Prozessval p)
        {
            try
            {
                newWindowThread = new Thread(() =>
                {
                    WebBrowser wb = new WebBrowser();
                    String data = "myval=" + ControlHelper.SerializeXMLString(p);
                    System.Text.Encoding encod = System.Text.Encoding.UTF8;
                    byte[] bytVal = encod.GetBytes(data);
                    wb.Navigate("myurl", "_Blank", bytVal, "Content-Type: application/x-www-form-urlencoded");

                    Application.Run();
                });

                newWindowThread.SetApartmentState(ApartmentState.STA);
                newWindowThread.Start();
            }
            catch(Exception ex)
            {                
                throw ex;
            }
        }







感谢您的帮助。




Thanks for any help.

推荐答案

它与线程无关。所有.NET UI库都在一个线程中工作。更准确地说,您可以创建一个应用程序,该应用程序使用两个不同的线程和两个不同的UI实例(两个或更多),但为此,每个线程都应以 Application.Start 。然后你会得到两个不同的UI线程,几乎彼此独立;你可以通过 Invoke / BegingInvoke 在它们之间进行通信。这是一种非常奇特的方法;现在,让我们忘了它。



没有什么可以阻止你运行类 WebBrowser 的几个实例,除了一些比尔伍德拉夫在对这个问题的评论中提到的表现成本。你不需要多个线程。要了解原因,您需要了解面向事件的架构的基础知识。



-SA
It has nothing to do with threading. All .NET UI libraries work in one thread. More exactly, you can create an application which uses two different threads with two different instanced of UI (two or more), but to do so, each of those threads should start with Application.Start. Then you will get two different UI threads, pretty much independent from each other; you would be able to communicate between them via Invoke/BegingInvoke. This is a very exotic approach; for now, let's forget it.

Nothing prevents you from running several instanced of the class WebBrowser, except some performance cost Bill Woodruff mentioned in his comment to the question. You don't need several threading for that. To understand why, you need to understand the basics of event-oriented architecture.

—SA


这篇关于如何在单独的窗口中启动Web浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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