TsButton – a weird problem

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #34400
    Nimral
    Participant

    Hi all,

    can anyone give me a hint what I am doing wrong? What I want is a TsButton, Label = “Start”, which starts a thread doing something. Only one instance of the thread may run, but the thread can be started multimple times if, after it finishes, someone clicks the “Start” button again. While teh therad runs, a “Stop” button can be used to terminate it. My idea was to use one single button for this: it's caption is initialized to read “Start”, while the thread runs I change the button caption to “Stop”, if teh thread finishes (eiteh by button press or because it reached its end) the button caption changes to “Start” and the user may decide to click it for another run of the thread. Kind of a toggling switch.

    So I wrote the button onCLick handler like (complete source is attached)

    Code:
    if Button.Caption = 'Start” then
    begin
    // Button checked while no thread is running – start thread!
    Button.Caption := 'Stop';
    // create a new thread
    Mythread.create;
    // set thread properties
    // …
    // Start the thread
    MyThread.execute;
    end
    else
    // set flag to termiate the thread
    MyThread.Terminate;

    And the MyThread class has a destructor which contains

    Code:
    Button.Caption := 'Start';

    The Thread.execute routine does right now only contain a loop with “sleep” statements to simulate a real worker thread doing something, and moves a progress bar so I can see it is doing something.

    The atatched code works … almost. Problem: when I click the “Start” button to start the thread, the button caption, like intended, changes to “Stop”. This works 100%. The other way round does not: when the thread ends, or when I click the now so called “Stop” button while the therad is running, the thread terminates gracefully, but the button caption says still “Stop”, though, according to the debugger, the button caption text was correctly set by the thread destructor to read “Start”. If I examine the button caption using debugger/watch, it does correctly read “Start”, but “Stop” is displayed on screen. If I move the mouse pointer outside the button, or if I minimize and restore the program main window, the correct text “Start” is painted immedately. The button caption changes to “Start” without click, if I move the mouse pointer to an area outside the button rectangle. It seems that the button face is not repainted correctly, unless I move the mouse pointer outside. Trying this several times, I found, to my surprise, that this behaviour occurs *most* of the times. Sometimes the same procedure yields the expected result: the button caption changes back to “Start”. I tried say a hundred of times to find the cause of this inconsistent behaviour, but found nothing. Looks like a weird timing issue in the GUI.

    Already tried: put several Button.Repaint, Button.Redraw, Mainform.Repaint, Mainform.Redraw and Application.ProcessMessages in any place I could think of, but it did not help.

    Anyone having an idea why this behaviour occurs, and what I can do to get my program working?

    Are there times/conditions when a redraw of graphical elements simply does not work / is ignored even if I use calls to repaint and redraw to force this???

    Thx,

    Armin.

    #45051
    Support
    Keymaster

    Hello

    Try to use Synchronize procedure. I think, you have a problem because not uses that.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.