.net C# - 在标题栏上显示"Not responding"消息后停用 Winform



我有一个ClientForm(从MainWindow打开(,它有一个搜索按钮,点击大约需要12秒才能处理。在搜索期间,如果用户多次点击该表单";不响应";text get显示在其标题栏上,这是可以的,但在搜索完成后,ClientForm将失去焦点或将被停用,并且MainWindow会获得焦点-这是问题

下面是代码片段。

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// this is a wpf window
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var cf= new ClientForm();
cf.Show();
}
}

// This is a winform 
public partial class ClientForm : Form
{
public ClientForm()
{
InitializeComponent();
}
private void Search_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
// Do some processing
Thread.Sleep(12000);
Cursor.Current = Cursors.Default;
}
}

这是.net框架的问题吗?

我没有使用后台线程进行搜索,或者没有使用ShowDialog显示表单。我想知道如何解决这个问题,以及为什么会出现这个问题。

谢谢!!!

使用后台线程,我自己的示例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
button2.Enabled = false;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{

backgroundWorker1.ReportProgress(i);
Thread.Sleep(100);

}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void button1_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
button1.Enabled = false;
button2.Enabled = true;
label1.Text = "Runing...";
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
button2.Enabled = false;
label1.Text = "Complete";
}
private void button2_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
backgroundWorker1.CancelAsync();
button1.Enabled = true;
button2.Enabled = false;
label1.Text = "Cancel";
}
}
}
}

谢谢你的帮助,但我已经在下面的链接中找到了这个奇怪行为的答案。https://learn.microsoft.com/en-us/windows/win32/win7appqual/preventing-hangs-in-windows-applications?redirectedfrom=MSDN

带有";不响应";不是实际窗口,因为实际窗口被设置为隐藏并因此被停用。为了解决这个问题,我删除了GhostWindowFunctional,现在没有失去焦点。唯一的缺点是用户不能移动、最小化和关闭没有响应的应用程序,但我们有任务管理器来关闭/杀死这样的应用程序。

// This is a winform 
public partial class ClientForm : Form
{
[DllImport("user32.dll")]
public static extern void DisableProcessWindowsGhosting();    
public ClientForm()
{
InitializeComponent();
DisableProcessWindowsGhosting();   // This will disable it
}
private void Search_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
// Do some processing
Thread.Sleep(12000);
Cursor.Current = Cursors.Default;
}
}

相关内容

最新更新