【问题标题】:"while" loop in windows forms apllication - no window is opened (c#)Windows窗体应用程序中的“while”循环 - 没有打开窗口(c#)
【发布时间】:2021-04-30 02:38:17
【问题描述】:

我是 C# 新手,我想创建一个 Windows 窗体应用程序,它显示(它必须是可见的!)一个带有一些信息和按钮的窗口,它还从 Internet 加载一个页面(使用 selenium 和 phantom.js -虽然它已被弃用)每分钟。我写了这样的东西:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;

namespace WindowsFormsApplication1
{
    public partial class Someclass : Form
    {
        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void Someclass_Load(object sender, EventArgs e)
        {
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            // Shows some text "Hello friend" 
            MessageBox.Show("Hello friend!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello again", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public Someclass()
        {
            InitializeComponent();
       
            while (!IsDisposed)
            {
                MessageBox.Show("Now the page will be downloaded");
                var driverService = PhantomJSDriverService.CreateDefaultService();
                driverService.HideCommandPromptWindow = true; 
                using (var driver = new PhantomJSDriver(driverService))
                {
                    driver.Navigate().GoToUrl("http://stackoverflow.com/");
                    MessageBox.Show("Here we are going to open StackOverflow");
                    var questions = driver.FindElements(By.ClassName("fs-display2"));
                    foreach (var question in questions)
                    {
                        // This will display some text from stackoverflow main page.
                        Console.WriteLine(question.Text);
                        question.Click();
                        MessageBox.Show("This is stackoverflow: " + question.Text);
                    }
                    MessageBox.Show("Here we go");
            }
            System.Threading.Thread.Sleep(60000); // delay in microseconds
        }
    }
}

我的问题是,如果我使用“while”,我的窗口不会出现(但来自互联网的页面会正确加载 - 每 1 分钟一次),如果我使用“if”而不是“while”,我的窗口会显示得很好,但是,当然,页面加载只进行一次。什么可以解决我的问题?

【问题讨论】:

  • 该代码不能放在那里。当构造函数被调用时,表单仍然不存在。 InitializeCompoment 将开始创建包含在表单中的对象,但循环永远不会允许表单引擎完成将表单显示在屏幕上的工作。该代码可以作为按钮上的某些单击事件的事件处理程序插入。或放置在 BackgroundWorker DoWork 事件中或作为 Timer 对象的事件处理程序。
  • 您很可能会通过不断在 UI 线程上执行该工作来阻止想要显示您的窗口的线程。它与 If 一起使用的原因是,一旦您完成下载,它就可以返回绘制您的 UI。 While 循环占据 UI 线程,从不将其返回给绘制内容

标签: c# selenium while-loop phantomjs


【解决方案1】:

这是一个非常简单的解决方案:您的 while 循环阻止了您的 UI 更新。你必须把你的循环放在一个额外的线程中,或者只使用一个后台工作者

【讨论】:

  • 它做的不止这些,它甚至没有让构造函数运行
  • 嗯,它就是这么做的!防止在 UI 线程中运行的构造函数完成,仅此而已! OP必须了解原因,而不是了解后果;)
【解决方案2】:

是的,朋友们,你是对的。我放了(使用仪器面板-> 后台工作人员):

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
 while (!IsDisposed)
{
......................
}
System.Threading.Thread.Sleep(60000); // delay in microseconds
}

在我的 public 部分类 someClass : Form 之后 public Someclass()

还添加了:

backgroundWorker1.RunWorkerAsync(2000);

在我的 public Someclass()

所以现在代码看起来像:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;
    using OpenQA.Selenium;
    using OpenQA.Selenium.PhantomJS;
    
    namespace WindowsFormsApplication1
    {
        public partial class Someclass : Form
        {
            private void label1_Click(object sender, EventArgs e)
            {
            }
    
            private void Someclass_Load(object sender, EventArgs e)
            {
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                // Shows some text "Hello friend" 
                MessageBox.Show("Hello friend!");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello again", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
    
            public Someclass()
            {
                InitializeComponent();
                backgroundWorker1.RunWorkerAsync(2000);
            }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
    this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
     while (!IsDisposed)
    {
    MessageBox.Show("Now the page will be downloaded");
                var driverService = PhantomJSDriverService.CreateDefaultService();
                driverService.HideCommandPromptWindow = true; 
                using (var driver = new PhantomJSDriver(driverService))
                {
                    driver.Navigate().GoToUrl("http://stackoverflow.com/");
                    MessageBox.Show("Here we are going to open StackOverflow");
                    var questions = driver.FindElements(By.ClassName("fs-display2"));
                    foreach (var question in questions)
                    {
                        // This will display some text from stackoverflow main page.
                        Console.WriteLine(question.Text);
                        question.Click();
                        MessageBox.Show("This is stackoverflow: " + question.Text);
                    }
                    MessageBox.Show("Here we go");
    }
    System.Threading.Thread.Sleep(60000); // delay in microseconds
    }
        }
    }

并且工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2010-10-18
    • 2015-05-08
    • 1970-01-01
    • 2011-05-29
    • 2012-09-27
    相关资源
    最近更新 更多