Repository navigation
Expand file tree
/
Copy pathmultithreading-in-csharp.cpp
More file actions
146 lines (120 loc) · 4.53 KB
/
Copy pathmultithreading-in-csharp.cpp
File metadata and controls
146 lines (120 loc) · 4.53 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TPL5
{
public partial class Form1 : Form
{
public Thread threadid1;
public Thread threadid2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Mohammad Talha - L14-5011
// Manahil Hassan - L15-5006
}
private void start_Click(object sender, EventArgs e)
{
message.Text = "Threads Starts";
// Enable buttons
sync.Enabled = true;
quite.Enabled = true;
// Starts threads
threadid1 = new Thread(new ThreadStart(Thread1));
threadid2 = new Thread(new ThreadStart(Thread2));
threadid1.Start();
threadid2.Start();
}
private void sync_Click(object sender, EventArgs e)
{
start.Enabled = false;
unsync.Enabled = true;
// Stop threads
threadid1.Abort();
threadid2.Abort();
threadid1 = new Thread(new ThreadStart(Thread1));
threadid2 = new Thread(new ThreadStart(Thread2));
threadid1.Start();
Thread.Sleep(750); // deplay to the theads 500 + 1000 / 2 = 750 .. this will sychronized the threads , and execute the threads one by one
threadid2.Start();
message.Text = "Threads Synchronized";
}
private void unsync_Click(object sender, EventArgs e)
{
// Stop threads
threadid1.Abort();
threadid2.Abort();
// Starts threads
threadid1 = new Thread(new ThreadStart(Thread1));
threadid2 = new Thread(new ThreadStart(Thread2));
threadid1.Start();
threadid2.Start();
message.Text = "Threads Unsynchronized";
Console.WriteLine("Unsync Threads");
}
private void quite_Click(object sender, EventArgs e)
{
// Stop threads
threadid1.Abort();
threadid2.Abort();
// Stop threads garphics
this.Invalidate();
start.Enabled = true;
sync.Enabled = false;
unsync.Enabled = false;
message.Text = "Threads stopped";
Console.WriteLine("Stop Threads");
}
public void Thread1()
{
for (int i = 0; i < 10; i++)
{
try {
Console.WriteLine("Thread {0} started" + i, Thread.CurrentThread.Name);
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
Pen myPent1 = new Pen(System.Drawing.Color.Red, 2);
Rectangle myRectangle = new Rectangle(40, 80, 150, 150);
graphicsObj.DrawRectangle(myPent1, myRectangle);
Thread.Sleep(500);
Console.WriteLine("Thread {0} completed" + i, Thread.CurrentThread.Name);
this.Invalidate();
Thread.Sleep(1000);
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
}
public void Thread2()
{
for (int i = 0; i < 10; i++)
{
try {
Console.WriteLine("Thread {0} started" + i, Thread.CurrentThread.Name);
// MessageBox.Show("Start Threads 2");
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
Pen myPent2 = new Pen(System.Drawing.Color.Green, 2);
Rectangle myCircle = new Rectangle(310, 80, 150, 150);
graphicsObj.DrawEllipse(myPent2, myCircle);
Thread.Sleep(500);
Console.WriteLine("Thread {0} completed" + i, Thread.CurrentThread.Name);
this.Invalidate();
Thread.Sleep(1000);
} catch(Exception e) {
Console.WriteLine(e.ToString());
}
}
}
}
}