ArgumentException when adding UserControl to StackPanel
when a program is trying to add WordBlock (which is my class extending
UserControl) to StackPanel's content in DoSend method, it sometimes
(actually quite often, especially when query returns more than one result)
throws an ArgumentException, it surely has something to do with Threading
(SearchThreadEngine is a method running in in second thread), but I am
weak in the topic, and don't know why it is happenig. So, I will gladly
accept any help. Here's a stack trace:
{System.ArgumentException: Value does not fall within the expected range.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at
MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1
collection, CValue value)
at
MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1
collection, DependencyObject value)
at
System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject
value)
at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value)
at System.Windows.PresentationFrameworkCollection`1.Add(T value)
at Dictionary.MainPage.DoSend(IQueryable`1 words, WordContext context)}
System.Exception {System.ArgumentException}
public void DoSend(IQueryable<Word> words, WordContext context)
{
Result.Children.Clear();
using (context)
{
foreach (Word word in words)
{
Result.Children.Add(new WordBlock(word));
}
waitHandle.Set();
}
}
public void SearchThreadEngine()
{
while (!abort)
{
if (ToSearch != "")
{
string toSearch = ToSearch;
Thread.Sleep(200);
if (toSearch != ToSearch)
continue;
WordContext wc = new WordContext(WordContext.connectionString);
ToSearch = "";
IQueryable<Word> result = (from w in wc.Words where
w.Foreign.Equals(toSearch) || w.Foreign.StartsWith(toSearch+"
") select w);
if(result.Count() == 0)
result = (from w in wc.Words where w.Foreign.Equals("to
"+toSearch) || w.Foreign.StartsWith("to "+toSearch + " ")
select w);
if (result.Count() != 0)
{
Result.Dispatcher.BeginInvoke(new SendResult(DoSend), new
Object[] { result, wc });
waitHandle.WaitOne();
}
}
}
abort = false;
}
No comments:
Post a Comment