Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 10-Nov-11 0:41am
v3
Comments
Mark Salsbery 10-Nov-11 11:14am    
What is the type of object owned by another thread - a UI element?

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.
 
Share this answer
 
Try one of these two solutions

C#
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

C#
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!!
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900