回调函数给主线程传递参数

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



namespace WindowsFormsApp1
{
    delegate void ThreadCallBackDelegate(TestData testData);
    public struct Item
    {
        public string name;
        public int testCount;
        public int currentCount;
        public int status;

    };

    public struct TestData
    {
        public int id;
        public Item[] item;
    };
    public partial class Form1 : Form
    {
        public TestData testData;
        public Form1()
        {
            InitializeComponent();
            testData.item = new Item[2];
        }

        private void ThreadCallBack(TestData testData)
        {
            if (testData.id == -1)
            {
                this.label1.Text = "all pass";
                return;
            }
            if (testData.item[testData.id].status == 2)
            {
                this.label1.Text = testData.item[testData.id].name + " pass";
            }
            else
                this.label1.Text = ("CallBack:" + testData.item[testData.id].name+ testData.item[testData.id].currentCount);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Test test = new Test();
            int id = 0;
            testData.item[id].testCount = 3;
            testData.item[id].currentCount = 0;
            testData.item[id].status = 1;
            testData.item[id].name = "cs";
            testData.item[++id].testCount = 5;
            testData.item[id].currentCount = 0;
            testData.item[id].status = 1;
            testData.item[id].name = "reboot";
            test.testData = this.testData;
            test.callBack = ThreadCallBack;

            Thread t = new Thread(new ThreadStart(test.TestAll));
            t.Start();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace WindowsFormsApp1
{
    class Test
    {
        public ThreadCallBackDelegate callBack;
        public TestData testData;
        public void TestAll()
        {
            TestCs();
            TestReboot();
            testData.id = -1;
            callBack(testData);
        }
        public void TestCs()
        {
            int id = 0;
            testData.id = id;
            if (testData.item[id].status == 0) return;
            for (; testData.item[id].currentCount < testData.item[id].testCount; testData.item[id].currentCount++)
            {
                Thread.Sleep(1000);
                callBack(testData);
            }
            testData.item[id].status = 2;
            Thread.Sleep(500);
            callBack(testData);
        }
        public void TestReboot()
        {
            int id = 1;
            testData.id = id;
            if (testData.item[id].status == 0) return;
            for (; testData.item[id].currentCount < testData.item[id].testCount; testData.item[id].currentCount++)
            {
                Thread.Sleep(500);
                callBack(testData);
            }
            testData.item[id].status = 2;
            Thread.Sleep(500);
            callBack(testData);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值