【问题标题】:first time running application slow from dll第一次从 dll 运行应用程序缓慢
【发布时间】:2018-01-18 04:51:04
【问题描述】:

我是 C# 新手。

我想问的是我创建一个表单并将其编译成 dll 并尝试从另一个应用程序(从公司应用程序)调用它。

我想问的是,当我尝试第一次打开我的表单时,需要一段时间(比如 1-2 分钟)然后我关闭表单(不是应用程序)并重新尝试再次打开表单第二次它比第一次快得多。

但是如果我完全关闭应用程序并第一次重新打开我的表单需要一段时间(如 1-2分钟)。

对于dll本身,都是选择数据库。代码如下

public partial class Genre : Form
{
    SqlConnection myConnection = new SqlConnection(class.Conn);
    DataTable dt_main = new DataTable();

    Bitmap gbr_inf = new Bitmap(Properties.Resources.info_icon, 25, 25);
    Bitmap gbr_error = new Bitmap(Properties.Resources.close, 25, 25);

    RepositoryItemComboBox repositoryItemComboBox1 = new RepositoryItemComboBox();
    RepositoryItemComboBox repositoryItemComboBox2 = new RepositoryItemComboBox();


    public Genre()
    {            
        InitializeComponent();
        repositoryItemComboBox1.ButtonClick += RepositoryItemComboBox1_ButtonClick;
        repositoryItemComboBox2.ButtonClick += RepositoryItemComboBox2_ButtonClick;
    }

 private void Genre_Load(object sender, EventArgs e)
    {
        SDB();         
        fill_repo();
    }

 public void SDB()
    {
        SqlCommand command = new SqlCommand();
        SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            dt_main.Clear();
            myConnection.Open();

            command.Connection = myConnection;
            command.CommandText = "Select * from Genre with (nolock) order by code";                
            adapter.SelectCommand = command;
            adapter.Fill(dt_main);
            gridControl1.DataSource = dt_main;               
        }
        catch (Exception ex)
        {
            MessageBox.Show("error" + ex);
        }
        finally
        {
            myConnection.Close();
        }
    }

  public void fill_repo()
    {
        DataTable dtrepo = new DataTable();
        dtrepo.Clear();
        dtrepo = dt_main.Copy();

        for (int i = 0; i < dtrepo.Rows.Count; i++)
        {                                
            string code = dtrepo.Rows[i]["code"].ToString();
            string genre = dtrepo.Rows[i]["genre"].ToString();

            if (!repositoryItemComboBox1.Items.Contains(code))
            {
                repositoryItemComboBox1.Items.Add(code);
            }
            if (!repositoryItemComboBox2.Items.Contains(genre))
            {
                repositoryItemComboBox2.Items.Add(genre);
            }

        }
    }

 private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
    {           
        if (e.Column.FieldName == "code" && view.IsFilterRow(e.RowHandle))
        {
            e.RepositoryItem = repositoryItemComboBox1;
        }
        if (e.Column.FieldName == "genre" && view.IsFilterRow(e.RowHandle))
        {
            e.RepositoryItem = repositoryItemComboBox2;
        }          
    }
}

似乎是什么问题?

【问题讨论】:

  • 我没有看到 DevExpress 的链接。这就是 DLL 中的全部内容吗?第一次需要加载库的代码。如果有很多依赖项做某事,那将需要时间。但是超过一分钟?您是否尝试过逐行调试以查看发生了什么?
  • 如果您从数据库中选择记录,应用程序在第一次运行时运行时间较长是正常的;之后,记录已经在缓存中,因此可以在接下来的运行中更快地选择它们。
  • @SamiKuhmonen 好吧,对于导入,我不会在这篇文章中输入它。它有 13 个依赖项,主要是 devexpress。调试我没有看到任何错误
  • @SebastianHofmann 我知道。让我给你一个表格a和表格b的例子。与上面的表格a类似,表格b比表格a更有选择。假设我第一次打开应用程序并首先打开我的表单 A(需要 1 分钟)然后我打开我的表单 B(需要不到 1 分钟)。反之亦然。如果我第一次打开应用程序并首先打开我的表格 B(需要超过 1 分钟)然后我打开我的表格 A(需要不到 1 分钟)

标签: c# sql winforms


【解决方案1】:

当您拨打SDB时:

public void SDB()
{
    SqlCommand command = new SqlCommand();
    SqlDataAdapter adapter = new SqlDataAdapter();
    try
    {
        dt_main.Clear();
        myConnection.Open();

        command.Connection = myConnection;
        command.CommandText = "Select * from Genre with (nolock) order by code";                
        adapter.SelectCommand = command;
        adapter.Fill(dt_main);
        gridControl1.DataSource = dt_main;               
    }
    catch (Exception ex)
    {
        MessageBox.Show("error" + ex);
    }
    finally
    {
        myConnection.Close();
    }
}

您正在从 Genre 表中复制所有字段和所有行,并将它们放入 dt_main DataTable。这可能是一项昂贵的操作,具体取决于 Genre 表的大小。

如果您希望您的应用程序运行良好,您通常应该通过在任何给定时间只选择您感兴趣的字段和行来避免非常大的查询。在这种情况下,您只使用了codegenre 字段,因此您可以通过使用获得更好的性能

"Select code, genre with (nolock) order by code"

此外,您将在填充下拉列表之前再次复制整个表格:

dtrepo = dt_main.Copy();

这完全没有必要。

缓存数据也很重要,因此您不会在每次加载表单时都访问数据库。您正在这样做(偶然),但您确实应该为此制定更好的计划。

【讨论】:

  • 抱歉,我认为查询的影响不会超过此。因为记录本身不到100。从我的研究来看,是因为冷/热启动吗?你可以看到之前的评论,我试图模拟这个。
猜你喜欢
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多