创建窗口时 C# WPF 运行操作
我正在一个单独的线程中打开一个新窗口。我希望打开窗口并自动运行作业,然后向该窗口报告其进度(我将使用进度栏)。
打开窗口的操作代码:
private void button_Run_Click(object sender, RoutedEventArgs e)
{
ExecutorWindow myExecWindow = new ExecutorWindow();
myExecWindow.Show();
}
窗口代码:
public partial class ExecutorWindow : Window
{
BackgroundWorker execBackground = new BackgroundWorker();
public ExecutorWindow()
{
InitializeComponent();
RunExecutor();
}
public void RunExecutor()
{
// CREATE BACKGROUNDWORKER FOR EXECUTOR
execBackground.DoWork += new DoWorkEventHandler(execBackground_DoWork);
execBackground.RunWorkerCompleted += new RunWorkerCompletedEventHandler(execBackground_RunWorkerCompleted);
execBackground.WorkerReportsProgress = true;
execBackground.WorkerSupportsCancellation = true;
// RUN BACKGROUNDWORKER
execBackground.RunWorkerAsync();
}
private void execBackground_DoWork(object sender, DoWorkEventArgs e)
{
// CREATE EXECUTOR INSTANCE
Executor myExecutor = new Executor(arg1, arg2);
myExecutor.RunWhileRemainMachines();
}
private void execBackground_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("RunWorkerCompleted execBackground");
}
}
但发生的情况是,仅当我关闭窗口时才调用 RunProcessingJob (这是正常的,因为我在 ShowDialog 之后运行它)。我尝试从公共 ExecutorWindow() 函数调用它,但结果恰恰相反:仅当 RunProcessingJob 完成时窗口才会打开。
我不知道如何订购......我想要的行为是: - 单击按钮 - 打开窗户 - 运行处理作业(无需用户进一步交互) - 运行时更新窗口
我应该在 public ExecutorWindow() 中调用的 BackgroundWorker 中运行我的处理作业吗?
这个处理工作已经在密集使用BackgroundWorkers,我不确定这是一种优雅的方式。
编辑:我已经根据您的评论更新了我的代码(直接运行我的窗口而不创建另一个线程。另外我运行我从窗口构造函数处理工作,但现在在一个单独的线程中,以防止我遇到的阻塞问题)。
I'm opening a new Window in a separate thread. I want the window to open and a job to run automatically, then report its progress to this window (i'll use a progress bar).
The action code that opens the window :
private void button_Run_Click(object sender, RoutedEventArgs e)
{
ExecutorWindow myExecWindow = new ExecutorWindow();
myExecWindow.Show();
}
The window code :
public partial class ExecutorWindow : Window
{
BackgroundWorker execBackground = new BackgroundWorker();
public ExecutorWindow()
{
InitializeComponent();
RunExecutor();
}
public void RunExecutor()
{
// CREATE BACKGROUNDWORKER FOR EXECUTOR
execBackground.DoWork += new DoWorkEventHandler(execBackground_DoWork);
execBackground.RunWorkerCompleted += new RunWorkerCompletedEventHandler(execBackground_RunWorkerCompleted);
execBackground.WorkerReportsProgress = true;
execBackground.WorkerSupportsCancellation = true;
// RUN BACKGROUNDWORKER
execBackground.RunWorkerAsync();
}
private void execBackground_DoWork(object sender, DoWorkEventArgs e)
{
// CREATE EXECUTOR INSTANCE
Executor myExecutor = new Executor(arg1, arg2);
myExecutor.RunWhileRemainMachines();
}
private void execBackground_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("RunWorkerCompleted execBackground");
}
}
But what happens is that the RunProcessingJob is called when I close the window only (which is normal because I run it after ShowDialog). I've tried to call it from the public ExecutorWindow() function but then it's the opposite : The window opens only when the RunProcessingJob is finished.
I dont know how to order that.. the behaviour I want is :
- click button
- open window
- run processing job (without further interaction from user)
- update window while running
Should I run my processing job in a BackgroundWorker called in public ExecutorWindow() ?
This processing job is already making use of BackgroundWorkers intensively and I'm not sure this is an elegant way to go..
EDIT : I've updated my code based on your comments (running directly my window without creating another thread. Plus i run my processing job from the window Constructor, but now in a separate thread to prevent the blocking issues I had).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这完全取决于 Executor.RunProcessingJob 的作用;如果不查看该代码就无法回答该问题。
但是,为什么要在另一个线程上创建 ExecutorWindow 呢?如果您希望它保持对用户输入的响应,则除了在另一个线程上执行处理之外别无选择(您提到了BackgroundWorker,所以这可能已经发生了) 。但如果您这样做,则首先无需在其自己的线程上创建 ExecutorWindow。
It totally depends on what
Executor.RunProcessingJob
does; the question can't be answered without looking at that code.However, why are you creating the
ExecutorWindow
on another thread? If you want it to remain responsive to user input, you have no option other than to execute your processing on yet another thread (you mentionBackgroundWorker
, so this probably happens already). But if you do that, there's no need to createExecutorWindow
on its own thread in the first place.我认为你应该在 ExecutorWindow 类中启动处理线程。将其挂接到某个事件,例如 Load。另外,我认为您不需要单独的线程来创建 &显示您的 ExecutorWindow。 (如果您不想要模式对话框,只需调用 Show() 而不是 ShowDialog())
I think you should start your processing thread in your ExecutorWindow class. Hook it to some event, Load, for example. Also I don't think you need a separate thread for creating & showing your ExecutorWindow. (If you don't want a modal dialog just call Show() instead of ShowDialog())
重写 ExecutorWindow 的 OnInitialize 方法
Override
ExecutorWindow
's OnInitialize Method