注册 登录
编程论坛 C# 论坛

关于委托的问题请教

ilsoviet1917 发布于 2022-04-13 14:47, 1336 次点击
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProcessMethodCall
{
    public partial class Form1 : Form
    {
        private delegate void WriteTextBox(char ch);
        private WriteTextBox writeTextBox;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(checkBox1.Checked == true)
            {
                groupBox2.Text = "运行中...";
                groupBox2.Refresh();
                textBox1.Clear();
                textBox1.Refresh();
                writeTextBox = new WriteTextBox(WriTextBox1);
                WriTxt(writeTextBox);
                groupBox2.Text = "任务1";
                textBox3.Focus();
                textBox3.SelectAll();
            }

            if(checkBox2.Checked == true)
            {
                groupBox2.Refresh();
                groupBox3.Text = "运行中...";
                groupBox3.Refresh();
                textBox2.Clear();
                textBox2.Refresh();
                writeTextBox = new WriteTextBox(WriTextBox2);
                WriTxt(writeTextBox);
                groupBox3.Text = "任务2";
                textBox3.Focus();
                textBox3.SelectAll();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void WriTxt(WriteTextBox wMethod)
        {
            string strdata = textBox3.Text;
            for(int i = 0; i < strdata.Length; i++)
            {
                wMethod(strdata[i]);
                DateTime now = DateTime.Now;
                while(now.AddSeconds(1) > DateTime.Now) { }
            }
        }

        private void WriTextBox1(char ch)
        {
            textBox1.AppendText(ch + "\r");
        }

        private void WriTextBox2(char ch)
        {
            textBox2.AppendText(ch + "\r");
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }
    }
}


我理解的工作流程是否正确?
1.定义一个委托WriteTextBox,这个委托没有返回值,接受char类型参数。
2.创建一个writeTextBox委托实例。
3.WriTxt方法将WriteTextBox委托作为参数使用,
    这个方法把textBox3中的得到的字符串以单个字符的形式填充委托的参数。
4.委托调用WriTextBox1方法,把从WriTxt方法中的得到的字符串写入textBox1。

有两个问题:
1.按照流程应该是先获取字符,再把字符写入到textBox1里,为什么
writeTextBox = new WriteTextBox(WriTextBox1);

WriTxt(writeTextBox);
前面执行?

2.        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }
这句话没有任何用处,为什么删了会报错?怎么才能正常删除?
1 回复
#2
qq28895779662022-04-18 16:38
就是个来回取值显示么,跨个线程完事
CrossThreadCall.cs
程序代码:

using System.Threading;
using System.Windows.Forms;

public static class CrossThreadCall
{
    public static void CrossThreadCalls(this Control ctl, ThreadStart del)
    {
        if (del == null) return;
        if (ctl.InvokeRequired)
            ctl.Invoke(del, null);
        else
            del();
    }
}


比如调用,引用下上面的cs文件
程序代码:

this.CrossThreadCalls(() =>
                {
                    xxx=xxx
                    或
                    xxx.Text="xxxx";
                });

1