因此,只要应用程序正在运行或请求取消,我的应用程序几乎需要连续执行操作(每次运行之间暂停10秒左右).它需要做的工作可能需要30秒.
是否更好地使用System.Timers.Timer并使用AutoReset确保它在前一个"tick"完成之前不执行操作.
或者我应该在LongRunning模式下使用带有取消令牌的常规任务,并且在其内部有一个常规的无限while循环调用在调用之间使用10秒Thread.Sleep执行工作的操作?至于async/await模型,我不确定它在这里是否合适,因为我没有任何工作的返回值.
CancellationTokenSource wtoken;
Task task;
void StopWork()
{
wtoken.Cancel();
try
{
task.Wait();
} catch(AggregateException) { }
}
void StartWork()
{
wtoken = new CancellationTokenSource();
task = Task.Factory.StartNew(() =>
{
while (true)
{
wtoken.Token.ThrowIfCancellationRequested();
DoWork();
Thread.Sleep(10000);
}
}, wtoken, TaskCreationOptions.LongRunning);
}
void DoWork()
{
// Some work that takes up to 30 seconds but isn't returning anything.
}
Run Code Online (Sandbox Code Playgroud)
或者只是在使用AutoReset属性时使用简单的计时器,并调用.Stop()取消它?
所以我有一个字符串数组,所有字符串都使用系统默认的ANSI编码,并从sql数据库中提取.因此,有256种不同的可能字符字节值(单字节编码).有没有办法让json_encode()工作并显示这些字符而不必在我的所有字符串上使用utf8_encode()并最终得到像"\ u0082"这样的东西?
或者这是json的标准?
我有一个应用程序,它同时生成几百个TCP连接,并从它们接收一个恒定的数据流.
private void startReceive()
{
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.Completed += receiveCompleted;
e.SetBuffer(new byte[1024], 0, 1024);
if (!Socket.ReceiveAsync(e)) { receiveCompleted(this, e); }
}
void receiveCompleted(object sender, SocketAsyncEventArgs e)
{
ProcessData(e);
if (!Socket.ReceiveAsync(e)) { receiveCompleted(this, e); }
}
Run Code Online (Sandbox Code Playgroud)
我的尝试导致了这样的事情:
private async void StartReceive()
{
byte[] Buff = new byte[1024];
int recv = 0;
while (Socket.Connected)
{
recv = await NetworkStream.ReadAsync(Buff, 0, 1024);
ProcessData(Buff,recv);
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是方法调用StartReceive()会阻塞,而不是到达随附的StartSend() method called afterStartReceive(). Creating a new task forStartReceive() …
我有一个包含三列的mysql表:用户名,位置,时间戳.这基本上是用户活动的日志,记录了他们所处的位置以及他们在那里的时间.
我想要做的是选择一个不同的用户名+位置,其中只提供最新的项目(按时间戳).
所以说表包括:
tom roomone 2011-3-25 10:45:00
tom roomtwo 2011-3-25 09:00:00
tom roomtwo 2011-3-25 08:30:00
pam roomone 3011-3-25 07:20:23
Run Code Online (Sandbox Code Playgroud)
我希望只选择这些:
tom roomone 2011-3-25 10:45:00
tom roomtwo 2011-3-25 09:00:00
Run Code Online (Sandbox Code Playgroud) 是否有一种简单的方法可以基本上只使用此方法获取数据的副本而不是引用?我试过.ToArray().Where()但似乎仍然传递了一个引用.
例:
static void Main(string[] args)
{
List<ob> t = new List<ob>();
t.Add(new ob() { name = "hello" });
t.Add(new ob() { name = "test" });
ob item = t.Where(c => c.name == "hello").First();
// Changing the name of the item changes the original item in the list<>
item.name = "burp";
foreach (ob i in t)
{
Console.WriteLine(i.name);
}
Console.ReadLine();
}
public class ob
{
public string name;
}
Run Code Online (Sandbox Code Playgroud) 好的,所以我有一个 .net exe,当我输入mono myexe.exe. 但是,如果我想使用另一个命令或关闭终端窗口,应用程序将停止执行。
我试过使用mono myexe.exe &它运行并显示 [8] 20078 等,但是一旦我输入其他内容,它就会显示 [8]+ Stopped,然后执行我输入的命令。
有任何想法吗?
我目前有一个应用程序正在从套接字接收数据包,处理它们并将它们添加到ConcurrentQueue.然后我有一个处理这些项目的单独线程.
我遇到的问题是生产者/消费者问题,即使没有任何项目,消费者也试图获取物品,从而导致显着的CPU使用率.
ProcessPackets在自己的线程上运行:
private ConcurrentQueue<PrimaryPacket> Waiting = new ConcurrentQueue<PrimaryPacket>();
private void ProcessPackets()
{
PrimaryPacket e;
while (true)
{
if (Waiting.TryDequeue(out e))
{
Packets.TryAdd(((ulong)e.IPAddress << 32 | e.RequestID), e);
}
}
}
public void AddPacket(PrimaryPacket e)
{
Waiting.Enqueue(e);
}
Run Code Online (Sandbox Code Playgroud)

实现BlockingCollection(T)来处理这个问题的最佳方法是什么?或另一种解决方案
另外值得注意的是,每秒大约有30,000个项目被添加到队列中.
我有一个这种结构的表,它目前包含约160万条记录.
CREATE TABLE `chatindex` (
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`roomname` varchar(90) COLLATE utf8_bin NOT NULL,
`username` varchar(60) COLLATE utf8_bin NOT NULL,
`filecount` int(10) unsigned NOT NULL,
`connection` int(2) unsigned NOT NULL,
`primaryip` int(10) unsigned NOT NULL,
`primaryport` int(2) unsigned NOT NULL,
`rank` int(1) NOT NULL,
`hashcode` varchar(12) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`timestamp`,`roomname`,`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Run Code Online (Sandbox Code Playgroud)
房间名称和用户名列都可以包含相同的确切数据,但每个项目的唯一性和重要位来自于将时间戳与这两个项目组合在一起.
开始需要一段时间(10-20秒)的查询是这样的:
SELECT timestamp,roomname,username,primaryip,primaryport
FROM `chatindex`
WHERE username LIKE '%partialusername%'
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能优化这个?我不能这样做,partialusername%因为对于某些查询,我只会有一小部分实际用户名的中心,而不是实际值开头的前几个字符.
编辑:
另外,对于这个特殊目的,狮身人面像会更好吗?