Skip to content

Commit

Permalink
Q6 - Delegate Async: asynchronous delegate invocation the BeginInvoke…
Browse files Browse the repository at this point in the history
… method with callback procedures
  • Loading branch information
sikatikenmogne committed May 4, 2024
1 parent fbdcb02 commit 6825115
Show file tree
Hide file tree
Showing 8 changed files with 535 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.concurency-workshop/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

403 changes: 403 additions & 0 deletions .idea/.idea.concurency-workshop/.idea/dbnavigator.xml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .idea/.idea.concurency-workshop/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.concurency-workshop/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.concurency-workshop/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Repeat the exercise using a lambda expression instead of a class.

Modify the code to create a thread that takes an argument ("Hi") and displays it in the console using a lambda expression.

```C#
```csharp
Console.WriteLine("Q4 - =======Thread & Thread Param=======");
Console.WriteLine();

Expand Down Expand Up @@ -132,10 +132,60 @@ When the async result is completed, it will display "callback end" in red on the

Observe the provided code and understand its functionality.

```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NS_MAIN
{
class Program
{
private delegate void DELG(object o);
static void Main(string[] args)
{
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();
}
}
}
```

#### B. Display Observation:

Describe the expected output of the code.

![image.png](console-output.png)

#### C. Class Implementation:

Create the CLclient and CLserver classes to achieve the same output as observed in point B.
Expand Down
52 changes: 50 additions & 2 deletions concurency-workshop/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -15,6 +16,8 @@ internal class Program
delegate int MySecondDelegateMethod(int i2);
public delegate void ParameterizedDelegate(Object message);

public delegate void AsyncDelegate(object obj);

/// Q5 pool thread
public delegate void DelegateThreadFive(Object obj);

Expand Down Expand Up @@ -188,12 +191,57 @@ static void Main(string[] args)

// Wait for background threads to finish
mre.WaitOne(); // Block the main thread until the event is signaled


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

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

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

/// <summary>
/// Q6 - Delegate Async
/// </summary>

Console.WriteLine("Q6 - =======Delegate Async=======");

AsyncDelegate asyncDelegate = (obj) =>
{
string msg = obj.ToString();
for (int i = 0; i <= 9; i++)
{
Thread.Sleep(1000);
Console.WriteLine("Message :" + msg + " #" + (i + 1));
}
};

// BeginInvoke method
AsyncCallback callback = (IAsyncResult ar) =>
{
AsyncDelegate delegateInstance = (AsyncDelegate)((AsyncResult)ar).AsyncDelegate;
delegateInstance.EndInvoke(ar);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Callback completed");
Console.ResetColor();
};

// BeginInvoke with callback
IAsyncResult asyncResult = asyncDelegate.BeginInvoke("T1", callback, null);

// asyncResult.AsyncWaitHandle.WaitOne();
while (!asyncResult.IsCompleted)
{
Console.WriteLine("The main thread is waiting for the call back end");
Thread.Sleep(2000);
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Callback end");
Console.ResetColor();

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

Console.WriteLine("Main thread terminating...");
}
}
}
Binary file added console-output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6825115

Please sign in to comment.