0

I've got richTextbox on my application and I want to refresh it every one second. When some function is called timer stops working.

private void button1_Click(object sender, EventArgs e)
{
    //some selenium code to tests something 
}

public void timer1_Tick(object sender, EventArgs e)
{
    String Temp = richTextBox1.Text;
    richTextBox1.Text = Counter + Temp;
    Counter++;
}

flow:

  1. Run app
  2. richTextBox = 1... 12... 123... 1234
  3. Click on button (it takes 10 seconds to finish executing function)
  4. richTextBox = 1... 13... 123... 1234... 12345... 123456...

Is there possibility that during 3rd step timer will work as well?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Kamil J
  • 9
  • 1
  • 1
    BackgroundWorker would be your friend for the selenium code block. – LarsTech Aug 27 '15 at 17:58
  • 1
    If the operation is blocking the UI thread then nothing will be able to update the rich text box (or any other UI element) during that time. Look into threads, background workers, `async`, etc. – David Aug 27 '15 at 18:01
  • possible duplicate of [Display progress bar while doing some work in C#?](http://stackoverflow.com/questions/1952201/display-progress-bar-while-doing-some-work-in-c) – Thomas Weller Aug 27 '15 at 18:10

2 Answers2

0

Forms Timers work through Window messages, and the WindowProc is executed on the main thread, so, if the Selenium code locks the main thred the timer will not be fired.

Also the UI will be blocked, using a Thread Timer and an BeginInvoke will not solve your situation.

As David commented you can create a BackgroundWorker, use the ThreadPool or even better, use (if Selenium has it) an asyncrhronous API to avoid lockiing main thread.

Gusman
  • 14,905
  • 2
  • 34
  • 50
0

Your problem is that the code represented by //some selenium code to tests something is running on and blocking the UI thread so the Timer (and nothing else running on the UI thread) can run until it completes.

You will need to run the //some selenium code to tests something on a separate thread. Look into the BackgroundWorker for an example of this.

Here's some sample code to get you started:

class BackgroundWorkerExample 
{
    private BackgroundWorker _backgroundWorker;

    public Program()
    {
        _backgroundWorker = new BackgroundWorker();
        _backgroundWorker.DoWork += BackgroundWorker_DoWork;
        _backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        _backgroundWorker.RunWorkerAsync();
    }

    private static void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //some selenium code to tests something 
    }

    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //Whatever you want to do when the tests complete
    }
}

There are other options as well such as Thread, ThreadPool, TaskFactory, etc. However since you are already using, and somewhat familiar with, Windows Forms the BackgroundWorker will likely be the easiest choice for you.

Cory Charlton
  • 8,868
  • 4
  • 48
  • 68