C# Windows 窗体无法响应单击操作

发布于 2024-11-14 22:17:27 字数 856 浏览 4 评论 0原文

我有一个 WindowsForm 项目,它运行两个 BackgroundWorker 对象来测试是否应该收集数据以及数据的收集。这些似乎运行正常,但是当我添加查看收集的数据的功能时,表单在尝试绘制可用数据的图表时冻结。

单击第一个按钮时,数据将在带有列表框的新表单窗口中列出。在这个新窗口上,我有另一个按钮,用于绘制数据图表(如果看起来不错的话)。

这是整个项目冻结的地方。我尝试过在构造函数中创建一个新对象,而不是软复制传递到新表单中的类引用。只要数据收集 BackgroundWorker 未运行,它就看起来工作正常。当收集发生时,单击绘图按钮会冻结表单,数据显示按钮工作正常。

private void wellStatusButton1_Click(object sender, EventArgs e)
{
     try
     {
        ListDataForm temp = new ListDataForm(tubes[0], 1);
        temp.ShowDialog();
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
  }

private void plotDataButton_Click(object sender, EventArgs e)
{
     try
     {
        GraphForm plotData = new GraphForm(at);
        plotData.ShowDialog();
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
}

I have a WindowsForm project that is running two BackgroundWorker objects for testing if data should be collected and the collection of the data. These seem to run ok, but when I add the functionality to look at the data collected, the form freezes upon trying to graph the data available.

When clicking the first button the data is listed in a new form window with list boxes. On this new window I have another button that is for graphing the data if it seems ok.

This is where the whole project freezes. I have tried instead of soft copying the class reference that I pass into the new form, making a new object within the constructor. And as long as the data collection BackgroundWorker is not running, it appears to be working ok. When the collection is happening, clicking the plot button freezes the form, the data show button works just fine.

private void wellStatusButton1_Click(object sender, EventArgs e)
{
     try
     {
        ListDataForm temp = new ListDataForm(tubes[0], 1);
        temp.ShowDialog();
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
  }

private void plotDataButton_Click(object sender, EventArgs e)
{
     try
     {
        GraphForm plotData = new GraphForm(at);
        plotData.ShowDialog();
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2024-11-21 22:17:27

很难从你的问题中获得很多信息,但可能对你有用的一件事是:当 Windows 程序在后台执行其他操作时,通常 UI 可能会被冻结。您可以尝试定期使用:Application.DoEvents(),这将使 UI 显得响应更快。

It is hard to get much information from your question, but one thing that might be useful to you is this: When a windows program is doing something else in the background, usually the UI may be frozen. You can try using: Application.DoEvents() periodically, that will allow the UI to appear more responsive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文