我有这段代码:
private void AnswerToCe(int currentBlock, int totalBlock = 0)
{
byte[] bufferToSend;
byte[] macDst = mac;
byte[] macSrc = ConnectionManager.SInstance.GetMyMAC();
byte[] ethType;
byte[] header;
if (Function == FlashFunction.UPLOAD_APPL || Function == FlashFunction.UPLOAD_BITSTREAM)
{
ethType = BitConverter.GetBytes((ushort)EthType.ETH_TYPE_UPLOAD);
ethType = new byte[] { ethType[1], ethType[0] };
header = Header.GetBytes((ushort)binaryBlocks.Count, (ushort)(currentBlock + 1), (ushort)binaryBlocks[currentBlock].Length);
int index = 0;
bufferToSend = new byte[macDst.Length + macSrc.Length + ethType.Length + header.Length + binaryBlocks[currentBlock].Length];
Array.Copy(macDst, 0, bufferToSend, index, macDst.Length);
index += macDst.Length;
Array.Copy(macSrc, 0, bufferToSend, index, macSrc.Length); …Run Code Online (Sandbox Code Playgroud) 在我的程序中我有~40个运行任务,定义如下:
private void StartTryReconnectTask() {
TryReconnectCTKS = new CancellationTokenSource();
TryReconnectTask = new Task(this.TryReconnect, TryReconnectCTKS.Token);
TryReconnectTask.Start();
}
Run Code Online (Sandbox Code Playgroud)
在TryReconnect()内部,有一个无限的while循环,只有在任务被取消时才会停止.这里的一切似乎都很好.
然后我需要在按钮点击上启动任务(不是无限):
private void ExecuteRepairCommand(object o) {
Task.Run(() => {
...
});
}
Run Code Online (Sandbox Code Playgroud)
启动这项新任务需要大约30/40秒.如果我使用线程一切正常,线程立即启动.为什么?是什么原因?