Esempio n. 1
0
        public static int Main(string[] args)
        {
            Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", TestUrlOne);
            Console.WriteLine("You may see a lot of Chromium debugging output, please wait...");
            Console.WriteLine();

            //Console app doesn't have a message loop which we need as Cef.Initialize/Cef.Shutdown must be called on the same
            //thread. We use a super simple SynchronizationContext implementation from
            //https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/
            //Continuations will happen on the main thread
            //The Nito.AsyncEx.Context Nuget package has a more advanced implementation
            //https://github.com/StephenCleary/AsyncEx/blob/8a73d0467d40ca41f9f9cf827c7a35702243abb8/doc/AsyncContext.md#console-example-using-asynccontext

            AsyncContext.Run(async delegate
            {
                Cef.EnableWaitForBrowsersToClose();

                var settings = new CefSettings();
                //The location where cache data will be stored on disk. If empty an in-memory cache will be used for some features and a temporary disk cache for others.
                //HTML5 databases such as localStorage will only persist across sessions if a cache path is specified.
                settings.CachePath = Path.GetFullPath("cache");

                var success = await Cef.InitializeAsync(settings);

                if (!success)
                {
                    return;
                }

                var t1 = MainAsync(TestUrlOne, TestUrlTwo, "cache\\path1", 1.0);
                //Demo showing Zoom Level of 2.0
                //Using seperate request contexts allows the urls from the same domain to have independent zoom levels
                //otherwise they would be the same - default behaviour of Chromium
                var t2 = MainAsync(TestUrlThree, TestUrlFour, "cache\\path2", 2.0);

                await Task.WhenAll(t1, t2);

                Console.WriteLine("Image viewer launched.  Press any key to exit.");

                // Wait for user input
                Console.ReadKey();

                //Wait until the browser has finished closing (which by default happens on a different thread).
                //Cef.EnableWaitForBrowsersToClose(); must be called before Cef.Initialize to enable this feature
                //See https://github.com/cefsharp/CefSharp/issues/3047 for details
                Cef.WaitForBrowsersToClose();

                // Clean up Chromium objects.  You need to call this in your application otherwise
                // you will get a crash when closing.
                Cef.Shutdown();
            });

            //Success
            return(0);
        }