-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
84 lines (71 loc) · 2.49 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using bounce.Properties;
using System;
using System.Drawing;
using System.Media;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.DataFormats;
namespace bounce
{
public partial class Form1 : Form
{
private bool hasFunctionRun = false;
private SoundPlayer simpleSound = new SoundPlayer(Resources.funky);
public Form1()
{
InitializeComponent();
}
private async void buttonOpenForm_Click(object sender, EventArgs e)
{
if (!hasFunctionRun)
{
playSimpleSound();
hasFunctionRun = true;
}
Random random = new Random();
await Task.Run(() =>
{
for (int i = 0; i < 50; i++)
{
Invoke((Action)(() =>
{
Form formToShow;
if (random.Next(2) == 0) // Generate 0 or 1 randomly
formToShow = new Form2();
else
formToShow = new Form3();
formToShow.StartPosition = FormStartPosition.Manual;
formToShow.Location = new Point(random.Next(Screen.PrimaryScreen.Bounds.Width - formToShow.Width), random.Next(Screen.PrimaryScreen.Bounds.Height - formToShow.Height));
formToShow.Show();
}));
// Introduce a short delay to keep the UI responsive
Task.Delay(100).Wait(); // Blocking delay inside the background task
}
});
}
private void buttonOpenForm2_Click_1(object sender, EventArgs e)
{
Task.Run(() => simpleSound.PlaySync());
Form2 form2 = new Form2();
form2.Show();
}
private void playSimpleSound()
{
simpleSound.PlayLooping();
}
// Method to stop the sound and dispose of the SoundPlayer instance
private void StopSimpleSound()
{
if (simpleSound != null)
{
simpleSound.Stop();
simpleSound.Dispose();
simpleSound = null; // Set to null to avoid any further use of the disposed object
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
StopSimpleSound(); // Stop and dispose of the sound
}
}
}