C# 循环时如何显示messagebox或loadingbox?(代码可能被阻止,25秒后继续)

C# 循环时如何显示messagebox或loadingbox?(代码可能被阻止,25秒后继续),c#,timer,sleep,box,C#,Timer,Sleep,Box,我已经有了这个,但它不起作用。。。我需要检查plc的位是否高。如果仍然很高,我需要等待5秒钟,然后再次检查。。现在我试图找到一些东西,以便为用户提供一些视觉反馈。在文本框中,我想输入“等待…”,而等待后的分数每5秒增加一次。我尝试了很多东西,但似乎都没能奏效。大多数情况下,它只是挂起25秒而不更新GUI,然后继续…:/ // First check if the plc bit, that says if there is still an order active, // is still

我已经有了这个,但它不起作用。。。我需要检查plc的位是否高。如果仍然很高,我需要等待5秒钟,然后再次检查。。现在我试图找到一些东西,以便为用户提供一些视觉反馈。在文本框中,我想输入“等待…”,而等待后的分数每5秒增加一次。我尝试了很多东西,但似乎都没能奏效。大多数情况下,它只是挂起25秒而不更新GUI,然后继续…:/

// First check if the plc bit, that says if there is still an order active, 
// is still set. If so then we wait a couple seconds.
var bitorder = Main.PIOGetValue(jbmis.IO_GET_INPUT, "BoxManInStarted");
int counter = 1;
string loadingpoints = "";

loadtimer.Tick += timer_Tick;
loadtimer.Interval = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
loadtimer.Start();
loadtimer.Enabled = true;

// Stopwatch sw = new Stopwatch();

while(bitorder != 0 && loadtimercounter != 25)
{
    // TODO multithreaded

    #region testcode
    // MessageBox.Show("Waiting for previous order to be stopped" + loadingpoints);
    // Context.UserMessageService
    //     .ShowMessage("Waiting for previous order to be stopped" + 
    //                  loadingpoints, "Waitingfororder");
    // sw.Start();

    // while (sw.Elapsed < TimeSpan.FromSeconds(25)) 
    // {
    //     if (sw.Elapsed.Seconds % 5 == 0)
    //     {
    //         loadingpoints = loadingpoints + ".";
    //         tbScannedEANPick.Background = Brushes.OrangeRed;
    //         tbScannedEANPick.Text = "Waiting" + loadingpoints;
    //     }                                        
    // }

    // sw.Stop();
    // loadingpoints = loadingpoints + ".";
    // tbScannedEANPick.Background = Brushes.OrangeRed;
    // tbScannedEANPick.Text = "Waiting" + loadingpoints;
    // tbScannedEANPick.UpdateLayout();
    #endregion

    if (loadtimercounter % 5 == 0)
    {
        loadingpoints = loadingpoints + ".";
        tbScannedEANPick.Background = Brushes.OrangeRed;
        tbScannedEANPick.Text = "Waiting" + loadingpoints;
        tbScannedEANPick.IsReadOnly = true;

        bitorder = Main.PIOGetValue(jbmis.IO_GET_INPUT, "BoxManInStarted");
    }

    counter ++;
}

//  After 25 seconds stop timer and continue
loadtimer.Stop();

void timer_Tick(object sender, EventArgs e)
{
    loadtimercounter += 5;
}
我找了半天。。。我试着使用线程。睡眠,计时器,秒表。。。全部为主螺纹或侧螺纹


提前谢谢

你需要在一个单独的线程上完成这项工作。调查

或者,您可以简单地使用多线程。我建议使用异步编程来完成后台工作和更新GUI的文本框控件。

您应该使用后台工作程序。 有一个专用的报告进度事件可用于更新所需的加载框


计时器和线程。睡眠秒表,嗯?只要您在UI线程本身中执行长时间运行的作业,它就不会工作。您可以尝试拆分作业,并将作业的一小部分放入计时器开关/案例步骤中。或者你可以使用Task、Thread和BackgroundWorker来简单地完成那些不在UI线程中的长时间工作。。但问题是我的主线还在继续。。它需要暂停25秒,然后继续。因为在25秒后,我会检查该位是否仍然很高。所以你需要暂停主线程而不阻塞GUI?这就是你需要的吗?是的。。。我现在在主线程中执行一个循环,以检查myThread.IsAlive是否为真。。如果它是假的,那么我可以继续。。但现在又出现了一个新问题。我得到一个异常,表示有另一个线程拥有的对象,我认为文本框…:/我是否可以使用某种没有“确定”按钮的messagebox来显示程序正忙?我认为显示更多代码比只显示这一部分更有帮助。我不能将所有代码都放在这里..:你知道一种方法可以让主线程暂停25秒,间隔5秒,而UI可以被阻止,但是我需要显示一个每5秒等待一次的框吗?但是在循环中检查一些东西的时候,只看到一个等待25秒的小框似乎太过了。这就是方法,如果您想在执行其他操作时更新UI,则没有其他方法。这并不是那么糟糕,尝试一下,你会发现,一旦你明白了这很容易。