首页 > 解决方案 > 填写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;
    }
}

标签: c#winformsdatagridview

解决方案


很难判断代码中是否还有其他潜在问题。基本上在你拥有的任何地方Thread.Sleep(),你的 GUI 都会冻结。

Abrir()这是您的方法的可能重构,使用async\awaitJQSOFTTask.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,则可能也需要对其进行重构。


推荐阅读