两个窗口之间的线程调用? [英] Thread calls between two windows?
问题描述
嗨!第一个问题,长期的读者!
我正在尝试与线程化应用程序打交道,但似乎无法克服第二个障碍.我管理的第一个窗口-在单独的线程中启动第二个窗口.但是,如果第一个窗口无法促使第二个窗口执行操作,那是没有用的!反之亦然.
将来的想法是,一个窗口是具有列表和按钮以及一个调度程序的主要控件.另一个监视事物,但是具有自己的用户交互.例如,第一个更改第二个监视的内容;第一个更改第二个监视的内容.第二个稍后报告将其监视的结果报告给第一个显示并修正...
我有一个带有两个按钮的窗口-打开窗口"和关闭窗口".
Hi! First question, long time reader!
I''m trying to get to grips with threaded apps but can''t seem to get passed the second hurdle. The first I''ve managed - to launch a second window in a separate thread. However, it is no use if the first window cannot prod the second into doing things! And visa-versa.
In future, the idea is that one window is the main control which has lists and buttons and a scheduler maybe; the other monitors things but has it''s own user interaction. The first for example changes what the second monitors; the second later reports back what it has monitored to the first to display and corrolate...
I have a window with two buttons - "Open Window" and "Close Window".
private void ButtonOpen_Click(object sender, RoutedEventArgs e)
{
Thread tbr = new Thread(() =>
{
Window2 br = new Window2();
br.Closed += (sender2, e2) => br.Dispatcher.InvokeShutdown();
br.Show();
System.Windows.Threading.Dispatcher.Run();
});
tbr.SetApartmentState(ApartmentState.STA); //required for WPF
tbr.Start();
((Button)sender).IsEnabled = false;
}
private void ButtonClose_Click(object sender, RoutedEventArgs e)
{
//need to figure out how this works
}
ButtonOpen_Click创建一个线程,并通过UI消息泵启动Window2的新实例,并在关闭时具有宽限退出.
但是,如何通过第一个窗口的ButtonClose_Click调用第二个窗口中的"this.close"?既然第一个在不同的线程上?我想如果我能破解,那么我想通过单击第一个窗口中的按钮以调用第二个中发生的事情来调用的任何东西都可以使用相同的原理?
在此先感谢您的帮助.
ButtonOpen_Click creates a thread and launches a new instanace of Window2 with UI message pump and has a gracefull exit when closed.
But, how do I invoke "this.close" in the second window via the first''s ButtonClose_Click? Since the first is on a different thread? I guess if I can crack this, then the same principle can be used for anything I want to call via a button click on the first window to invoke something to happen in the second?
Thanks in advance for your help.
推荐答案
您好,亲爱的
有时我正在研究一个聊天程序.因为很多客户端连接到服务器,所以服务器必须在多线程上工作,我遇到了像你这样的很多问题,但是最终我可以解决我的问题.在每个线程中的线程和控件之间都有一种标准的通信方式,它是"Invoke"方法,它是"Control"类的方法之一.
我以一个例子向你表示公道:
我用两种形式创建一个项目:Form1,Form2
Form1有两个按钮:BtnOpenForm,BtnCloseForm
当用户单击BtnOpenForm时,将创建一个新线程,并在该线程中创建Form2的新实例,然后显示此新的异常
当用户单击BtnCloseForm时,此新实例将关闭
这是一个抽象的样本,并不完整.但您可以在任何线程中的任何控件之间进行通信
表格1:
Hello Dear
Some times a go i was working on a chat program . because a lot of client connect to server , it was necessary that server work on multiple thread , i had a lot of problem like you , bu finally i could solve my problem . there is a standard way to communicate between thread and controls in each thread , and it is ''Invoke'' method which is one of methods of ''Control'' class.
I guild you by one example :
I create a project with two forms : Form1 , Form2
Form1 has two button : BtnOpenForm , BtnCloseForm
when user clicks BtnOpenForm a new thread creates and in that thread create a new instance of Form2 then this new insatnce is shown
when user clicks BtnCloseForm this new instance will close
this is an abstract Sample not a complete one . but you can Communicate between any controls in any thread
Form1 :
public delegate void DelegetCloseForm();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Form2 F;
private void BtnOpenForm_Click(object sender, EventArgs e)
{
System.Threading.Thread T = new System.Threading.Thread(delegate()
{
F = new Form2();
F.ShowDialog();
});
T.Start();
}
private void BtnCloseForm_Click(object sender, EventArgs e)
{
this.F.Invoke(F.Dlg);
}
}
表格2:
Form2 :
public DelegetCloseForm Dlg;
private void CloseForm()
{
this.Close();
}
public Form2()
{
InitializeComponent();
Dlg = new DelegetCloseForm(this.CloseForm);
}
这很简单,只需要一点练习
如果有问题评论我
祝你好运
this is very simple , just need a little practice
if had problem comment me
Good luck
感谢Reza Mansoori615指明了方向!我非常感激.那就是我最初尝试的方式,进行了更改,然后尝试了一些随机的事情,这些事情越来越令人沮丧.
但是,这是Windows窗体的方式,而不是WPF.更多的外观是,我发现了一个提示-在WPF中,通过分派器调用了Invoke.所以...
窗口1:
Thanks Reza Mansoori615 for pointing the way! I''m extremely grateful. That''s how I tried it originally, changed it, then tried random things getting more and more frustrated.
However, that''s the Windows Forms way, not WPF... A bit more looking and I found a hint - in WPF, Invoke is called via the Dispatcher. So...
Window 1:
public delegate void dWindow2Close(); //declare - outside window class
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private Window2 br;
private void ButtonOpen_Click(object sender, RoutedEventArgs e)
{
Thread tbr = new Thread(() =>
{
br = new Window2();
br.Closed += (sender2, e2) =>
br.Dispatcher.InvokeShutdown();
br.Show();
System.Windows.Threading.Dispatcher.Run();
});
tbr.SetApartmentState(ApartmentState.STA); //required for WPF
tbr.Start();
((Button)sender).IsEnabled = false;
}
private void ButtonClose_Click(object sender, RoutedEventArgs e)
{
br.Dispatcher.Invoke(br.Window2Close);
}
}
窗口2
Window 2
public partial class Window2 : Window
{
public dWindow2Close Window2Close; //to instantiate
public Window2()
{
InitializeComponent();
Window2Close = new dWindow2Close(SecondWindowClose);
}
private void SecondWindowClose()
{
this.Close();
}
}
对于那些询问的人,我打算打开5到10个浏览器窗口,以实时监视实时数据并整理到中央数据库.导航DOM很简单,因此没有实际工作可以转移给后台工作人员.但是,当每个浏览器开始运行Flash广告时,一个线程的工作量并不小!我的所有其他CPU都处于空闲状态,而单个CPU却处于阻塞状态...在单独的线程窗口中启动每个浏览器似乎是我唯一的选择.
再次感谢那些阅读并回复的人!
For those who asked, I intend opening 5 to 10 browser windows to monitor live data in real time and collate to a central DB. Navigating the DOM is trivial so there is no actual work to offload to a background worker. However, when each browser starts running flash ads the workload is not trivial for one thread! And all my other CPU''s stand idle while a single one is choking... Launching each browser in a seperately threaded window seems my only option.
Thanks again to those who read and replied!
这篇关于两个窗口之间的线程调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!