private async Task print(IPAddress ip, BmpInfo bmpInfo) { TcpClientInfo client = IPClientBmpMap[ip].TcpClientInfo; try { OnLog?.Invoke(ip, bmpInfo.bmp, $"正在尝试打印", LogLevel.Info); NetworkStream stream = client.Client.GetStream(); // 写入打印数据流 await stream.WriteAsync(bmpInfo.printingBytes, 0, bmpInfo.printingBytes.Length); lock (IPClientBmpMap) { client.IdleTime = 0; IPClientBmpMap[ip].Queue.Dequeue(); } OnLog?.Invoke(ip, bmpInfo?.bmp, "打印成功", LogLevel.Success); await prePrint(ip); } catch (Exception e) { OnLog?.Invoke(ip, null, $"写入数据发生错误 {e.Message}", LogLevel.Error); lock (IPClientBmpMap) { closeIP(ip); } OnLog?.Invoke(ip, null, "重新尝试连接", LogLevel.Info); await Connect(ip); } }
public async Task Print(IPAddress ip, Bitmap bmp, int colorDepth) { await Task.Run(async() => { BmpInfo bmpInfo = null; // 获得需要打印的图片数据 bmpInfo = new BmpInfo { bmp = bmp, printingBytes = await getPrintingBytes(bmp, colorDepth) }; lock (IPClientBmpMap) { addIP(ip); IPClientBmpMap[ip].Queue.Enqueue(bmpInfo); OnLog?.Invoke(ip, bmpInfo?.bmp, "已进入打印队列", LogLevel.Success); // 如果正在打印则不做任何操作, 队列中的数据会自动打印 if (IPClientBmpMap[ip].IsPrinting) { return; } IPClientBmpMap[ip].IsPrinting = true; } await prePrint(ip); }); }