提问者:小点点

是否可以在没有BackgroundWorker的情况下使WinForms响应?[闭门]


我有一个在C#中使用WinForms制作的程序,它使用的是另一个开发人员提供的API,它必须在主线程中运行(因为它使用的是WM#消息,但可能还有其他原因),所以我不能使用BackgroundWorker。我的程序使用该API至少运行15分钟。因此,当我运行它并单击表单时,它将冻结并崩溃,因为它没有响应。我可以做些什么使表单响应,并且在使用该API时不触发Windows警报“应用程序未响应”?

以下是我在循环中为给定文件夹中的所有文件名运行的代码:

fApi.ApiSetDates(DateTime.MinValue, DateTime.MinValue, invoiceIssueDate.Year, invoiceIssueDate.Month);
try
    {
        if (fApi.ImportFakturFromXML(fileName) != 0)
        {
            throw new Exception(fApi.GetLastError());
        }
        File.Delete(fileName);
    }
    catch (Exception x)
    {
        MessageBox.Show(x.ToString());
    }

共2个答案

匿名用户

下面是一个小Windows窗体的非设计器生成的代码,当单击它的单独按钮时,它在一个循环中执行许多长时间运行的UI线程阻塞操作。这段代码比我最初在评论中建议的计时器更具可读性和可维护性,并且比d.Kastier在他的答案评论中建议的要简单一些。

namespace LongRunningLoop
{
   public partial class Form1 : Form
   {
      private bool m_Closed = false;

      public Form1()
      {
         InitializeComponent(); // set up button1
      }

      // async void event handler.  Usually async void
      // is a no-no, but here we really do want to fire and forget.
      // We prevent nasty things from happening by disabling the UI
      // before starting the main work and exiting if we detect
      // that the form has been closed.
      private async void button1_Click(object sender, EventArgs e)
      {
         // Disable the UI so there's no reentrancy during the Task.Delay()
         button1.Enabled = false;
         for (int i = 0; i < 60; i++)
         {
            if (m_Closed)
            {
               // Don't keep doing work if the user has closed the form
               break;
            }
            Thread.Sleep(5000); // A long-running, blocking call (sleep thread for 5 seconds)
            await Task.Delay(100); // Yield to allow other events to be processed
         }
         // Re-enable the UI
         button1.Enabled = true;
      }

      private void Form1_FormClosed(object sender, FormClosedEventArgs e)
      {
         // Set a flag to signal the loop to exit
         m_Closed = true;
      }
   }
}

匿名用户

我要展示我通常做的事。

您将需要对UI控件的引用,然后在ThreadWG中运行您的代码。

private static void RunOnUiThread(Control c, Action action) 
{
     c.Invoke(new MethodInvoker(action));
}


private void ExecuteMyFunction()
{
     Task.Run(() => 
     {
          // run code in background...

          // run code in Main Thread (UI)
          // anyControl is a `Control` or a `Form`
          RunOnUiThread(anyControl, () => { /* code to run on mainthread */ });

          // run more code in background...
     }
 }