Progressbar behaves strangely

I just created a C # application that uses a thread that polls the UART for a receive event. If the data is received, the event is fired in my main thread (GUI), and the progress bar is controlled using the PerformStep () method (of course, I previously set the Max value accordingly). A call to PerformStep is called using the following expression to handle cross-threading.

this.Invoke((Action)delegate{progressBar2.PerformStep();})

When you run this application, the progress bar never reaches its final value. He stops at 80%. When debugging and stopping in the above line, everything works fine using separate steps. I have no idea what's going on!

Start reading the stream in the main stream:

pThreadWrite = new Thread(new ThreadStart(ReadThread));
pThreadWrite.Start();

Read the topic:

private void ReadThread()
{
while(1)
{
    if (ReceiveEvent)
    {       
    FlashProgressBar();
    }
}
}

An event that fires in the main thread:

private void FlashProgressBar()
{
this.Invoke((Action)delegate { progressBar2.PerformStep();});
} 

( )

, .

2: , , git. . SO:

, , , Windows. Windows Forms - , ++ Windows. -, . , .

, . Invoke , Windows , , . , , , Invoke.

, , BeginInvoke!

EDIT:

thread.sleep(), , . . , , :

:

 private void ReadThread()
    {
        FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
        UInt32 numBytesAvailable = 0;         

        while (true)
        {
            ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable);
            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                 // read data and start timer                    
                 TimeoutWaitForAckn.Start();
            }
        }
    }

, , . :

    private void Timeout_Handler(object sender, System.Timers.ElapsedEventArgs e)
    {
        Thread.Sleep(300);
        Trigger_ReportBufferReceivedEvent(ReceiveBuffer);
    }

- , , ? System.Timers.Timer.

+3
1

, , , .

, , . , ( ). . , 100% 10 . , Debugger.Break().

, , , .

0

Source: https://habr.com/ru/post/1791738/


All Articles