Skip to content

Commit

Permalink
Q7 - Implement events
Browse files Browse the repository at this point in the history
  • Loading branch information
sikatikenmogne committed May 4, 2024
1 parent cd8df1b commit 8bebce3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Apply the concepts of threading and synchronization mechanisms to solve practica
4. [x] 4. Implement threads (including parameterized threads)
5. [x] 5. Implement thread pools
6. [x] 6. Implement asynchronous delegates and callback procedures
7. [ ] 7. Implement events
7. [x] 7. Implement events
8. [ ] 8. Implement mutexes
9. [ ] 9. Implement monitors
10. [ ] 10. Implement semaphores
Expand Down Expand Up @@ -202,6 +202,9 @@ Describe the expected output of the code.

Create the CLclient and CLserver classes to achieve the same output as observed in point B.

#### D. Event Implementation:
**💻 [related commit]()**

---
### Q8 - Synchronization:

Expand Down
21 changes: 21 additions & 0 deletions concurency-workshop/NS_CLIENT/CLclient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace concurency_workshop.NS_CLIENT
{
public class CLclient
{

private string _name;
public CLclient(NS_SERVER.CLserver server, string name)
{
_name = name;

server.MessageReceived += Server_MessageReceived;
}

private void Server_MessageReceived(object sender, string message)
{
Console.WriteLine($"{_name} received message: {message}");
}
}
}
22 changes: 22 additions & 0 deletions concurency-workshop/NS_SERVER/CLserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace concurency_workshop.NS_SERVER
{
public class CLserver
{
// Declare a delegate for the event
public delegate void MessageEventHandler(object sender, string message);

// Declare the event using the delegate
public event MessageEventHandler MessageReceived;

private string _msg;
public string Msg {
get { return _msg; }
set
{
_msg = value;
// Trigger the event when the message is set
MessageReceived?.Invoke(this, _msg);
}
}
}
}
49 changes: 49 additions & 0 deletions concurency-workshop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal class Program
/// </summary>
/// <param name="id">The unique user ID.</param>
/// <returns>A string containing the username for the specified ID.</returns>
private delegate void DELG(object o);


public static int addMethod(int i1, int i2)
{
Expand Down Expand Up @@ -240,8 +242,55 @@ static void Main(string[] args)

Console.WriteLine("Q6 - ==============END=============");
Console.WriteLine();


// -------------------------------

// <summary>
// Q7 - Evt
// </summary>

Console.WriteLine("Q7 - =========Evt=========");
Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Initialisation du thread principal...ok");
DELG delg;
NS_SERVER.CLserver server = new NS_SERVER.CLserver();
NS_CLIENT.CLclient client1 = new NS_CLIENT.CLclient(server, "C1");
NS_CLIENT.CLclient client2 = new NS_CLIENT.CLclient(server, "C2");
string[] messages = {"msg1","msg2","msg3"};
delg = (o) =>
{
for (int i = 0; i < messages.Length; i++)
{
server.Msg = messages[i];
System.Threading.Thread.Sleep(4000);
}
};
Console.WriteLine("Début traitement asynchrone...ok");
IAsyncResult asr = delg.BeginInvoke(((object)("nostate")),
(asR)=>
{
delg.EndInvoke(asR);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Fin traitement asynchrone...ok");
},delg);
while (!asr.IsCompleted)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Traitement en cours sur le thread principal");
System.Threading.Thread.Sleep(3000);
}

Console.Read();

Console.WriteLine("Q7 - ==============END=============");

// -------------------------------

Console.WriteLine("Main thread terminating...");

}
}
}
2 changes: 2 additions & 0 deletions concurency-workshop/concurency-workshop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CLpara.cs" />
<Compile Include="NS_CLIENT\CLclient.cs" />
<Compile Include="NS_SERVER\CLserver.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down

0 comments on commit 8bebce3

Please sign in to comment.