этот код "var results ..", выдавая исключение "Вызывающий поток не может получить доступ к этому объекту, поскольку он принадлежит другому потоку".LINQ запрос не плохой.
Пожалуйста, что я не так делаю?
DataClassesDataContext db = new DataClassesDataContext();
bool doStg()
{
try
{
var results = from t in db.table
select t;
//doing some things..
return true;
}
catch (Exception Exception)
{
MessageBox.Show(Exception.Message);
return false;
}
}
bool stepByStep()
{
Dispatcher.BeginInvoke(new Action(() => info.Text = "Step 1"), null);
if (!doStg()) { return false; }
Dispatcher.BeginInvoke(new Action(() => progressBar.Value = 33), null);
Dispatcher.BeginInvoke(new Action(() => info.Text = "Step 2"), null);
if (!doStg()) { return false; }
Dispatcher.BeginInvoke(new Action(() => progressBar.Value = 66), null);
Dispatcher.BeginInvoke(new Action(() => info.Text = "Step 3"), null);
if (!doStg()) { return false; }
Dispatcher.BeginInvoke(new Action(() => progressBar.Value = 100), null);
return true;
}
private void button_Click(object sender, RoutedEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
args.Result = stepByStep();
};
worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
object result = args.Result;
if ((bool)result) { MessageBox.Show("Done"); }
else { MessageBox.Show("Processing stopped."); }
};
worker.RunWorkerAsync();
}
РЕДАКТИРОВАТЬ:
Источник исключения: "System.Data.Linq"
TargetSite: {System.Data.Linq.SqlClient.SqlNode VisitInvocation (System.Linq.Expressions.InvocationExpression)}