C# 自定义线程类

自定义线程类,扩展了C#原生Thread没有停止线程的方法

MyThread.cs

using System;
using System.Threading;

/// <summary>
///自定义线程类,扩展了C#原生Thread没有停止线程的方法
/// </summary>
public class MyThread
{
    /// <summary>
    /// 表示线程是否运行的标志
    /// </summary>
    public volatile bool isRunning; 
    private Thread thread; // 内部的线程对象
    private Delegate function; // 线程执行的方法
    private object[] parameters; // 方法的参数
    private object result; // 线程执行的返回值

    public MyThread()
    {
        this.isRunning = false;
        this.thread = null;
        this.function = null;
        this.parameters = null;
        this.result = null;
    }

    /// <summary>
    /// 绑定无参数无返回值的委托
    /// </summary>
    /// <param name="action"></param>
    public void Bind(Action<MyThread> action)
    {
        this.function = action;
        this.parameters = null;
    }

    /// <summary>
    /// 绑定有返回值的委托
    /// </summary>
    /// <param name="function"></param>
    public void Bind(Func<MyThread, object> function)
    {
        this.function = function;
        this.parameters = null;
    }

    /// <summary>
    /// 绑定有参数无返回值的委托
    /// </summary>
    /// <param name="action"></param>
    /// <param name="parameters"></param>
    public void Bind(Action<MyThread, object[]> action, params object[] parameters)
    {
        this.function = action;
        this.parameters = parameters;
    }

    /// <summary>
    /// 绑定有参数有返回值的委托
    /// </summary>
    /// <param name="function"></param>
    /// <param name="parameters"></param>
    public void Bind(Func<MyThread, object[], object> function, params object[] parameters)
    {
        this.function = function;
        this.parameters = parameters;
    }

    /// <summary>
    /// 启动线程
    /// </summary>
    public void Start()
    {
        if (this.function == null) return;
        if (this.thread == null || !this.thread.IsAlive)
        {
            this.isRunning = true;
            if (this.function is Action<MyThread>)
            {
                this.thread = new Thread(() => {
                    ((Action<MyThread>)this.function)(this);
                    this.isRunning = false;
                });
            }
            else if (this.function is Func<MyThread, object>)
            {
                this.thread = new Thread(() => {
                    this.result = ((Func<MyThread, object>)this.function)(this);
                    this.isRunning = false;
                });
            }
            else if (this.function is Action<MyThread, object[]>)
            {
                this.thread = new Thread(() => {
                    ((Action<MyThread, object[]>)this.function)(this, this.parameters);
                    this.isRunning = false;
                });
            }
            else if (this.function is Func<MyThread, object[], object>)
            {
                this.thread = new Thread(() => {
                    this.result = ((Func<MyThread, object[], object>)this.function)(this, this.parameters);
                    this.isRunning = false;
                });
            }
            this.thread.Start();
        }
    }

    /// <summary>
    /// 停止线程
    /// </summary>
    public void Stop()
    {
        if (this.isRunning)
        {
            this.isRunning = false;
            //等待线程结束
            this.thread.Join();
        }
    }

    /// <summary>
    /// 获取线程执行的返回值
    /// </summary>
    public object Result
    {
        get
        {
            return this.result;
        }
    }

}

/// <summary>
/// MyThread调用例子
/// </summary>
public class MyThreadExample
{
    public static MyThread thread1;

    /// <summary>
    /// 不带返回值调用例子
    /// </summary>
    public static void Test1()
    {

        thread1 = new MyThread();
        thread1.Bind(PrintMessage, "Hello, World!");
        thread1.Start();
    }
    //停止任务
    public static void button15_Click()
    {
        thread1.Stop();
    }

    /// <summary>
    /// 模拟任务一直在循环执行
    /// </summary>
    /// <param name="myThread"></param>
    /// <param name="parameters"></param>
    static void PrintMessage(MyThread myThread, object[] parameters)
    {
        string message = (string)parameters[0];
        while (myThread.isRunning)
        {
            Console.WriteLine(message);
            System.Threading.Thread.Sleep(300);
        }
    }


    /// <summary>
    /// 带返回值调用例子
    /// </summary>
    public static void Test2()
    {
        var myThread = new MyThread();
        myThread.Bind(Add, 1, 2);
        myThread.Start();
        Console.WriteLine("线程已启动");

        while (myThread.isRunning)
        {
            // 等待线程执行完成
        }

        Console.WriteLine($"计算结果为:{myThread.Result}");
        myThread.Stop();
        Console.WriteLine("线程已停止");
    }

    private static object Add(MyThread myThread, object[] parameters)
    {
        int num1 = (int)parameters[0];
        int num2 = (int)parameters[1];
        int result = num1 + num2;

        // 模拟耗时操作
        Thread.Sleep(3000);

        return result;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值