c# 使用console.writeline居然遇到阻塞

udp server里接收数据里的一段代码

          while (true)
            {               
               if (cts) break;
               byte[] data = new byte[1024];
               int recv = server.ReceiveFrom(data,SocketFlags.None, ref remote);
              Console.WriteLine("data:" + Encoding.Default.GetString(data));
            }

发现用客户端发数据,只打印一次再也不打印了,最后发现是卡在Console.writeline;环境是.net3.5;

改用Trace.Writeline就不阻塞;以前没遇到过这种情况。老老实实用Trace吧。

using System; using System.IO; namespace CD_OVL { public class FileProcessor { public string[] ProcessFile(string filePath) { string[] data = new string[13]; // 创建数据数组 string extension = Path.GetExtension(filePath).ToLower(); // 获取文件扩展名 string fileName = Path.GetFileName(filePath); // 获取文件名 if (extension == ".txt") // 如果是TXT文件 { data[0] = fileName.Substring(0, Math.Min(8, fileName.Length)); // 提取文件名前8个字符 ProcessTxtFile(filePath, ref data); // 处理TXT文件 } else if (extension == ".csv") // 如果是CSV文件 { if (fileName.Length >= 24) // 如果文件名长度大于等于24 { data[0] = fileName.Substring(16, 8); // 提取文件名第16到24个字符 } ProcessCsvFile(filePath, ref data); // 处理CSV文件 } return data; } private void ProcessTxtFile(string filePath, ref string[] data) { try { string[] lines = File.ReadAllLines(filePath); // 读取文件的所有行 bool found4PP = false; // 是否找到4PP Recipe bool foundThickness = false; // 是否找到Thickness Recipe bool foundTransmissivity = false; // 是否找到Transmissivity Recipe foreach (string line in lines) { Console.WriteLine($"Processing line: {line}"); // 添加日志记录 // 忽略空行和注释行 if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";")) { continue; } string[] columns = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // 分割每一行 if (columns.Length < 2) // 如果列数小于2,跳过该行 { Console.WriteLine("Skipping line due to insufficient columns."); continue; } string key = columns[0].Trim(); string value = columns[1].Trim(); switch (key.ToLower()) { case "4pp recipe": found4PP = true; Console.WriteLine("Found 4PP Recipe"); break; case "average" when found4PP: data[1] = value; Console.WriteLine($"Set RS AVERAGE to {data[1]}"); break; case "uniformity" when found4PP: data[2] = value.Replace("%", ""); Console.WriteLine($"Set RS UNIF to '{data[2]}' from original value '{value}'"); break; case "max" when found4PP: data[3] = value; Console.WriteLine($"Set RS MAX to {data[3]}"); break; case "min" when found4PP: data[4] = value; Console.WriteLine($"Set RS MIN to {data[4]}"); found4PP = false; break; case "thickness recipe": foundThickness = true; Console.WriteLine("Found Thickness Recipe"); break; case "average" when foundThickness: data[5] = value; Console.WriteLine($"Set TH AVERAGE to {data[5]}"); break; case "uniformity" when foundThickness: data[6] = value.Replace("%", ""); Console.WriteLine($"Set TH UNIF to '{data[6]}' from original value '{value}'"); break; case "max" when foundThickness: data[7] = value; Console.WriteLine($"Set TH MAX to {data[7]}"); break; case "min" when foundThickness: data[8] = value; Console.WriteLine($"Set TH MIN to {data[8]}"); foundThickness = false; break; case "transmissivity recipe": foundTransmissivity = true; Console.WriteLine("Found Transmissivity Recipe"); break; case "transmissivity2 ave" when foundTransmissivity: data[9] = value; Console.WriteLine($"Set TR AVERAGE to {data[9]}"); break; case "transmissivity2 uni" when foundTransmissivity: data[10] = value.Replace("%", ""); Console.WriteLine($"Set TR UNIF to '{data[10]}' from original value '{value}'"); break; case "transmissivity2 max" when foundTransmissivity: data[11] = value; Console.WriteLine($"Set TR MAX to {data[11]}"); break; case "transmissivity2 min" when foundTransmissivity: data[12] = value; Console.WriteLine($"Set TR MIN to {data[12]}"); foundTransmissivity = false; break; default: Console.WriteLine($"Unknown key encountered: {key}"); break; } } } catch (Exception ex) { Console.WriteLine("处理TXT文件时发生错误: " + ex.Message); // 显示错误信息 } } private void ProcessCsvFile(string filePath, ref string[] data) { try { string[] lines = File.ReadAllLines(filePath); // 读取文件的所有行 bool found4PP = false; // 是否找到4PP Recipe bool found2PP = false; // 是否找到2PP Recipe foreach (string line in lines) { Console.WriteLine($"Processing line: {line}"); // 添加日志记录 // 忽略空行和注释行 if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";")) { continue; } string[] columns = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // 分割每一行 if (columns.Length < 2) // 如果列数小于2,跳过该行 { Console.WriteLine("Skipping line due to insufficient columns."); continue; } string key = columns[0].Trim().ToLower(); string value = columns[1].Trim(); switch (key) { case "4pp recipe": found4PP = true; Console.WriteLine("Found 4PP Recipe"); break; case "average" when found4PP: data[1] = value; Console.WriteLine($"Set RS AVERAGE to {data[1]}"); break; case "uniformity" when found4PP: data[2] = value.Replace("%", ""); Console.WriteLine($"Set RS UNIF to '{data[2]}' from original value '{value}'"); break; case "max" when found4PP: data[3] = value; Console.WriteLine($"Set RS MAX to {data[3]}"); break; case "min" when found4PP: data[4] = value; Console.WriteLine($"Set RS MIN to {data[4]}"); found4PP = false; break; case "2pp recipe": found2PP = true; Console.WriteLine("Found 2PP Recipe"); break; case "d average" when found2PP: data[1] = value; Console.WriteLine($"Set RS AVERAGE to {data[1]}"); break; case "d uniformity" when found2PP: data[2] = value.Replace("%", ""); Console.WriteLine($"Set RS UNIF to '{data[2]}' from original value '{value}'"); break; case "d max" when found2PP: data[3] = value; Console.WriteLine($"Set RS MAX to {data[3]}"); break; case "d min" when found2PP: data[4] = value; Console.WriteLine($"Set RS MIN to {data[4]}"); found2PP = false; break; default: Console.WriteLine($"Unknown key encountered: {key}"); break; } } } catch (Exception ex) { Console.WriteLine("处理CSV文件时发生错误: " + ex.Message); // 显示错误信息 } } } }
最新发布
05-14
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值