调用线程无法访问此对象

时间:2010-11-19 16:24:27

标签: c# wpf multithreading accessibility

此代码“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)}

1 个答案:

答案 0 :(得分:1)

移动以下代码行:

DataClassesDataContext db = new DataClassesDataContext(); 

进入stepByStep()方法正文,然后将db作为参数传递给doStg()方法。

    bool doStg(DataClassesDataContext db)
    {
        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() 
    {
        DataClassesDataContext db = new DataClassesDataContext(); 

        Dispatcher.BeginInvoke(new Action(() => info.Text = "Step 1"), null);
        if (!doStg(db)) { return false; }
        Dispatcher.BeginInvoke(new Action(() => progressBar.Value = 33), null);

        Dispatcher.BeginInvoke(new Action(() => info.Text = "Step 2"), null);
        if (!doStg(db)) { return false; }
        Dispatcher.BeginInvoke(new Action(() => progressBar.Value = 66), null);

        Dispatcher.BeginInvoke(new Action(() => info.Text = "Step 3"), null);
        if (!doStg(db)) { return false; }
        Dispatcher.BeginInvoke(new Action(() => progressBar.Value = 100), null);

        return true;
    }