找回密码
 立即注册
回调SetProgressBarcallbackValueCallBack | 软件设计/软件工程 2022-05-02 227 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
1.什么是回调函数

回调函数(callbackFunction),顾名思义,就是用于回调函数的。回调函数只是一个功能片段,是用户按照回调函数调用约定实现的函数。

2.C#回调函数实现机制

(1) 定义一个回调函数;

(2) 提供函数实现的一方在初始化时将回调函数的实现函数委托给调用者;

(3) 当特定的事件或条件发生时,调用者使用委托调用回调函数来处理事件。

3、回调机制的应用

使用回调机制,可以为工作流实现扩展。工作流中需要用户干预或需要提供给用户的数据可以通过回调的方式提供给用户。用户不需要知道整个工作流,只需要知道回调函数的描述就可以使用工作流模块提供的功能,这对于信息隐藏也很有用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace CSharp_002_回调机制
{
    public partial class frmMain : Form
    {
        //定义回调
        private delegate void SetProgressBar2ValueCallBack(int value);
        //声明回调
        private SetProgressBar2ValueCallBack setProgressBar2ValueCallBack;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            //初始化回调
            setProgressBar2ValueCallBack = new SetProgressBar2ValueCallBack(SetProgressBar2ValueMethod);


            Thread progressBar2Thread = new Thread(SetProgressBar2Value);
            progressBar2Thread.Start();

            for (int i = 0; i <= 100; i++)
            {
                Application.DoEvents();
                Thread.Sleep(50);
                pgProgressBar1.Value = i;
            }
        }

        //设置进度条2的值 的线程
        private void SetProgressBar2Value()
        {
            for (int i = 0; i <= 100;i++ )
            {
                Thread.Sleep(50);
                pgProgressBar2.Invoke(setProgressBar2ValueCallBack, i);
            }
        }
        //设置进度条2的值 被委托的方法
        private void SetProgressBar2ValueMethod(int value)
        {
            pgProgressBar2.Value = value;
        }

    }//end class frmMain
}

(1. What is a callback function
Callback function, as the name suggests, is used for callback functions. The callback function is just a function fragment, which is implemented by the user according to the callback function calling convention.
2. Implementation mechanism of c# callback function
(1) Define a callback function;
(2) The party providing the function implementation delegates the implementation function of the callback function to the caller during initialization;
(3) When a specific event or condition occurs, the caller uses the delegate to call the callback function to handle the event.
3. Application of callback mechanism
Using the callback mechanism, you can extend the workflow. The data that needs user intervention or needs to be provided to users in the workflow can be provided to users through callback. Users do not need to know the whole workflow, but only need to know the description of the callback function to use the functions provided by the workflow module, which is also very useful for information hiding.
using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Data;
using System. Drawing;
using System. Text;
using System. Windows. Forms;
using System. Threading;
namespace CSharp_ 002_ Callback mechanism
{
public partial class frmMain : Form
{
//Define callback
private delegate void SetProgressBar2ValueCallBack(int value);
//Callback declaration
private SetProgressBar2ValueCallBack setProgressBar2ValueCallBack;
public frmMain()
{
InitializeComponent();
}
private void btnStart_ Click(object sender, EventArgs e)
{
//Initialize callback
setProgressBar2ValueCallBack = new SetProgressBar2ValueCallBack(SetProgressBar2ValueMethod);
Thread progressBar2Thread = new Thread(SetProgressBar2Value);
progressBar2Thread. Start();
for (int i = 0; i <= 100; i++)
{
Application. DoEvents();
Thread. Sleep(50);
pgProgressBar1. Value = i;
}
}
//Thread that sets the value of progress bar 2
private void SetProgressBar2Value()
{
for (int i = 0; i <= 100;i++ )
{
Thread. Sleep(50);
pgProgressBar2. Invoke(setProgressBar2ValueCallBack, i);
}
}
//Method of setting the value of progress bar 2 to be delegated
private void SetProgressBar2ValueMethod(int value)
{
pgProgressBar2. Value = value;
}
}//end class frmMain
}
)





上一篇:PHP扩展APC参数优化
下一篇:关于调试WINDOWS SERVICES的方法