2013-12-22 59 views
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(); 

幾個小時,我行「類型的未處理的異常 'System.StackOverflowException' 發生在System.Windows.Forms.dll中」

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

異常

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

後得到這裏是錯誤的描述: http://msdn.microsoft.com/en-us/library/w6sxk224%28v=vs.90%29.aspx

確保你沒有無限循環或無限遞歸。

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

那麼我該怎麼辦?我需要等到cefGlueBrowserForm中的某些代碼完成或達到時間。但是,爲什麼那麼錯誤,我有時間檢查...

+0

您的狀態在||將不會被檢查。如果第一個條件成立。 –

+2

請不要刪除[你原來的問題](http://stackoverflow.com/questions/20731211/an-unhandled-exception-of-type-system-stackoverflowexception-occurred-in-syste)只是爲了重新發布它。 – hvd

+0

看看這個callstack吧! – JeffRSon

回答

1

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.

如果第一個條件爲真在||你的第二個條件不會被選中。

這個程序說明了這個概念

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; 
    } 
} 
+0

無關緊要,因爲問題的第二個條件沒有副作用。編輯:其實,我可能完全誤解了你的答案。現在挪動Downvote,但你會詳細說明嗎?問題在於,問題中的條件沒有副作用。 – hvd

+0

「但是,爲什麼然後錯誤,我有時間檢查...」我想如果沒有被檢查,時間檢查不是很好 –

+1

啊,現在我明白了你的觀點,它基本上是「你想要的'&&' ,而不是'||'「。從你的回答中不清楚,但你是對的。請注意,如果您使用'|'而不是'||',那麼第二個操作數將被評估,您仍然會遇到同樣的問題。 – hvd

1

Application.DoEvents是邪惡的,請不要使用。它可能會導致無法解釋的效果 - 比如StackOverflow。應該避免在UI線程中忙於等待。修復它的使用,例如BackgroundWorker

+0

爲什麼BackgroundWorker如果我只需要等待?我不需要BackgroundWorker進程只是爲了等待。或者我? – mbrc

+0

你顯然使用'Application.DoEvents'來防止阻塞GUI線程。正確的做法是將阻擋部分移動到後臺線程。但是,如果您可以使用.Net Framework 4.5,則可以更容易地使用async/await。 – JeffRSon

+0

你能舉個例子說明你的意思嗎? thx – mbrc

相關問題