简体   繁体   中英

How can I run my browser only one time

I want to start my browser only one time and then get unstatic information from there. But my browser starts many times. How can I start it only one time and close it, when my 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 ();
        }
    }

May this will work for you.

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 ();
        }
    }

The reason behind this is while (GameIsRun) code . GameIsRun is always true that is why it goes to infinite loop.

How you can overcome this issue : you have to make the value of GameIsRun false after launching the browser just like this :

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

Use this code , once the browser has launched , it would make GameIsRun as false.

Hope it'll help you to resolve your issue.

For that you can use singleton concept.

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

  private Singleton()
  {
  }

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

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM