C# JavaScript队列函数在C中的等价性#
我有一个C# JavaScript队列函数在C中的等价性#,c#,javascript,delayed-execution,C#,Javascript,Delayed Execution,我有一个JavaScript代码,我想把这个想法带到我的C#项目中。 此代码将多个函数调用合并到单个管理延迟调用中 以下是JavaScript代码: 以下是一个粗略的翻译,让您开始学习: var worker = new Action<IEnumerable<string>>(collection => { Console.WriteLine(string.Join(" ", collection)); }); var args = new List<
JavaScript
代码,我想把这个想法带到我的C#
项目中。此代码将多个函数调用合并到单个管理延迟调用中 以下是
JavaScript
代码:
以下是一个粗略的翻译,让您开始学习:
var worker = new Action<IEnumerable<string>>(collection =>
{
Console.WriteLine(string.Join(" ", collection));
});
var args = new List<string>();
var timer = new Timer(state =>
{
worker(args);
//Reset args
args.Clear();
});
var queueWorkRunOnce = new Action<string>(arg =>
{
args.Add(arg);
timer.Change(/*Delay in ms*/ 1000, Timeout.Infinite);
});
queueWorkRunOnce("Hi");
//Some other stuff
queueWorkRunOnce("There");
//Some other stuff
queueWorkRunOnce("Hello");
queueWorkRunOnce("World");
Console.ReadKey();
var worker=新操作(集合=>
{
Console.WriteLine(string.Join(“,collection));
});
var args=新列表();
变量计时器=新计时器(状态=>
{
工人(args);
//重置参数
args.Clear();
});
var queueWorkRunOnce=新操作(arg=>
{
args.Add(arg);
timer.Change(/*毫秒延迟*/1000,超时无限);
});
排队运行(“Hi”);
//一些其他的东西
排队工作区(“那里”);
//一些其他的东西
queueWorkRunOnce(“你好”);
“世界”;
Console.ReadKey();
使此线程安全留给读者作为练习。这里修改了相同的代码。。为我工作
如果有人能发明一个线程安全的版本,那就太棒了
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace QueueWorkRunOnce {
public class Test {
public static void worker(List<string> collection) {
Console.WriteLine(string.Join(" ", collection.ToArray()));
}
public static List<string> args = new List<string>();
public Timer timer = new Timer(state => {
worker(args);
args.Clear();
});
public void queueWorkRunOnce(string arg){
args.Add(arg);
timer.Change(/*Delay in ms*/ 1000, Timeout.Infinite);
}
public Test() {
Console.WriteLine("new Test");
queueWorkRunOnce("Hi");
//Some other stuff
queueWorkRunOnce("There");
//Some other stuff
queueWorkRunOnce("Hello");
queueWorkRunOnce("World");
}
}
class Program {
static void Main(string[] args) {
new Test();
Thread.Sleep(3000);
new Test();
Console.ReadKey();
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用系统线程;
命名空间队列运行{
公开课考试{
公共静态无效工作程序(列表集合){
Console.WriteLine(string.Join(“,collection.ToArray());
}
public static List args=new List();
公共计时器=新计时器(状态=>{
工人(args);
args.Clear();
});
public void queueworkrunance(字符串arg){
args.Add(arg);
timer.Change(/*毫秒延迟*/1000,超时无限);
}
公开考试(){
控制台写入线(“新测试”);
排队运行(“Hi”);
//一些其他的东西
排队工作区(“那里”);
//一些其他的东西
queueWorkRunOnce(“你好”);
“世界”;
}
}
班级计划{
静态void Main(字符串[]参数){
新测试();
睡眠(3000);
新测试();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace QueueWorkRunOnce {
public class Test {
public static void worker(List<string> collection) {
Console.WriteLine(string.Join(" ", collection.ToArray()));
}
public static List<string> args = new List<string>();
public Timer timer = new Timer(state => {
worker(args);
args.Clear();
});
public void queueWorkRunOnce(string arg){
args.Add(arg);
timer.Change(/*Delay in ms*/ 1000, Timeout.Infinite);
}
public Test() {
Console.WriteLine("new Test");
queueWorkRunOnce("Hi");
//Some other stuff
queueWorkRunOnce("There");
//Some other stuff
queueWorkRunOnce("Hello");
queueWorkRunOnce("World");
}
}
class Program {
static void Main(string[] args) {
new Test();
Thread.Sleep(3000);
new Test();
Console.ReadKey();
}
}