簡體   English   中英

循環時如何顯示消息框或加載框? (代碼可能會被阻止,並在25秒后繼續顯示)

[英]How can I show a messagebox or loadingbox while doing a loop? (Code may be blocked and after 25 seconds continue)

我已經有了它,但是它不起作用...我需要檢查plc的數據是否很高。 如果仍然很高,我需要等待5秒鍾,然后再次檢查。。現在,我正在嘗試查找某些內容,以便為用戶提供一些視覺反饋。 在文本框中,我想放置“ waiting ...”,而等待后的點數每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;
}

我正在搜索半天...我試圖在主線程或側線程中使用Thread.sleep,計時器,秒表等。

提前致謝!!

您需要在單獨的線程上進行工作。 研究異步編程

或者,您可以只使用多線程。 我建議同時使用異步編程進行后台工作和更新GUI的文本框控件。

您應該使用后台工作者 有一個專用的報告進度事件,可用於更新所需的加載框。

后台工作者類和示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM