在另一个线程中创建模式对话框 [英] Creating a modal dialog in another thread

查看:185
本文介绍了在另一个线程中创建模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要创建一个显示一些自定义进度信息的模式对话框.但是,我需要在调用方继续执行其工作的同时在线程中进行操作.

我创建了一个简单的基于BackgroundWorker的类,该类拥有并创建对话框.它工作正常,但是我无法使对话框成为我的应用程序的模态.我猜这是因为它是由另一个线程拥有和管理的.我想知道是否有办法解决这个问题.

代码的主要部分如下:

Hello everyone,

I need to create an modal dialog box that shows some custom progress information. However, I need to do it in a thread while the caller keeps doing its job.

I created a simple BackgroundWorker based class which owns and creates the dialog. It works fine but I am unable to make the dialog modal to my application. I am guessing this is because it is owned and managed by another thread. I was wondering if there is a way to get around this.

The main bits of code is as follows:

public class ProgressDialogThread : BackgroundWorker
{        
  public ProgressDialogThread()
  {}
  protected override void OnDoWork(DoWorkEventArgs e)
  {
     // myForm is my Winform
     myForm.ShowDialog();         
  }
}



我可以简单地称其为:



And I can simply call this as:

ProgressDialogThread pdt = new ProgressDialogThread();
pdt.RunWorkerAsync();



如我所说,对话框显示出来,除此模式问题外,其他一切都很好.

有人可以给我任何帮助,我将不胜感激.

非常感谢,

Keith



As I said, the dialog shows and everything else is fine besides this modality issue.

I would greatly appreciate any help someone can give me.

Many thanks,

Keith

推荐答案

据我所知,您对形式和模态是正确的.

您可以尝试切换方法:显示启动BackgroundWorker的模式窗体.

这也使您可以轻松地处理进度事件并显示进度栏(或动画的仍在工作"的栏).

如果要以OO方式执行此操作,请将委托传递给将调用BackgroundWorker的函数,并进行一些额外的事件连接,以便您仍可以处理进度.这将允许重新使用该对话框.

干杯.
As far as I know, you''re right on forms and modality.

You could try switching your approach: show a modal form that starts a BackgroundWorker.

This would also allow you easily to process the progress events and show a progress bar (or an animated ''still working'' bar).

If you want to do this in an OO fashion, pass in a delegate to the function that will call the BackgroundWorker and do some extra event wiring so that you can still handle progress. This would allow for re-use of the dialog.

Cheers.


是的,这是这样做的明智方法(通过使用BackgroundWorker达到其预期的目的!).但是,调用者是一个不受管的DLL,它是一成不变的,没有更改的余地...

我本可以成为一名飞行员...我为什么决定做软件!

不过感谢您的答复.

基思(Keith)
Yes, that would be the sane way to do this (by using the BackgroundWorker for it''s intended purpose!). However, the caller is an unmanaged DLL and it is sort of set in stone with no scope for changes...

I could have become a pilot...why did I ever decide to do software!

Thanks for your reply though.

Keith


这篇关于在另一个线程中创建模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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