创建一个以开始日期作为输入参数并提前 24 小时的事件
Create an event with start date as an input parameter and 24h forward
我正在使用 C#,我想创建一个事件,在字段填满 24 小时后向用户显示一个字段。这意味着当字段填满时,我必须等待 24 小时才能将其显示给用户。
有什么简单的方法吗?
是的,我没有写任何代码,因为我在网上找到了建议计时器的示例,它与我的问题不相似。
我正在使用 .NET 3.5
此示例代码(控制台应用程序)演示了 属性 值更改时,然后在 24 小时后调用方法显示数据:
public class Program
{
private CancellationTokenSource tokenSource;
private string _myProperty;
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
_ = Run(() => SendToUser(_myProperty), TimeSpan.FromHours(24), tokenSource.Token);
}
}
private static void Main()
{
var prg = new Program();
prg.tokenSource = new CancellationTokenSource();
prg.MyProperty = "Test test";
Console.ReadLine();
}
private void SendToUser(string data)
{
//Display data for user
Console.WriteLine(data);
}
public static async Task Run(Action action, TimeSpan period, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(period, cancellationToken);
if (!cancellationToken.IsCancellationRequested)
action();
}
}
public static Task Run(Action action, TimeSpan period)
{
return Run(action, period, CancellationToken.None);
}
}
在 .NET 3.5 中:
public class Program
{
private string _myProperty;
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
ExecuteAfter(() => SendToUser(_myProperty), TimeSpan.FromHours(24));
}
}
private static void Main()
{
var prg = new Program();
prg.MyProperty = "Test test";
Console.ReadLine();
}
private void SendToUser(string data)
{
//Display data for user
Console.WriteLine(data);
}
public static void ExecuteAfter(Action action, TimeSpan delay)
{
Timer timer = null;
timer = new System.Threading.Timer(s =>
{
action();
timer.Dispose();
}, null, (long)delay.TotalMilliseconds, Timeout.Infinite);
}
}
或使用 FluentScheduler - 准备好的 nuget 任务调度程序包,https://www.nuget.org/packages/FluentScheduler/3.1.46
我正在使用 C#,我想创建一个事件,在字段填满 24 小时后向用户显示一个字段。这意味着当字段填满时,我必须等待 24 小时才能将其显示给用户。
有什么简单的方法吗?
是的,我没有写任何代码,因为我在网上找到了建议计时器的示例,它与我的问题不相似。
我正在使用 .NET 3.5
此示例代码(控制台应用程序)演示了 属性 值更改时,然后在 24 小时后调用方法显示数据:
public class Program
{
private CancellationTokenSource tokenSource;
private string _myProperty;
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
_ = Run(() => SendToUser(_myProperty), TimeSpan.FromHours(24), tokenSource.Token);
}
}
private static void Main()
{
var prg = new Program();
prg.tokenSource = new CancellationTokenSource();
prg.MyProperty = "Test test";
Console.ReadLine();
}
private void SendToUser(string data)
{
//Display data for user
Console.WriteLine(data);
}
public static async Task Run(Action action, TimeSpan period, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(period, cancellationToken);
if (!cancellationToken.IsCancellationRequested)
action();
}
}
public static Task Run(Action action, TimeSpan period)
{
return Run(action, period, CancellationToken.None);
}
}
在 .NET 3.5 中:
public class Program
{
private string _myProperty;
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
ExecuteAfter(() => SendToUser(_myProperty), TimeSpan.FromHours(24));
}
}
private static void Main()
{
var prg = new Program();
prg.MyProperty = "Test test";
Console.ReadLine();
}
private void SendToUser(string data)
{
//Display data for user
Console.WriteLine(data);
}
public static void ExecuteAfter(Action action, TimeSpan delay)
{
Timer timer = null;
timer = new System.Threading.Timer(s =>
{
action();
timer.Dispose();
}, null, (long)delay.TotalMilliseconds, Timeout.Infinite);
}
}
或使用 FluentScheduler - 准备好的 nuget 任务调度程序包,https://www.nuget.org/packages/FluentScheduler/3.1.46