0
DateTime tThen = DateTime.Now;
do
{
    Application.DoEvents();
} while (!cefGlueBrowserForm.Done || tThen.AddSeconds(15) > DateTime.Now);

string htmlSource = cefGlueBrowserForm.DocumentDomHtml;
propertyBag.GetResponse = () => new MemoryStream(Encoding.UTF8.GetBytes(htmlSource));
cefGlueBrowserForm.Dispose();

After few hours I get in line

while (!cefGlueBrowserForm.Done || tThen.AddSeconds(15) > DateTime.Now);

exception of

An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

Here is description of error: http://msdn.microsoft.com/en-us/library/w6sxk224%28v=vs.90%29.aspx

Make sure you do not have an infinite loop or infinite recursion.

Too many method calls is often indicative of a very deep or unbounded recursion.

so what Can I do? I need to wait until some code in cefGlueBrowserForm is finished or time is reached. But why then error, I have time check...

Jim G.
  • 15,141
  • 22
  • 103
  • 166
mbrc
  • 3,523
  • 13
  • 40
  • 64

2 Answers2

1

From MSDN docs

The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated.

Your second condition in || won't be checked if first condition is true.

This program illustrates the concept

class Program
{

    static void Main(string[] args)
    {
        Console.WriteLine( p() || q() ); //prints Return True from p , True
        Console.WriteLine( q() || p() ); //prints Return False from q, Return true from p, True
    }

    static bool p()
    {
        Console.WriteLine("Return True");
        return true;
    }

    static bool q()
    {

        Console.WriteLine("Return False");
        return false;
    }
}
Yaseen
  • 1,348
  • 2
  • 16
  • 31
  • Irrelevant, as the second condition in the question has no side effects. Edit: Actually, I might be completely misunderstanding the point of your answer. Downvote removed for now, but would you elaborate? The point stands, the condition in the question is free of side effects. –  Dec 22 '13 at 19:38
  • "But why then error, I have time check..." I suppose that time check isn't any good if that isn't being checked – Yaseen Dec 22 '13 at 19:44
  • 1
    Ah, now I see your point, which is essentially "you want `&&`, not `||`". That's not clear from your answer, but you're right. Note that if you use `|` instead of `||`, where the second operand would be evaluated, you'd still have the same problem. –  Dec 22 '13 at 19:46
  • How do you think is this related to StackOverlow exception? – JeffRSon Dec 22 '13 at 19:59
  • Yes, it's a bit clearer, thanks for that. I still think you're focusing on the wrong aspect, though, because even if the second operand of `||` were evaluated, the code wouldn't work as intended. –  Dec 22 '13 at 19:59
  • My answer was based on "infinite loop" assumption. if `cefGlueBrowserForm.Done` happens to be `false`, then don't we have an infinite loop there ? – Yaseen Dec 22 '13 at 20:03
1

Application.DoEvents is evil, don't use that. It may cause unexplainable effects - like StackOverflow. Busy-waiting in the UI thread should be avoided. To fix it use, eg, BackgroundWorker.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • Why BackgroundWorker if I just need to wait? I don't need to BackgroundWorker process just for waiting. Or do I? – mbrc Dec 22 '13 at 19:47
  • You obviously use `Application.DoEvents` to prevent blocking the GUI thread. The right way to do it, is to move the blocking part to a background thread. If you can use .Net Framework 4.5 it could be easier to use async/await, though. – JeffRSon Dec 22 '13 at 19:57
  • Sorry - that's too wide a subject. Also I don't know enough about your project. Just search for "c# async await" to find a wealth of information and examples. Re DoEvents see for example http://www.codinghorror.com/blog/2004/12/is-doevents-evil.html and http://stackoverflow.com/questions/5181777/use-of-application-doevents – JeffRSon Dec 22 '13 at 20:09