简体   繁体   中英

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

I already have this but it doesn't work... I need to check if a bit from plc is high or not. If it is still high I need to wait 5 seconds and check again.. Now I am trying to find something so there is some visual feedback for the user. In a textbox I want to put 'waiting...' while the points after waiting increase per 5 seconds. I tried a lot of things but can't seem to get it to work. Mostly of the time's it just hangs 25 seconds without updating the GUI and then it continues... :/

// 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;
}

I am searching for half a day... I tried to use Thread.sleep, timer, stopwatch, ... all in main thread or side thread..

Thanks in advance!!

You need to do the work on a separate thread. Look into asynchronous programming

Or you could just simply use multi threading. I would recommend using asynchronous programming for both doing the background work and updating the GUI's textbox control.

You should use a background worker . There is a dedicated report progress event that can be used to update the loading box that you need.

Background Worker Class and example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM