Windows 表单没有响应:我该如何解决这个问题?
Windows form not responding: How can I resolve this issue?
我创建了一个 windows 服务和一个安装项目。
在 windows 服务的安装过程中,我添加了一个 windows 表单,允许用户上传项目文件夹中的文件,但是当我点击按钮上传文件时,我的windows 表单始终处于状态 未响应
我的 windows 服务的 ProjectInstaller
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
Form1 validationForm = new Form1();
validationForm.ShowDialog();
}
Windows形式
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
try
{
OpenFileDialog fileDialog = new OpenFileDialog();
//fileDialog.Filter = "Dat files |*.dat";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
var path = fileDialog.FileName;
Process.Start(path);
}
}
catch (Exception)
{
MessageBox.Show("An error occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我认为 Process.Start(path);
阻止 UI 线程。
尝试使用 Task.Run(() => Process.Start(a));
代替。
试试这个。
private void button1_Click(object sender, EventArgs e)
{
var task = new Thread(() => GetFile());
task.SetApartmentState(ApartmentState.STA);
task.Start();
task.Join();
}
private static void GetFile()
{
try
{
OpenFileDialog fileDialog = new OpenFileDialog();
//fileDialog.Filter = "Dat files |*.dat";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
var path = fileDialog.FileName;
Process.Start(path);
}
}
catch (Exception)
{
MessageBox.Show("An error occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
您的 UI 由于 运行 过程较长而被锁定,这就是为什么您会看到 "Not responding"
标记您的点击 Async
:
private async void button1_Click_1(object sender, EventArgs e)
和
await Task.Run(() =>
{
//Insert the long running stuff here
Process.Start(path);
});
我创建了一个 windows 服务和一个安装项目。
在 windows 服务的安装过程中,我添加了一个 windows 表单,允许用户上传项目文件夹中的文件,但是当我点击按钮上传文件时,我的windows 表单始终处于状态 未响应
我的 windows 服务的 ProjectInstaller
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
Form1 validationForm = new Form1();
validationForm.ShowDialog();
}
Windows形式
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
try
{
OpenFileDialog fileDialog = new OpenFileDialog();
//fileDialog.Filter = "Dat files |*.dat";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
var path = fileDialog.FileName;
Process.Start(path);
}
}
catch (Exception)
{
MessageBox.Show("An error occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我认为 Process.Start(path);
阻止 UI 线程。
尝试使用 Task.Run(() => Process.Start(a));
代替。
试试这个。
private void button1_Click(object sender, EventArgs e)
{
var task = new Thread(() => GetFile());
task.SetApartmentState(ApartmentState.STA);
task.Start();
task.Join();
}
private static void GetFile()
{
try
{
OpenFileDialog fileDialog = new OpenFileDialog();
//fileDialog.Filter = "Dat files |*.dat";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
var path = fileDialog.FileName;
Process.Start(path);
}
}
catch (Exception)
{
MessageBox.Show("An error occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
您的 UI 由于 运行 过程较长而被锁定,这就是为什么您会看到 "Not responding"
标记您的点击 Async
:
private async void button1_Click_1(object sender, EventArgs e)
和
await Task.Run(() =>
{
//Insert the long running stuff here
Process.Start(path);
});