gpt4 book ai didi

c# - 多线程任务表单调用问题,有更好的方法吗?

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:51 24 4
gpt4 key购买 nike

我有一个我编写的应用程序,它执行一些繁重的任务,我在其中显示一个任务表单。此任务表单显示当前进度,以及在繁重任务线程中设置的状态文本。我现在遇到的问题是,我的 Invoke 调用(UpdateStatus 方法)在表单实际有时间显示自身之前被调用,并开始抛出异常。

这是我的表格:

public partial class TaskForm : Form
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;

/// <summary>
/// The task dialog's instance
/// </summary>
private static TaskForm Instance;

/// <summary>
/// Private constructor... Use the Show() method rather
/// </summary>
private TaskForm()
{
InitializeComponent();
}

public static void Show(Form Parent, string WindowTitle, string InstructionText, string SubMessage, bool Cancelable, ProgressBarStyle Style, int ProgressBarSteps)
{
// Make sure we dont have an already active form
if (Instance != null && !Instance.IsDisposed)
throw new Exception("Task Form is already being displayed!");

// Create new instance
Instance = new TaskForm();

// === Instance form is setup here === //
// Setup progress bar...
// Hide Instruction panel if Instruction Text is empty...
// Hide Cancel Button if we cant cancel....
// Set window position to center parent

// Run form in a new thread
Thread thread = new Thread(new ThreadStart(ShowForm));
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Thread.Sleep(500); // Wait for Run to work or the Invoke exceptions are thrown
}

public static void CloseForm()
{
// No exception here
if (Instance == null || Instance.IsDisposed)
return;

try
{
Instance.Invoke((Action)delegate()
{
Instance.Close();
Application.ExitThread();
});
}
catch { }
}

protected static void ShowForm()
{
Application.Run(Instance);
}

public static void UpdateStatus(string Message)
{
if (Instance == null || Instance.IsDisposed)
throw new Exception("Invalid Operation. Please use the Show method before calling any operational methods");

Instance.Invoke((Action)delegate()
{
Instance.labelContent.Text = Message;
});
}

new public void Show()
{
base.Show();
}
}

注意:我确实删除了一些与我的问题无关的代码

问题出在这个例子中:

//显示任务对话框

TaskForm.Show(this, "My Window Title", "Header Text", false);
TaskForm.UpdateStatus("Status Message"); // Exception Thrown HERE!!

如果 ShowForm() 方法中没有 Thread.Sleep(),我会遇到可怕的 Invoke 异常(InvalidOperationException =>“在创建窗口句柄之前无法在控件上调用 Invoke 或 BeginInvoke。”)。有没有更好的方法来显示此表单以防止这些问题?不得不使用 Thread.Sleep() 来等待 GUI 像它应该的那样弹出,我感到内疚。

最佳答案

您可以使用 IsHandleCreated 属性 [msdn]判断窗口句柄是否已经创建。所以不需要 sleep 时间,我们不能保证它是恒定的。

所以你可以使用类似的东西,

TaskForm.Show(this, "My Window Title", "Header Text", false);
while(!this.IsHandleCreated); // Loop till handle created
TaskForm.UpdateStatus("Status Message");

我已经使用过这个并且它对我有用,我也欢迎任何关于这个解决方案的评论。

关于c# - 多线程任务表单调用问题,有更好的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com