I'm creating a practice application that plays a sound every so often seconds and as a button is pressed it decreases the time interval for the sound to play. For example at the start it plays a sound every 15 seconds and when I press the button it should now play the sound every 10 seconds. Basically every time the user presses the button it decreases the interval by 5 seconds. So I have a DispatcherTimer at the top of my class and created its interval and Tick method when the page loads like such. I also have an int variable for the seconds in the time span.
DispatcherTimer timer = new DispatcherTimer();
int interval = 15;
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
timer.Interval = new TimeSpan(0, 0, interval);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
interval -= 5;
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, interval);
}
But this makes the sound play crazily. Is there a proper way to do this?
if this is your actual code then Larry is quite right... u have to call timer.stop(); before starting the second.
also give some condition so as to disallow interval < 0
Do you cancel the first dispatcher timer before you start the second? Is it possible that after the first click you have two dispatcher timers firing?
Related
I am developing a quiz in the visual studio with Windows Form but I have to put a timer with a countdown of 20s to appear on the screen and when it reaches 0 it takes to the last Form which is the "Game Over" how can I do that?
There are many ways to do this.
The samplist is you can add a timer(from Toolbox), set interval=1000, then enable a Tick function when the quiz started, count the seconds down, when the totalSeconds hits 0, display "Game Over".
private void startButton_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if(totalSeconds > 0)
{
totalSeconds--;
this.timeDisplay.Text = totalSeconds.ToString();
}
else
{
this.timer1.Stop();
this.timeDisplay.Text = "Game Over";
}
}
I'm using MediaCapture element for video capturing in UWP. However, I cannot find how can I embed this time element (Screenshot is available at here)
to the video. I'm using a Dispatcher to tick and show the time:
DispatcherTimer timer = new DispatcherTimer(); // Timer for clock.
timer.Interval = TimeSpan.FromSeconds(1); // timer ticks in one second.
timer.Tick += Timer_Tick; // Tick event handler.
timer.Start(); // Start the timer.
private void Timer_Tick(object sender, object e)
{
DateTime_Text.Text = DateTime.Now.ToString("G");// Printing datetime on page.
} // Show time in main screen with tick.
Are anyone knows how can I embed this Tick event to MediaCapture ?
Thanks.
i want to change the behavior of my Telerik GanttView TimeLineContainer. The problem is, when i load data into GanttView with long duration (years) the TimeLine scales very bad. That means that it zooms in and not like I would have to zoom out.
So in best case I would like to have the TimeLine scrollbar so that the user can adjust the size on runtime.
So far I tried this, but it doesnt scales right.
this.radGanttView.GanttViewElement.GraphicalViewElement.TimelineContainer.StretchHorizontally = false;
this.radGanttView.GanttViewElement.GraphicalViewElement.TimelineContainer.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.FitToAvailableSize;
Solved this by adding a RadTrackBar:
private void radTrackBar_ValueChanged(object sender, EventArgs e)
{
int zoomfaktor = 10;
double value = 51 + zoomfaktor * Math.Pow(this.radTrackBarZoom.Value, 2d);
Console.WriteLine(string.Format("{0} - {1}", this.radTrackBarZoom.Value, value));
TimeSpan time = new TimeSpan(0, (int)value, 0); this.radGanttView.GanttViewElement.GraphicalViewElement.OnePixelTime = time;
this.radGanttView.GanttViewElement.GraphicalViewElement.OnePixelTime = time;
}
I am using a scrollviewer having stackpanel with 10 low quality images .When flick once the image moves to the next and load the high resolution image of the viewed image in that view. The problem is when flick 6 times continuously,scroll moves 6 times and the method for add high resolution occurs 6 times and load the image in view. I need an idea to wait for the completed flick for some seconds and execute the high resolution add image method once. Is there any way to do that?
You can delay the loading of the high resolution images using a timer.
The code might look something like this:
DispatcherTimer timer = new DispatcherTimer();
public MainPage()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(1); //You can adjust the delay suitable to your needs
timer.Tick += new EventHandler(timer_Tick);
}
void handle_Flick(object sender, GestureEventArgs args)
{
//If timer is not running, start the timer
//and do everything else other than loading high-resolution image.
if(timer.IsEnabled != true)
{
//start the timer
timer.Start();
}
}
void timer_Tick(object sender, EventArgs e)
{
//Stop the timer
timer.Stop();
//Load the high resolution image
}
Is there a way I can detect this? I want to keep performing an action as long as the user is holding on an icon.
Instead of using the GestureListener for this you could instead use the mouse manipulation events to detect how long to perform your action. For instance:
Listen for MouseLeftButtonDown to know when the user has touched the icon
Keep performing the action until either MouseLeftButtonUp or MouseLeave fire indicating that the user is no longer touching that icon
You may also have to play with MouseEnter for initiating the action
Today only i did the same thing in my project.I'll tell you the basic logic what i implemented(assuming it has to be done on button).Step 1: On the button _ManipulationStarted_ event start a timer with the interval after which you want to fire the repeat action.
Step 2: On the button _ManipulationCompleted_ event stop the timer.
Step 3: If the timer is fired,stop the timer and start another timer with interval = the repeat interval for your action.And inside the second timer fire handler perform your operation only if the control has focus. In this case, where the control is a button, you can check if the button.IsPressed is true then perform action.
The code will look something like:
Button forward=new Button();
DispatcherTimer forwardHoldTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
forward.ManipulationStarted += (a, b) => { forwardHoldTimer.Start(); };
forward.ManipulationCompleted += (c, d) => { forwardHoldTimer.Stop(); };
forwardHoldTimer.Tick+=(s1,e1)=>
{
forwardHoldTimer.Stop();
DispatcherTimer t = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) };
t.Tick += (x, y) =>
{
if (forward.IsPressed)
{
//Your action logic will go here
}
else
t.Stop();
};
t.Start();
};
Hope this helps.
NOTE: Amresh Kumar was correct in suggesting using the manipulation events. Also, I was given the same advice on the Windows Phone App Hubs forums so I've edited this post to reflect the code changes.
Before, the UX was flaky because lifting my finger off the screen didn't always trigger a cancellation. Not surprisingly, the GestureCompleted code in the toolkit appears to be better geared towards touchscreens than are mouse button events.
XAML:
<iconControls:iconUpDownArrow>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Tap="RangeUpTap" Hold="RangeUpHold" GestureCompleted="RangeUpCancel" />
</toolkit:GestureService.GestureListener>
</iconControls:iconUpDownArrow>
code:
private void RangeUpTap(object sender, GestureEventArgs e)
{
RangeIncrementUp(sender, e);
}
private readonly TimeSpan _rangeIncrementTimeSpan = new TimeSpan(1500000);
private readonly DispatcherTimer _rangeIncrementTimer = new DispatcherTimer();
private void RangeUpHold(object sender, GestureEventArgs e)
{
_rangeIncrementTimer.Interval = _rangeIncrementTimeSpan;
_rangeIncrementTimer.Tick += RangeIncrementUp;
_rangeIncrementTimer.Start();
}
private void RangeUpCancel(object sender, GestureEventArgs e)
{
_rangeIncrementTimer.Stop();
_rangeIncrementTimer.Tick -= RangeIncrementUp;
}
private void RangeIncrementUp(object sender, EventArgs e)
{
int range = Convert.ToInt32(tBoxRange.Text);
if (range < 1000)
{
range += 10;
}
tBoxRange.Text = range.ToString();
}