微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Windows 中的“while”循环形成应用程序 - 没有打开窗口c#

如何解决Windows 中的“while”循环形成应用程序 - 没有打开窗口c#

我是 C# 新手,我想创建一个 windows 窗体应用程序,它显示(它必须是可见的!)一个带有一些信息和按钮的窗口,并且它还从互联网加载一个页面(使用 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”,我的窗口显示良好,但是,当然,页面加载只进行一次。什么可以解决我的问题?

解决方法

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

,

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

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
}

在我的公共部分类 someClass 中:在 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
    }
        }
    }

并且工作正常。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。