c# - 填写DataGridView时表格不动
问题描述
我使用 DataGridView 作为应用程序的控制台,我需要每秒填写一次,为此我使用以下代码:
public class dgView
{
public static DataGridView DataConsole;
public static void addLinha(int tipo_acao, string acao, string extra, int tipo_extra)
{
// INSERE LINHA NO CONSOLE
try
{
if (DataConsole == null) return;
var idLinha = DataConsole.Rows.Add();
using (var linha = DataConsole.Rows[idLinha])
{
//await Task.Delay(300);
if (tipo_acao == 0)
{
linha.DefaultCellStyle.ForeColor = Color.Teal;
linha.Cells[3].Style.ForeColor = Color.Gray;
}
else if (tipo_acao == 1)
{
linha.DefaultCellStyle.ForeColor = Color.Yellow;
linha.Cells[3].Style.ForeColor = Color.Orange;
}
else
{
linha.DefaultCellStyle.ForeColor = Color.Red;
linha.Cells[3].Style.ForeColor = Color.Magenta;
}
linha.Cells["dg_data"].Value = DateTime.Now;
linha.Cells["dg_mapa"].Value = "" + Config.Mapa + "";
linha.Cells["dg_acao"].Value = "" + Config.rm.GetString("" + acao + "") + "";
if (tipo_extra == 0)
{
linha.Cells["dg_extra"].Value = "" + extra + "";
}
else
{
linha.Cells["dg_extra"].Value = "" + Config.rm.GetString(extra) + "";
}
DataConsole.CurrentCell = DataConsole.Rows[idLinha].Cells[0];
//DataConsole.Refresh();
}
}
catch (Exception ex)
{
throw;
}
}
}
但是,表格冻结了,我无法将其移动到屏幕的任何部分,有什么办法可以解决这个问题吗?请记住,DataGridView 不是,也不能通过DataSource
属性填充,而是通过系统中的不断验证来填充。
例子:
public static void Abrir(string metodoChamador)
{
try
{
Abrir:
dgView.addLinha(2, "config_Abrir", "config_Validando", 1);
Thread.Sleep(5000);
Atalho:
string AtalhoExiste = "" + Directory.GetCurrentDirectory() + "\\" + Config.Atalho + ".lnk";
if (!File.Exists(AtalhoExiste))
{
dgView.addLinha(2, "config_FaltaIcone", "", 0);
Thread.Sleep(5000);
goto Atalho;
}
else
{
ProcessStartInfo info = new ProcessStartInfo(@"" + Directory.GetCurrentDirectory() + "\\" + Config.Atalho + ".lnk");
Process whatever = Process.Start(info);
whatever.Dispose();
Thread.Sleep(5000);
IntPtr WinHandle = Win32.FindWindow(null, Config.Atalho);
if (WinHandle == (IntPtr)0)
{
goto Abrir;
}
}
}
catch (Exception ex)
{
throw;
}
}
解决方案
很难判断代码中是否还有其他潜在问题。基本上在你拥有的任何地方Thread.Sleep()
,你的 GUI 都会冻结。
Abrir()
这是您的方法的可能重构,使用async\await
JQSOFTTask.Delay()
的建议:
public async static void Abrir(string metodoChamador)
{
try
{
IntPtr WinHandle = IntPtr.Zero;
do
{
dgView.addLinha(2, "config_Abrir", "config_Validando", 1);
await Task.Delay(5000);
string AtalhoExiste = "" + Directory.GetCurrentDirectory() + "\\" + Config.Atalho + ".lnk";
while (!File.Exists(AtalhoExiste))
{
dgView.addLinha(2, "config_FaltaIcone", "", 0);
await Task.Delay(5000);
}
ProcessStartInfo info = new ProcessStartInfo(@"" + Directory.GetCurrentDirectory() + "\\" + Config.Atalho + ".lnk");
Process whatever = Process.Start(info);
whatever.Dispose();
await Task.Delay(5000);
WinHandle = Win32.FindWindow(null, Config.Atalho);
}
while (WinHandle.Equals(IntPtr.Zero));
}
catch (Exception ex)
{
throw;
}
}
如果您Thread.Sleep()
在其他位置,则需要对其进行重构。如果您有任何其他使用 的无限循环goto
,则可能也需要对其进行重构。
推荐阅读
- c++ - 如何使用 OpenCV 检测给定二进制图像中的自定义形状?
- python - 如何使用python发布请求,指定正文
- php - 通过AJAX实时捕获来自php的帖子
- java - 两个 for 循环之间的长度差异如何影响性能?
- html - 我可以将文件从数据库分配给 html 文件输入吗
- python - 如何加载 UCI 卫星数据集?
- swagger - 42crunch Schema 没有为 boolean 定义 JSON 值的类型
- python - 获取请求未在 python 中加载
- python - 为什么 patplotlib 发送错误在主线程之外启动 Matplotlib GUI 可能会失败
- excel - 复制值直到下一列的最后一整行