I would like to place a pause within a click event in my application. I am placing a vibration within the click event, and sometimes when a new page is navigated to, the vibration does not stop. I'm hoping that the pause will help solve this issue.
MainPage.xaml.cs
void newButton_Click(object sender, EventArgs e)
{
//Button vibration
if (Settings.EnableVibration.Value)
{
VibrateController.Default.Start(TimeSpan.FromMilliseconds(40));
//place vibration stop here
}
...
}
According to this article, the minimal duration is 0.1 seconds, while you are setting it to 0.04. Also I would try to call the VibrateController.Stop() method.
Solved with VibrationController.Default.Stop();
Related
I want to prevent a button click from queuing. In testing I have a Form, a Button and in the Code-Behind I have the event handler:
private void button1_Click(object sender, EventArgs e)
{
if (_codeRunning)
return;
_codeRunning = true;
//Application.DoEvents();
//button1.Enabled = false;
_click ++;
Debug.WriteLine("Click Number: " + _click);
Task.Delay(5000).Wait();
//button1.Enabled = true;
_codeRunning = false;
}
When I run debug and click the button twice or three or four times rapidly, Debug Output shows each click about five seconds after the last one. What I would like it to show is a single Click and drop the rest until first Event is complete.
I have also tried to disable the button, as well as temporarily remove the Handler from the Button_click event. It is all the same results.
There are various amounts of trouble you'll get into when you hang-up the UI thread like this. This is certainly one of them, nothing pleasant happens when the user wildly bangs on the button to try to get something noticeable to happen. And sure, those clicks won't get lost, they stay stored in the message queue. To activate your Click event handler again when your event handler stops running.
Pretty important to learn how to use the BackgroundWorker or Task classes to avoid this kind of trouble. Just setting the button's Enabled property is then enough to solve this problem.
Purging the mouse clicks from the message queue is technically possible. But ugly to do, it requires pinvoke. I'll hesitantly post the alternative, don't assume that this is in general a good strategy. You'll need to read this post to have some insight into why DoEvents() is a dangerous method.
private void button1_Click(object sender, EventArgs e) {
button1.Enabled = false;
button1.Update();
'' long running code
''...
Application.DoEvents();
if (!button1.IsDisposed) button1.Enabled = true;
}
The Update() call ensures that the user gets the feedback he needs to know that banging the button repeatedly isn't going to do anything useful. The DoEvents() call will dispatch all the queued-up mouse clicks, nothing happens with them since the button is still disabled. The IsDisposed test is essential to solve the problem with DoEvents(), it ensures your program won't crash when the user clicked the window's Close button while the code was running.
Use the HourGlass class in this post to provide more feedback.
I had a button that on click event was going to run a method. Same issue happent and when the user clicked multiple times the method was triggered multiple times. So I made a boolean and changed it value when the method started.
private bool IsTaskRunning = false;
private void MyMethod()
{
if ( IsTaskRunning==false )
{
IsTaskRunning=true;
// My heavy duty code that takes a long time
IsTaskRunning=false; // When method is finished
}
}
So now the method runs only if it's done the last time.
I'm developing an App for windows 8.1 and i need to execute a method to pause some tasks or play a sound when the app is minimized in the sidebar.
I tried:
Application.Current.Suspending += new SuspendingEventHandler(Method_Name);
private void Method_Name(object sender, object e)
{
Other_Method();
}
But this event takes a few seconds (5~10) to occur after I minimize the app.
What event occurs when the app is dragged to the sidebar? What process sends the event?
Thanks.
Check out this post for the answer. It's WindowSizeChanged and check the value of ApplicationView.Value.
[EDIT]
Apparently for 8.1 things have changed a bit. The ApplicationView stuff is deprecated (that was quick) but this still takes place in SizeChanged for the window. Check out this for more details.
After long searching I found something that is not exactly what i wanted, but it works.
This event occurs everytime that visibility of the page changes (Is started, is maximized or minimized for exemple), then you must do some conditions using the if operator.
Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(One_Method);
private void One_Method(object sender, Windows.UI.Core.VisibilityChangedEventArgs e)
{
if(Some_Condition)
{
//TODO: Code that you want execute.
}
}
I'll keep the answer in open for the case of someone knows something more efficient.
I have an ASP.NET application in which I have a function that runs on a button click event, all I need is to have another button which when clicked the function that is running should stop executing. I have added the sample code. Can someone please help me on this? Thanks
protected void btnCreateSites_Click(object sender, EventArgs e)
{
GenerateSites("CPM");
}
static void GenerateSites(string siteName)
{
//perform some complex operation here
}
protected void btnStopExecute_Click(object sender, EventArgs e)
{
//stop the execution of the GenerateSites() function when this button is clicked.
}
The first click event needs to launch a separate thread to run the process and store the reference to that thread somewhere. The second click event will get the reference of that thread, if it still exists, and call abort.
That's the simple way. Add some code to your question for an answer that is better suited to what you are trying to achieve.
I'de like you to take a look at this code:
I have a Button named Button1.
private void button1_MouseHover(object sender, EventArgs e)
{
button1.BackColor = Color.Black;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackColor = Color.Blue;
}
This code works but the problem is there is a very small delay. About 1/2 second delay on changing the colors. I've tried the same thing in WPF and there is absolutely no delay in that. Basically I want the Mouse event to fire as quickly as possible.
In what ways can i accomplish that task ?
Thank you
Try using the MouseEnter event rather than MouseHover - the latter is fired 'after a delay' because Windows can't tell that the mouse is hovering unless it has been stationary for a short while.
Calling button1.Invalidate(false) will result in redrawing the control within the next frame. Place this line right after your color-changing code and see if it works.
Tell me please how to execute a method BackgroundAudioPlayer fastforward?
That is, how to do that when you long press the button, fast forward, and then release the button, the track nchinal play, in general as well as buttons work in the locked mode.
If you long press call as follows:
private void btnNext_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
BackgroundAudioPlayer.Instance.FastForward();
}
The track is scrolled to the end and pops up an error.
What is your desired behavior? You probably need some code on the MouseLeftButtonUp event to call Play to resume the playing of the current audio track.