Hey all

I've been developing in c# for a few months now, but to my shame have only recently started looking at threads. And now that I do look at them, I'm having issues...

Here's a sample of my code:

private void cmdNext_Click(object sender, RoutedEventArgs e)
        {
            ...
            Thread t = new Thread(new ThreadStart(obj.makeGraph));
            t.Start();
            
            ...
        }

        public void makeGraph()
        {
            ...
            if ((Boolean)radioProduct.IsChecked)
            {
                 ...
            }
            ...

The code builds perfectly, but when I run it I get an "InvalidOperationException was unhandled" when I try to run the if statement. Apparently "The calling thread cannot access this object because another thread owns it." Any idea of where I'm going wrong, or what I can do to fix it?

Thanks in advance

Recommended Answers

All 5 Replies

You didn't post enough code to indicate what the problem is here. Please post enough code where we can compile and see the error.

Anyway i'm not sure what your code is calling but when it comes to controls and updating the GUI that must be done on the main thread. Threads should not access controls on the GUI without calling .Invoke() to synchronize the threads to prevent concurrency issues. You may way to pass the value of your controls in to the thread before you start it. Also be sure to call Thread.Sleep() to sleep your main thread after your start a thread to be sure it has time to initialize before the method finishes. If your method finishes before the thread initializes it could potentially get garbage collected and lead to intermittent errors.

t.Start();
      Thread.Sleep(1000); //give the thread time to start

Sorry I gave so little code. Hope this helps some

private void cmdNext_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;
            TrendControl.SelectedIndex = 1;
            TrendReport obj = new TrendReport();
            Thread t = new Thread(new ThreadStart(obj.makeGraph));

            t.Start();
            Thread.Sleep(1000);
            
            this.Cursor = Cursors.Arrow;
        }

        public void makeGraph()
        {
            //A list encapsulating an entity name, and the number of times it appears
            List<Pair<String, int>> entities = new List<Pair<String, int>>();

            //Is the Product Radio Button Checked?
            if ((Boolean)radProduct.IsChecked)
            {
                //LINQ Query
                var ent = (from e in db.ELCReports
                           select e.VendorProductDateBridge.VendorProductBridge.VendorProductBridge_ProductName);
                var ents = ent.Distinct();
                //Scan through Products
                foreach (var en in ents)
                {
                    var elcid = (from e in db.ELCReports
                                 where en.Equals(e.VendorProductDateBridge.VendorProductBridge.VendorProductBridge_ProductName)
                                 select e.ELCReport_ID);
                    //Create Pairs
                    Pair<String, int> pair = new Pair<String, int>();
                    if (elcid.Count() > 0)
                    {
                        pair.field1 = en;
                        pair.field2 = elcid.Count();
                        entities.Add(pair);
                    }
                }
            }
        }
    }

I don't think I can really give enough to compile since I'm using WPF, and I can't really give you a bunch of XAML code as well. Sorry.
Anyway, could you elaborate a little on the .Invoke() Method you mentioned? I can't seen to find it anywhere... is it linked to a specific class, or an instance?

Thanks for the help

Hmmm... thanks...

trying to sort my way through delegates and the like... loks very confusing... but hopefuly worth it for a little performance boost.

Thanks

Thanks again!

I read through the msdn tutorial on the dispatched, got it through the link you gave me. Nice, clear, easy to understand examples on the working of the Dispatcher and delegates. I think anyone having my kind of problem should spend some time reading through that.

You were a great help :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.