我怎么能 运行 我的浏览器只用一次

How can I run my browser only one time

我只想启动浏览器一次,然后从那里获取非静态信息。但是我的浏览器启动了很多次。当我的 bool = false;

时,我怎样才能只启动它一次并关闭它
class Program
    {
        public static bool GameIsRun = true;
        static void Main(string[] args)
        {
            CheckGame();
        }

        public static ChromeDriver GetDriver()
        {
            return new ChromeDriver();
        }

        public static void CheckGame()
        {
            while (GameIsRun)
            {
                GetInformation(GetDriver());
            }
        }

        static void GetInformation(ChromeDriver driver)
        {
            driver.Navigate().GoToUrl("myURL");
            do
            {
               //loop for doing something on this page, I don't want to start my browser many times.  
            }
            while ();
        }
    }

希望这对你有用。

class Program
    {
        public static bool GameIsRun = true;
        public static IWebDriver driver = null;
        static void Main(string[] args)
        {
            CheckGame();
        }

        public static ChromeDriver GetDriver()
        {
            if(driver == null){
                 driver = new ChromeDriver();
            }
            return driver;
        }

        public static void CheckGame()
        {
            while (GameIsRun)
            {
                GetInformation(GetDriver());
            }
        }

        static void GetInformation(ChromeDriver driver)
        {
            driver.Navigate().GoToUrl("myURL");
            do
            {
               //loop for doing something on this page, I don't want to start my browser many times.  
            }
            while ();
        }
    }

这背后的原因是 while (GameIsRun) 代码。 GameIsRun 始终为真,这就是它进入无限循环的原因。

如何克服这个问题:启动浏览器后,您必须将 GameIsRun 的值设置为 false,就像这样:

public static void CheckGame()
            {
                while (GameIsRun)
                {
                    GetInformation(GetDriver());
                    GameIsRun = false;
                }
            }

使用此代码,一旦浏览器启动,它会使 GameIsRun 为 false。

希望它能帮助您解决问题。

为此你可以使用单例概念。

public sealed class Singleton
{
  private static Singleton instance=null;

  private Singleton()
  {
  }

  public static Singleton Instance
  {
    get
    {
        if (instance==null)
        {
            instance = new Singleton();
        }
        return instance;
    }
   }
 }

希望对您有所帮助。