C# Closing SoundPlayer on FormClosing - c#

I've created a bool value named musiclose. I'm planning if this musiclose value is true, player will close, else it will continue playing.
public static bool musiclose;
Timer code is below:
timer1.Interval = 1000;
timer1.Start();
Here is the code:
public void timer3_Tick(object sender, EventArgs e)
{
SoundPlayer player = new SoundPlayer(#"C:\Path\To\intro.wav");
if (musiclose)
{
player.Stop();
musiclose = false;
}
}
How can I do that??

1) Do not recreate SoundPlayer instance everytime inside timer3_Tick() event. Create a global instance of SoundPlayer.
2) Implement form closing event, https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx
3) In the close event,
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
player.Stop();
player.Dispose();
}

Related

How to resume a game timer from another form?

I'm making a pong game in windows forms and I can't figure out how to unpause after pausing in my gameplay form. After pausing a pause screen form is started and after clicking unpause it unpauses, however, I don't know how to make the timer start again. Please let me know.
Here's my code:
Gameplay form:
if (isPaused)
Paused();
}
private void Paused()
{
gameTimer.Stop();
PausedScreen pausedWindow = new PausedScreen();
pausedWindow.Show();
isPaused = false;
}
Pause Screen form:
public PausedScreen()
{
InitializeComponent();
}
private void UnPause(object sender, EventArgs e)
{
Hide();
//Resume timer
}
private void QuitGame(object sender, EventArgs e)
{
Application.Exit();
}
You can catch the event "Closed" when your pausedScreen is closed and then restart the timer in this function.
gameTimer.Stop();
PausedScreen pausedWindow = new PausedScreen();
pausedWindow.Show();
isPaused = false;
pausedWindow.Closed += new EventHandler(Closed);
private void Closed(object sender, EventArgs e){
gametimer.Start();
}
Hope this solves your problem.

Run Code after Child form is closed

so I have this code
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.ShowDialog();
ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);
}
}
public void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
updaterAccess();
menu.Items[2].Enabled = true;
}
so after I click Update it will run the child form Form1
which is this:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipTitle = "Update Complete";
notifyIcon1.BalloonTipText = "Successfully Update";
notifyIcon1.ShowBalloonTip(500);
timer1.Interval = 4000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
notifyIcon1.Dispose();
this.Close();
}
so as you can see it runs on a backgroundworker with a timer to close the child Form1
now my problem is that after closing the Child Form1 it doesn't run the MyForm_FormClosed which it should enable menu.Items[2] again and updaterAccess()
I think I'm missing something in my mainForm
Attached the event handler before firing ShowDialog
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed); //Attached the event handler before firing ShowDialog
ShowProgress.ShowDialog();
}
}
ShowDialog synchronously shows a modal dialog, meaning it blocks until the form is closed (the following code is not run until the form is closed). Therefore, when ShowDialog returns the form is already closed.
You can attach the event handler before calling ShowDialog() as #Jade suggests, which will work, but honestly you do not need to use the event system at all. Simply wait for ShowDialog to return then perform the actions you would when the form is closed:
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.ShowDialog();
}
updaterAccess();
menu.Items[2].Enabled = true;
}
If you want to do this in VB:
AddHandler ShowProgress.FormClosed, AddressOf MyForm_FormClosed

SendKeys GUI Bug

I've made this program in C#:
namespace Spammer
{
public partial class Form1 : Form
{
int delay, y = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
timer1.Interval = delay;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
It works fine most of the time, and it can really send keys quickly.
But when I insert a delay of, for example, 10 MS, it's very hard to click the "Stop" button to stop it. The only way to stop the sending is to close the program and I don't want to do that.
Is there anyway I can send keys very quickly, like 5-10 MS, without it impairing my ability to press the buttons inside the program? I can't click while it's sending quickly...
The problem is that you're using SendWait. That will wait for the target application to respond - and while that's happening, your application won't be able to respond to user input. If you use Send instead of SendWait, your UI thread won't be blocked waiting for the key press to be processed.
I was able to reproduce the issue. The app is sending a keystroke every 10 milliseconds. To me, this is not at all surprising that the app is causing freezes. A keystroke every 10 milliseconds is quite a barrage to the active App. Threading is not going to help. Why is this behavior surprising?
In other words, I don't expect things to work out well when I overload the message pump.
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spammer//your own namesapce
{
public partial class Form1 : Form
{
int delayInMilliseconds, y = 1;
private Timer timer1;
public Form1()
{
InitializeComponent();
//StartTimerWithThreading();
SetupTimer();
}
void StartTimerWithThreading()
{
Task.Factory.StartNew(() =>
{
SetupTimer();
});
}
void SetupTimer()
{
timer1 = new Timer();//Assume system.windows.forms.timer
textBox2.Text = "10";//new delay
timer1.Tick += timer1_Tick;//handler
}
private void button1_Click(object sender, EventArgs e)
{
delayInMilliseconds = int.Parse(textBox2.Text);
timer1.Interval = delayInMilliseconds;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
The simple solution is instead of adding code to a Click event handler for your button, we need a MouseDown event handler:
//MouseDown event handler for the button2
private void button2_MouseDown(object sender, EventArgs e) {
timer1.Enabled = false;
}
Or you can keep using the Click event handler but we send the key only when the MouseButtons is not Left like this:
private void timer1_Tick(object sender, EventArgs e) {
String textt = textBox1.Text;
if(MouseButtons != MouseButtons.Left) SendKeys.Send(textt);
}
//then you can freely click your button to stop it.

Timer doesn't fire after Form closed

I have a .NET form with a System.Windows.Forms.Timer declared using the VS designer. The timer works fine. After I close the form, the timer doesn't fire events even if I recreate the Timer object. I've configured the Form to never close using this:
void MainFormFormClosing(object sender, FormClosingEventArgs e)
{
// never close
e.Cancel = true;
// only hide
this.Visible = false;
}
How do I make the timer fire events? What am I doing wrong?
I just tried this one. Added a WinForms Timer component on the form, start timer on load, and debug current time in debug window. Workes fine for me...
public frmTimer()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Debug.WriteLine(DateTime.Now.ToLongTimeString());
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Visible = false;
}

Timer in C# that fires X seconds after opening program?

How can I run a function, after 10 seconds, after the opening of the program.
This is what I tried, and I'm not able to make it work.
private void button1_Click(object sender, EventArgs e)
{
Timer tm = new Timer();
tm.Enabled = true;
tm.Interval = 60000;
tm.Tick+=new EventHandler(tm_Tick);
}
private void tm_Tick(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
You have a few problems:
You need to use the Load event rather than a button click handler.
You should set the interval to 10000 for a 10 second wait.
You are using a local variable for the timer instance. That makes it hard for you to refer to the timer at a later date. Make the timer instance be a member of the form class instead.
Remember to stop the clock after you run the form, or, it will try to open every 10 seconds
In other words, something like this:
private Timer tm;
private void Form1_Load(object sender, EventArgs e)
{
tm = new Timer();
tm.Interval = 10 * 1000; // 10 seconds
tm.Tick += new EventHandler(tm_Tick);
tm.Start();
}
private void tm_Tick(object sender, EventArgs e)
{
tm.Stop(); // so that we only fire the timer message once
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
Is will be good for your program something like that?
namespace Timer10Sec
{
class Program
{
static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(After10Sec));
t.Start();
}
public static void After10Sec()
{
Thread.Sleep(10000);
while (true)
{
Console.WriteLine("qwerty");
}
}
}
}

Categories

Resources