Last active
May 29, 2020 23:07
-
-
Save luiseduardohd/cd4800c59316681fc9afa2ab603b388d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Linq; | |
namespace TestConsole | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
var timer = new VeryLoadedTimer(); | |
var elapsedtime = new ElapsedTime(); | |
//time: 0 | |
var task1 = timer.ScheduleTask(2, () => { Console.WriteLine("Time is " + elapsedtime.GetTime() ); }); | |
timer.ScheduleTask(6, () => { Console.WriteLine("Time is " + elapsedtime.GetTime() ); }); | |
timer.ScheduleTask(4, () => { Console.WriteLine("Time is " + elapsedtime.GetTime() ); }); | |
//timer. | |
//Time is 2 | |
//Time is 4 | |
//Time is 6 | |
Console.ReadLine(); | |
} | |
} | |
class ElapsedTime | |
{ | |
private DateTime startDateTime; | |
public ElapsedTime() | |
{ | |
startDateTime = DateTime.Now; | |
} | |
public long GetTime() | |
{ | |
var now = DateTime.Now; | |
var elapsedTimeTimeSpan = now - startDateTime; | |
var elapsedTime = elapsedTimeTimeSpan.Seconds; | |
return elapsedTime; | |
} | |
} | |
//interface IEmbeddedTimerCallback | |
//{ | |
// Action Call(); | |
//} | |
interface IVeryLoadedTimer | |
{ | |
int ScheduleTask(int interval_seconds, Action callback); | |
void CancelScheduled(int id); | |
} | |
class VeryLoadedTimer : IVeryLoadedTimer | |
{ | |
private Dictionary<int, (Action, int, CancellationTokenSource,DateTime)> tasks= | |
new Dictionary<int, (Action, int, CancellationTokenSource, DateTime)>(); | |
private static object lockObject = new Object(); | |
public void SwapTask(int id, Action callback) | |
{ | |
int prevInterval; | |
DateTime prevDate; | |
lock (lockObject) | |
{ | |
prevInterval = tasks[id].Item2; | |
prevDate = tasks[id].Item4; | |
} | |
CancelScheduled(id); | |
lock (lockObject) | |
{ | |
var Cancellationtoken = new CancellationTokenSource(); | |
var prevExTime = prevDate.AddSeconds(prevInterval); | |
var currentTime = DateTime.Now; | |
if (currentTime < prevExTime) | |
{ | |
var newIntervalTimeSpan = prevExTime - currentTime; | |
var newInterval = newIntervalTimeSpan.Seconds; | |
tasks.Add(id, (callback, newInterval, Cancellationtoken, DateTime.Now)); | |
_ = Task.Run(async delegate | |
{ | |
await Task.Delay(newInterval * 1000); | |
callback.Invoke(); | |
}); | |
} | |
} | |
} | |
public int ScheduleTask(int interval, Action callback) | |
{ | |
int newId; | |
lock (lockObject) | |
{ | |
var cancellation = new CancellationTokenSource(); | |
newId = tasks.Keys.DefaultIfEmpty().Max() + 1; | |
tasks.Add( newId, (callback, interval, cancellation,new DateTime()) ); | |
_ = Task.Run(async delegate | |
{ | |
await Task.Delay(interval*1000); | |
callback.Invoke(); | |
}); | |
} | |
return newId; | |
} | |
public void CancelScheduled(int id) | |
{ | |
lock (lockObject) | |
{ | |
tasks[id].Item3.Cancel(); | |
tasks.Remove(id); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IEmbeddedTimerCallback { void Call(); }. | |
class VeryLoadedTimer { | |
int ScheduleTask(int interval_seconds, IEmbeddedTimerCallback callback); | |
void CancelScheduled(int id); | |
} | |
class VeryLoadedTimer { | |
private Dictionary<int,(IEmbeddedTimerCallback,int,Cancellationtoken)> tasks; | |
private static object lockObject = new Object(); | |
void swapTask(int id, IEmbeddedTimerCallbac callback ){ | |
var prevInterval = Task[id].Value.Item2; | |
var prevDate = Task[id].Value.Item4; | |
Cancel(id); | |
lock (lockObject) | |
{ | |
var Cancellationtoken = new cancellationToken(); | |
var prevExTime = prevDate.AddSeconds(interval); | |
var currentTime = new DateTime(); | |
if( currentTime < prevExTime ) | |
{ | |
var newInterval = prevExTime - currentTime; | |
task.Add( | |
new Tuple<IEmbeddedTimerCallback,int,Cancellationtoken> | |
( newId, (callback, newInterval, Cancellationtoken) | |
) | |
); | |
Timer.Run(callback.call, cancellation).Delay(newInterval); | |
} | |
} | |
} | |
int ScheduleTask(int interval_seconds, IEmbeddedTimerCallback callback) | |
{ | |
lock (lockObject) | |
{ | |
var cancel = new cancellationToken(); | |
var newId = task.Keys.Max() +1; | |
task.Add( | |
new Tuple<IEmbeddedTimerCallback,int,Cancellationtoken,DateTime> | |
( newId, (callback, interval, Cancellationtoken,new DateTime()) | |
) | |
); | |
Timer.Run(callback.call, cancellation).Delay(interval); | |
} | |
} | |
void CancelScheduled(int id) | |
{ | |
lock (lockObject) | |
{ | |
task[id].Value.Item3.Cancel(); | |
task.Remove(task[id]); | |
} | |
} | |
} | |
time: 0 | |
var timer = timer.ScheduleTask(2, () => { Console.WriteLine("Time is " + timer.GetTime().ToString()); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VeryLoadedTimer { | |
private Dictionary<int,(IEmbeddedTimerCallback,int,Cancellationtoken)> tasks; | |
private static object lockObject = new Object(); | |
int ScheduleTask(int interval_seconds, IEmbeddedTimerCallback callback) | |
{ | |
lock (lockObject) | |
{ | |
var cancel = new cancellationToken(); | |
var newId = task.Keys.Max() +1; | |
task.Add( | |
new Tuple<IEmbeddedTimerCallback,int,Cancellationtoken> | |
( newId, (callback, interval, Cancellationtoken) | |
) | |
); | |
Timer.Run(callback.call, cancellation).Delay(interval); | |
} | |
} | |
void CancelScheduled(int id) | |
{ | |
lock (lockObject) | |
{ | |
task[id].Value.Item3.Cancel(); | |
task.Remove(task[id]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code made for a call interview
I haven't tested yet