Downloading do not wait for OnBeforeDownload

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

Downloading do not wait for OnBeforeDownload

Postby pssgcsim » Sun Jun 16, 2013 2:50 pm

Downloading do not wait for OnBeforeDownload and still downloading. Can i prevent it and wait for user response ?
Code: Select all
        protected override void OnBeforeDownload(CefBrowser browser, CefDownloadItem downloadItem, string suggestedName, CefBeforeDownloadCallback callback)
        {
            var question = Prompts.ShowQuestion("Do you want to download file \"" + suggestedName + "\"\nfrom url \"" + downloadItem.Url + "\" ?", MessageBoxButtons.YesNoCancel);
            bool canContinue = false;

            if (question == DialogResult.Yes)
            {
                string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                canContinue = true;
                callback.Continue(dir + "\\" + suggestedName, true);
            }

            _core.OnDownload(browser, downloadItem, suggestedName, callback, canContinue);
        }

        protected override void OnDownloadUpdated(CefBrowser browser, CefDownloadItem downloadItem, CefDownloadItemCallback callback)
        {
            _core.OnDownloadUpdated(browser, downloadItem, callback);
        }

Code: Select all
            bool isReady = false;
            bool canContinue = true;
            string suggestedName = "";
            browser.DownloadStarted += (s, e) =>
            {
                canContinue = e.CanContinue;
                suggestedName = e.SuggestedName;
                isReady = true;
            };

            browser.DownloadUpdated += (s, e) =>
            {
                if (isReady)
                {
                    if (e.DownloadItem.IsComplete || e.DownloadItem.IsCanceled)
                    {
                        _statusLabel.Text = "Ready ...";
                        _statusLabel.Visible = false;
                        canContinue = true;
                        suggestedName = "";
                        isReady = false;
                    }
                    else
                    {
                        if (canContinue == false)
                        {
                            e.Callback.Cancel();

                            _statusLabel.Visible = false;
                            canContinue = true;
                            suggestedName = "";
                            isReady = false;
                        }
                        else
                        {
                            _statusLabel.Text = "Downloading file " + suggestedName +
                                "...  Downloaded " + e.DownloadItem.PercentComplete + "% " +
                                StringTools.byteToMegabyte(e.DownloadItem.ReceivedBytes) +
                                " / " + StringTools.byteToMegabyte(e.DownloadItem.TotalBytes) + " ...";

                            _statusLabel.Visible = true;
                        }
                    }
                }
            };
User avatar
pssgcsim
Techie
 
Posts: 13
Joined: Sat May 25, 2013 10:56 am

Re: Downloading do not wait for OnBeforeDownload

Postby fddima » Mon Jun 17, 2013 3:28 am

AFAIK, methods OnBeforeDownload and OnDownloadUpdated can be called out-of-order and your's code must be ready for this situation.
Read more in this thread: viewtopic.php?f=6&t=10478&p=15863&hilit=OnBeforeDownload#p15861
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am


Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 16 guests