C # subclinger callback function notifies the main thread completion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication5
{
public class ThreadCallBackTest
{
///

/// Record the number of sub-threads completed normally
///

Int count = 0;

///
/// Main function
/// /

///
public bool Run()
{
//Total number of threads
Int total = 10;
m;
List listThread = new List();
for (int i = 0; i {
m = m = new br> mx = i;
my = i + 1;
m.callBack = ThreadCallBack (start)(Thread); new Threads(Thread); new t = Thread t = new
listThread.Add(t);
}

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}


















            
            //验证子线程完成情况
            if (count == total)
            {
                Console.WriteLine("全部完成");
                return true;
            }
            else
            {
                Console.WriteLine("未全部完成,count=" + count);
                return false;
            }
        }
        
        ///
/// Sub-thread function (business processing)
///

///
private void Buss (object param)
{
{
try
{
{
} MyParam myparam = (MyParam) param;
Thread'
(MyParam) param;
(MyParam) param;
(MyParam) param;
2000 if 5)

{
Throw new Exception("5555555");
Throw new Exception("5555555");
}
else
{
// service processing
Console.WriteLine (string.Format ( "threads {0}, {0} + {1} = {2}", myparam. x, myparam.y, myparam.x + myparam.y));
ThreadCallBackDelegate cb = myparam.callBack as ThreadCallBackDelegate;
cb ();
}
}
catch (Exception ex )
{
{

Abnormal capture of sub-threads to prevent abnormal sub-threads from causing main program to crash
Console.WriteLine(ex.ToString());
}}
> ///
/// Callback function, the business processing is normal, called after completion, not called if abnormal



Private void ThreadCallBack()
{< br> Count++;
}

} }

} ///
/// Define delegation
Thread
///

Thread delegate( void) Back privateDelegate() >
///
/// Sub-thread parameters
///

Class MyParam
{
public int x, y;
public ThreadCallBackDelegate callBack;
}
}
}

Share pictures

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System .Threading;

namespace ConsoleApplication5
{
public class ThreadCallBackTest
{
///
/// Record the number of sub-threads completed normally
///

int count = 0;

///
/// Main function
///


// /
public bool Run()
{
//Total number of threads
Int total = 10;
Thread list
Thread list
= new List();
for (int i = 0; i {
m = new MyParam();
i
i
i
= i + 1;
m.callBack = ThreadCallBack;
Thread t = n ew Thread(new ParameterizedThreadStart(Buss));
t.Start(m);
listThread.Add(t);


waiting for all threads

(var item in listThread)
{
item.Join ();
}

// child thread verification completion
if (count == total)
{
Console.WriteLine("Complete all");
Return true;
Console.WriteLine("Complete all");
}
Count all(count) Else {count

Return false;
}
}
}


(business processing) ///
(business processing) /// br> ///
private void Buss(object param)
{
try
MyParam (MyParam) (
MyParam)
Thread.Sleep(2000);

if (myparam.x == 5)
{
throw new Exception ( "5555555");
}
else
{
// service processing
Console.WriteLine (string.Format ( "threads {0} ,{0}+{1}={2}", myparam.x, myparam.y, myparam.x + myparam.y));
ThreadCallBackDelegate cb = myparam.callBack as ThreadCallBackDelegate;
;
Console





































































));
}
}

///
Callback function, which is called after business processing is normal and completed, but not called abnormally
/// < /summary>
private void ThreadCallBack()
{
count++;
}



/// delegate
/// definition

private delegate void ThreadCallBackDelegate();

///
/// Sub-thread parameters
///

class MyParam
{
public int x, y;
public ThreadBackDelegate callBack;



Leave a Comment

Your email address will not be published.