0

I have a timer in my application (timer1). When this timer goes off, it calls a sub that refreshes my datagridview. Before calling the refresh sub, I use GetActiveWindow() from user32 Lib to check If the form is the active window. This works as expected. Here is the code I am using to check the active window.

If Me.Handle = GetActiveWindow() Then
    gridRefresh()
Else
    MessageBox.Show("Works")
End If

I included the messagebox just to give me a visual that It is indeed working when the active window is not my Application.

What I am missing though is I would like to call the gridRefresh() sub once my Application becomes the active window again.

My first thought would be to use a Do Until loop and have it do nothing until It becomes the active window again like so:

If Me.Handle = GetActiveWindow() Then
    gridRefresh()
Else
    Do Until Me.Handle = GetActiveWindow()

    Loop
    gridRefresh()
End If

But when I try this solution It never comes out of the loop.

Edit: The timer interval is 1 minute. The reason I want it to refresh once it becomes active again is so the user doesn't have to wait a whole minute to see if anything has been added to the gridview

laylarenee
  • 3,276
  • 7
  • 32
  • 40
Spencer Waz
  • 155
  • 8

2 Answers2

2

You are not doing this correctly, Winforms already supports all this. No need for pinvoke, you can use the Form.ActiveForm property. And the Activate and Deactivate events tell you that a form is de/activated. Put this code in the form that contains the grid:

    protected override void OnDeactivate(EventArgs e) {
        // Runs when the window is deactivated.  Stop the timer
        timer1.Enabled = false;
        base.OnDeactivate(e);
    }

    protected override void OnActivated(EventArgs e) {
        // Runs when the window is activated.  Start the timer and immediately refresh
        timer1.Enabled = true;
        timer1_Tick(this, EventArgs.Empty);
        base.OnActivated(e);
    }

    private void timer1_Tick(object sender, EventArgs e) {
        // Periodically refresh the grid
        gridRefresh();
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you. After converting to vb This worked fine. Out of curiosity, do you know why minimizing the form does not de-activate the form? No issue I just used the minimize and maximize events, But I figured minimizing the form would de-activate it as well.. – Spencer Waz Jan 10 '14 at 12:44
  • Never mind. It does get de-activated when minimized. Apologies – Spencer Waz Jan 10 '14 at 12:50
1

You can add two Boolean fields to your form class, called Active and RefreshRequired.

Then add handlers to the form's Activated and Deactivate events. The Deactivate event handler just sets Active false. The Activated event handler looks like:

Active = True
If RefreshRequired Then
   gridRefresh()
   RefreshRequired = False
End If

Finally you re-write your original code as :

If Active Then
    gridRefresh()
Else
    RefreshRequired = True
End If
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448