博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
子线程执行完后通知主线程
阅读量:6475 次
发布时间:2019-06-23

本文共 1933 字,大约阅读时间需要 6 分钟。

Here is a simple demo by using delegate.

class Program{    public delegate void Entrust();    static void Main(string[] args)    {        Entrust callback = new Entrust(Timer1Complete);         Thread th = new Thread(ProcessTimer1);        th.IsBackground = true;        th.Start(callback);        // time-consuming operations in main thread        for (int i = 1;i< 10;i++)        {            Console.WriteLine("main {0}", i);            Thread.Sleep(500);        }        // time-consuming operations in main thread        Console.ReadKey();    }    private static void ProcessTimer1(object obj)    {        // time-consuming operations in sub thread        for (int i = 1; i <= 5; i++)        {            Console.WriteLine("sub {0}", i);            Thread.Sleep(500);        }        // time-consuming operations in sub thread        Entrust callback = obj as Entrust;        callback();    }    private static void Timer1Complete()    {        Console.WriteLine("cause the Timer1Complete method to execute");    }}
Class SurroundingClass    Public Delegate Sub Entrust()    Private Shared Sub Main(ByVal args As String())        Dim callback As Entrust = New Entrust(AddressOf Timer1Complete)        Dim th As Thread = New Thread(AddressOf ProcessTimer1)        th.IsBackground = True        th.Start(callback)        For i As Integer = 1 To 10            Console.WriteLine("main {0}", i)            Thread.Sleep(500)        Next        Console.ReadKey()    End Sub    Private Shared Sub ProcessTimer1(ByVal obj As Object)        For i As Integer = 1 To 5            Console.WriteLine("sub {0}", i)            Thread.Sleep(500)        Next        Dim callback As Entrust = TryCast(obj, Entrust)        callback()    End Sub    Private Shared Sub Timer1Complete()        Console.WriteLine("cause the Timer1Complete method to execute")    End SubEnd ClassIn VB.NET
View Code

转载于:https://www.cnblogs.com/jizhiqiliao/p/9968321.html

你可能感兴趣的文章
解析查询 queryString 请求参数的函数
查看>>
git bash 风格调整
查看>>
linux操作系统加固软件,系统安全:教你Linux操作系统的安全加固
查看>>
linux中yum源安装dhcp,24.Linux系统下动态网络源部署方法(dhcpd)
查看>>
HDOJ-1010 Tempter of the Bone
查看>>
JavaNIO基础02-缓存区基础
查看>>
日本开设无人机专业,打造无人机“人才市场”
查看>>
190行代码实现mvvm模式
查看>>
兼容几乎所有浏览器的透明背景效果
查看>>
jeesite 框架搭建与配置
查看>>
Linux VNC server的安装及简单配置使用
查看>>
阿里宣布开源Weex ,亿级应用匠心打造跨平台移动开发工具
查看>>
Android项目——实现时间线程源码
查看>>
招商银行信用卡重要通知:消费提醒服务调整,300元以下消费不再逐笔发送短信...
查看>>
python全栈_002_Python3基础语法
查看>>
C#_delegate - 调用列表
查看>>
[转]Windows的批处理脚本
查看>>
多维数组元素的地址
查看>>
数据库运维体系_SZMSD
查看>>
福大软工1816 · 第三次作业 - 结对项目1
查看>>