我的窗口中的线程问题 [英] Thread Problem in my window

查看:69
本文介绍了我的窗口中的线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

命名空间WpfApplication71
{

公共局部类MainWindow:Window
{
线程ProgressThread;
窗户w;
公共MainWindow()
{
InitializeComponent();
}

private void button1_Click(对象发送者,RoutedEventArgs e)
{
ProgressThread =新线程(()=>
{
w = new Window();
RoundProgressBar objprogress =新的RoundProgressBar();
w = new Window();
w.Margin = new厚度(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 =新的RoundProgressBar();
w.Content = objprogress;
w.ShowInTaskbar = false;
w.ShowDialog();
w.Content =进度;
w.ShowInTaskbar = false;
w.ShowDialog();

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

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

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



}
}

当我单击按钮线程时,在屏幕中间启动进度条.当我更改主屏幕时,进度条窗口不会更改.

我想在该窗口的中间显示进度条.如果我搬到任何地方.

w.Owner = this;

我知道在此代码的帮助下,它将可以完美工作.但是我在线程中工作,因此发生此错误

调用线程无法访问该对象,因为其他线程拥有它"

请帮助我解决此问题

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

推荐答案

使用BackgroundWorker线程.它具有必要的回调,可将进度封送至UI线程,然后您可以更新进度栏.
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.


问题出在这里:

您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [ http://msdn.microsoft.com/en-us /library/system.threading.threadpool.aspx [ ^ ]; 3)使用类 System.ComponentModel.BackgroundWorker http://msdn.microsoft.com/en -us/library/system.componentmodel.backgroundworker.aspx [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
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[^].



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


这篇关于我的窗口中的线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆