Click here to Skip to main content
12,073,837 members (64,729 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: WPF threads
Hi,
Am New In WPF and Backround Worker. I have Done Project For Download Manager I have Developed through Backgroundworker. Pause,Resume and Stop also doned in my project.

But requirement is During Net disconnected, Automatically Paused. so i have check net connection in every second in timer. so after come net connection i need to continue already doing that process.During that time it cause error.

Following Error:
The calling thread cannot access this object because a different thread owns it.  

May i know how do i continue already dowload processing or how do i resume that ?
Posted 10-Nov-11 1:17am
prabut1.1K
Edited 10-Nov-11 1:41am
Slacker00790.3K
v3
Comments
Mark Salsbery 10-Nov-11 11:14am
   
What is the type of object owned by another thread - a UI element?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You are trying to access an object in one thread that was created in a different thread. This is not allowed in C# so you have to find a different way to communicate between your two (or more) threads.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try one of these two solutions

public static void InvokeIfRequired(this DispatcherControl control, Action operation)
{
  if (control.Dispatcher.CheckAccess())
  {
    operation();
  }
  else
  {
    control.Dispatcher.BeginInvoke(DispatcherPriority.Normal, operation);
  }
}
 
Then, it's simple to do:
 
Dispatcher.CurrentDispatcher.InvokeIfRequired(()=>{ theButton.Content="Hello"; });

or this

public void Test()
{
    Button theButton = button1 as Button;
 
    if (theButton != null)
    {
        // Checking if this thread has access to the object.
        if (theButton.Dispatcher.CheckAccess())
        {
            // This thread has access so it can update the UI thread.      
            theButton.Content = "Hello";
        }
        else
        {
            // This thread does not have access to the UI thread.
            // Place the update method on the Dispatcher of the UI thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                (Action)(() => { theButton.Content = "Hello"; }));
        }
    }  
}

both found here on CodeProject!!
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 10 Nov 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100