Question: Rewrite using async/await for synchronization instead of using lock statement using C# public void Notify(object sender, StockNotification e) { lock (ToLock) { Console.WriteLine(brokerName.PadRight(11) + e.StockName.PadRight(11)
Rewrite using async/await for synchronization instead of using lock statement using C#
public void Notify(object sender, StockNotification e)
{
lock (ToLock)
{
Console.WriteLine(brokerName.PadRight(11) + e.StockName.PadRight(11) + e.CurrentValue.ToString().PadRight(11) + e.Changes.ToString().PadRight(11));
}
lock (ToLock)
{
string directory = "/Users/Desktop/StockMarket";
string filename = "stocksreport.txt";
string path = directory + filename;
bool FileExists = File.Exists(path);
if (!File.Exists(path))
{
using (StreamWriter writer = File.CreateText(path))
{
writer.WriteLine("Date".PadRight(11) + "ttStock".PadRight(11) + "Initial".PadRight(11) + "Current".PadRight(11));
writer.WriteLine(e.Date.PadRight(11) + e.StockName.PadRight(11) + e.InitialValue.ToString().PadRight(11) + e.CurrentValue.ToString().PadRight(11));
writer.Flush();
}
}
else if (File.Exists(path))
{
using (StreamWriter writer = File.AppendText(path))
{
writer.WriteLine(e.Date.PadRight(11) + e.StockName.PadRight(11) + e.InitialValue.ToString().PadRight(11) + e.CurrentValue.ToString().PadRight(11));
writer.Flush();
}
}
}
}
Step by Step Solution
3.25 Rating (163 Votes )
There are 3 Steps involved in it
To rewrite the given code using asyncawait for synchronization instead of using the lock statement we can use SemaphoreSlim which is wellsuited for th... View full answer
Get step-by-step solutions from verified subject matter experts
