zhy934453950
zhy934453950
2018-01-04 00:19

C#winfrom什么情况下定义一个delegate(不是事件)?业界规范是什么?

5
  • delegate
  • c#
  • winform

RT,我是每一项功能(方法函数)或者线程定义一个delegate,还是根据需要的形参不同而分别定义delegate,无关实现功能的分类。比如
按功能定义:
delegate void MultiFileProBarAddValueDelegate(); //定义一个委托
private void Add() {
if (this.InvokeRequired)
{
this.BeginInvoke(new MultiFileProBarAddValueDelegate(Add));

        }
        else
        {
            multiFileProgressBar.Value += 1;
        }
    }
    delegate void MultiFileProBarAdd1ValueDelegate(SQLiteDataReader dr,int temp);

private void MultiFileDataGridViewRowsCreate(SQLiteDataReader dr, int temp)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new MultiFileProBarAdd1ValueDelegate(MultiFileDataGridViewRowsCreate));
}
else
{
while (dr.Read())
{
/*
添加数据项
*/
}
}
delegate void MultiFileDeleteDelegate();
delegate void MultiFileReturnDelegate();
private void MultiFileReturn()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new MultiFileReturnDelegate(MultiFileReturn));

        }
        else
        {
            if (multiFileChoiceButton.Enabled == false)
            {
                multiFileChoiceButton.Enabled = true;
            }
            multiFileReturnButton.Visible = true;
            multiFileWorkingButton.Text = "开始扫描";
        }
    }

/**/
delegate void UniFileDelegate();
delegate void UniFileFillDelegate(SQLiteCommand commandInsert);
delegate void UniFileResultDelegate(SQLiteDataReader dr, List drList);
delegate void UniFileDeleteDelegate(List drList);

只按形参不同定义:
/*同样多文件*/
delegate void MultiFileDelegate();
private void Add() {
if (this.InvokeRequired)
{
this.BeginInvoke(new MultiFileDelegate(Add));

        }
        else
        {
            multiFileProgressBar.Value += 1;
        }
    }

    private void MultiFileChoiceButtonChange()
    {
        if (this.InvokeRequired) 
        {
            this.BeginInvoke(new MultiFileDelegate(MultiFileChoiceButtonChange));
        }
        else
        {
            multiFileChoiceButton.Enabled = false;
        }
    }

    private void MultiFileReturn()
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new MultiFileDelegate(MultiFileReturn));

        }
        else
        {
            if (multiFileChoiceButton.Enabled == false)
            {
                multiFileChoiceButton.Enabled = true;
            }
            multiFileReturnButton.Visible = true;
            multiFileWorkingButton.Text = "开始扫描";
        }
    }

private void MultiFileDataGridViewRowsCreate(SQLiteDataReader dr, int temp)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new MultiFile1Delegate(MultiFileDataGridViewRowsCreate));
}
else
{
while (dr.Read())
{
/*
添加数据项
*/
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

7条回答

为你推荐

换一换