Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code

namespace WpfApplication71
{

public partial class MainWindow : Window
{
Thread ProgressThread;
Window w;
public MainWindow()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
ProgressThread = new Thread(() =>
{
w = new Window();
RoundProgressBar objprogress=new RoundProgressBar();
w = new Window();
w.Margin = new Thickness(0, 0, 50, 0);
w.WindowState = WindowState.Normal;
w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
w.Height = 80;
w.Width = 80;
w.ResizeMode = ResizeMode.NoResize;
w.AllowsTransparency = true;
w.WindowStyle = WindowStyle.None;
RoundProgressBar Progress = new RoundProgressBar();
w.Content = objprogress;
w.ShowInTaskbar = false;
w.ShowDialog();
w.Content = Progress;
w.ShowInTaskbar = false;
w.ShowDialog();

w.Closed += (sender2, e2) =>
w.Dispatcher.InvokeShutdown();

System.Windows.Threading.Dispatcher.Run();
});

ProgressThread.SetApartmentState(ApartmentState.STA);
ProgressThread.Start();
}



}
}

when i click the button thread start the progress bar in middle of the screen .when i change main screen that progree bar window not changing .

I want to display that progress bar middle of that window . if i move anywhere.

w.Owner = this;

i know with the help of this code this will work perfect.but i am working in thread so this error is occured

"The calling thread cannot access this object because a different thread owns it"

Please help me out from this problem
Posted

Use a BackgroundWorker thread. That has the necessary callbacks that marshal the progress over to the UI thread and then you can update your progress bar.
 
Share this answer
 
Here is how the problem looks:

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

[EDIT]

Now, one of the problems is: you are creating the thread using its constructor on some click. This can makes number of running threads uncontrolled and pretty expensive. You can use better options: 1) do the same thing, but create extra thread in the very beginning of the run-time and throttle them by keeping them in a wait state using some thread synchronization primitives, typically System.Threading.EventWaitHandle; 2) use System.Threading.ThreadPool, http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^]; 3) use the class System.ComponentModel.BackgroundWorker, http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^].

[END EDIT]

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
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