KB Article A1634
Visible to All Users

How to delay event processing

Description:
I use the FocusedRowChanged event of the XtraGrid. The event handler contains code which takes some time to be executed. When a user scrolls the grid by holding down the up or down arrow keys, the grid scrolls slowly. I would like to prevent my code from being processed while a user is scrolling the grid. Is this possible?

Answer:
It is possible to implement this behavior. The code, which is currently executed in your FocusedRowChanged event handler should be executed by a timer. The timer should stop itself to execute the code only once.
The FocusedRowChanged event handler should start the timer. If the timer is already started (in cases when the FocusedRowChanged event is frequently generated because of continuous scrolling), the timer should be reset - stopped and restarted.

C#
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { if(checkEdit1.Checked) { if(timer1.Enabled) timer1.Enabled = false; // reset timer timer1.Enabled = true; // start timer } else ProcessFocusedRowChanged(); } private void timer1_Tick(object sender, System.EventArgs e) { timer1.Enabled = false; // self-stop timer ProcessFocusedRowChanged(); } // some time consuming process public void ProcessFocusedRowChanged() { ... // your code is here }
Comments (2)

    what if ProcessFocusedRowChanged() method needs sender object and FocusedRowChangedEventArgs ?In that case how this will work

    Alisher (DevExpress Support) 11 years ago

      Hi,
      In this case, create a wrapper class that will store these objects. You can use the Timer's Tag property to get access to an instance of this wrapper class. As a result, you will be able to obtain event arguments and the sender object. Check the following code:

      C#
      private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { if(checkEdit1.Checked) { timer.Tag = new ParametersWrapper(sender, e); if(timer1.Enabled) timer1.Enabled = false; // reset timer timer1.Enabled = true; // start timer } else ProcessFocusedRowChanged(ParametersWrapper); } private void timer1_Tick(object sender, System.EventArgs e) { timer1.Enabled = false; // self-stop timer ProcessFocusedRowChanged(timer.Tag as ParametersWrapper); } // some time consuming process public void ProcessFocusedRowChanged(ParametersWrapper args) { ... // your code is here } public class ParametersWrapper { public FocusedRowChangedEventArgs EventArgs { get; private set; } public object Sender { get; private set; } public ParametersWrapper(object sender, FocusedRowChangedEventArgs e) { this.Sender = sender; this.EventArgs = e; } }

      I hope you find this information useful. Please feel free to contact us in case of further difficulties.

      Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

      Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.