Could someone please explain to me the purpose or reasons for using Dispatcher.Invoke?

I am getting the error: *The calling thread cannot access this object because a different thread owns it. * and a quick Google search suggested that was due to not using Dispatcher.Invoke (they also said it was a common problem with newbies). This problem only started happening after I added the Task.Delay method, so it's obviously something to do with that?

The code that I am getting the error on is below:

 if(ImageList.SelectedIndex < (c-1))
            {
                    Task.Delay(1000).ContinueWith(_ =>
                    {
                        Background.Source = new BitmapImage(new Uri(Convert.ToString(AllImages[App.inx + 1])));
                        App.image1 = Convert.ToString(AllImages[App.inx]);
                        App.image2 = Convert.ToString(AllImages[App.inx + 1]);
                    }
                    );
            }

Anytime you need to invoke, it's because you are trying to access a piece of code within another thread. While threads offer the ability to run multiple tasks at the same time, they can't actually talk to each other straight out of the box.

Because of this, you MUST Invoke, requesting from the other thread the right to use its resources. You probably are trying to use a resource that exist in the main thread (such as a Form thread), and your new spun up thread is trying to use the same resource, existing on another thread.

This is assuming I read this correctly.

Right, that makes sense. I'm not entirely sure what threads are, and I couldn't find a succinct explanation online, but they seem to be about CPU load handling?

Also, from what I can tell, I haven't created any threads on my form... So why am I getting a thread-related error?!

Okay so I've done some more research and turns out I had the wrong idea. I now have:

`        public async Task<string> WaitAsynchronouslyAsync()
        {
            await Task.Delay(1000);
            return "Finished";
        }
`

and using it here:

`string result = await WaitAsynchronouslyAsync();

                Background.Source = new BitmapImage(new Uri(Convert.ToString(AllImages[App.inx + 1])));
                App.image1 = Convert.ToString(AllImages[App.inx]);
                App.image2 = Convert.ToString(AllImages[App.inx + 1]);`

The problem now is that this SEEMS to only be executing once. I.e., it works great the first time, but on consecutive tries it doesn't work and messes with my other code (I have a DoubleAnimation which doesn't work now except on the first time it's used)

Sorry, just ignore me. The code does execute each time it's called, I just wasn't seeing the results. Excuse my new-ness to coding.

Good to hear.

Threads do relate to a CPU. In the older days, when you were running multiple tasks on your PC, each task had to share the CPU's resources, allowing only one to run at a time. You wouldn't always recognize it, but the tasks were switching back and forth in relation to who could use the CPU to compute

Now step forward in time, and threading comes into play. With threading it allows for one CPU to run multiple tasks at the exact same time. No more flipping back and forth between tasks and who gets to use resources.

Threading has opened a lot of doors, in all sorts of fields including even videogames. A good example, if you run a form, and you have say a task running you may have noticed the form locks up and you can't move it or anything. If you place that task on a seperate thread, it doesn't lock the form thread, allowing you to interact with it (like pressing buttons, resizing, minimize, ext).

Also when you run a piece of code, a project, like a form, you are automatically on a thread, you don't create it yourself, it's created for you. (In a way you can look at it as in the old days, there only ever was one thread to ever use).

Hopefully that makes sense, it's been a few years since my Computer Architecture class

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.