Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frequency of WPF-slider ValueChanged-Event

In my application I use two sliders to control the brightness and contrast of certain images and the image has to be completely recalculated pixel by pixel every single time when either one of the two sliders changes its value-property. The recalculation of smaller images goes completely fine and doesn't cause any problems, however, larger images need longer to be recalculated and the slider thumb moves with a slight delay compared to the mouse pointer. I do need the image to be recalculated more or less in real time so simply having an event on DragCompleted or similarly is not acceptable.

The recalculation is initialized using the ValueChanged-event of the slider. I think a good solution to this problem would be if the event is not fired as quickly as possible but will at least wait say 50ms before it is fired again, but is there a property of a slider that can control that?

Another solution I was thinking of, is removing the event from the slider right at the start when the event gets handled and adding it again some short time later, but that might cause some delay itself which is also not preferred here.

I couldn't really find anything on this topic anywhere, so if somebody has any good suggestions or directions I could use, I would be very greatful.

like image 991
philkark Avatar asked Dec 26 '22 21:12

philkark


2 Answers

You can also make use of BindingBase.Delay property introduced in WPF 4.5.

Simply bind Slider's value to a dependency property setting Delay on the binding. This will cause value updates only after certain time (e.g. 500 ms) and this can make your app smoother.

like image 93
Libor Avatar answered Dec 28 '22 09:12

Libor


If you think your application don't need to do the calculations every time the ValueChanged event is triggered,You can use the DragCompleted Event in Thumb control to determine the position after the user finished dragging the control.

<Slider Thumb.DragCompleted="Slider_DragCompleted_1"  
    Height="27" Margin="132,162,0,0" VerticalAlignment="Top" Width="303"/>

When the user stopped dragging,

private void Slider_DragCompleted_1(object sender, DragCompletedEventArgs e)
{
    Slider s = sender as Slider;
    // Your code
    MessageBox.Show(s.Value.ToString());
}

But beware that this works only when user drags the slider.This doesn't get triggered when user clicks on the slider.

Refer this for handling other events like mouse click etc..

If you want to calculate with some time delay then you can use a timer .

EDIT: Based on your request you can do like this. In the 'ValueChanged' event.

 // Start a new thread only if the thread is stopped 
 // or the thread has not been created yet.
 if (threadPopular == null || threadPopular.ThreadState == ThreadState.Stopped)
 {
           threadPopular = new Thread(new ThreadStart(Your function));
           threadPopular.Start();
  }
like image 37
Naren Avatar answered Dec 28 '22 11:12

Naren