using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Helpers
{
class ThreadedHelper<I,O>
{
public I input { get; set; }
public ThreadedHelper(Func<I,O> doWork, Action<O> callback, Action<I, Exception> errorCallback)
{
this.doWork = doWork;
this.callback = callback;
this.errorCallback = errorCallback;
t = new Thread(Process);
}
public void Start()
{
t.Start();
}
private void Process()
{
try
{
O retun = doWork(input);
callback(retun);
}
catch (Exception ex)
{
errorCallback(input, ex);
t.Abort();
}
}
private Func<I,O> doWork;
private Action<O> callback;
private Action<I, Exception> errorCallback;
private Thread t;
// example
// using System.Threading;
// using Helpers;
//class Program
//{
// class Input
// {
// public int a { get; set; }
// public int b { get; set; }
// }
// class Output
// {
// public Input input { get; set; }
// public int result { get; set; }
// }
// static void Main(string[] args)
// {
// var p = new Program();
// new ThreadedHelper<Input, Output>(p.DoWork, p.Callback, p.ErrorCallback)
// {
// input = new Input { a = 4, b = 0 }
// }.Start();
// for (int i = 0; i < 30; i++)
// {
// Console.WriteLine("wait");
// Thread.Sleep(100);
// }
// Console.WriteLine("end");
// Console.ReadKey();
// }
// Output DoWork(Input input)
// {
// try
// {
// Thread.Sleep(2000);
// int s = input.a / input.b;
// return new Output { input = input, result = s };
// }
// catch (Exception ex)
// {
// throw ex;
// }
// }
// void Callback(Output output)
// {
// Console.WriteLine($@"output : {output.input.a},{output.input.b},{output.result}");
// }
// void ErrorCallback(Input i, Exception ex)
// {
// Console.WriteLine($@"output error : {i.a}, {i.b}, {ex.Message}, {ex.StackTrace}");
// }
//}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Helpers
{
class ThreadedHelper<I,O>
{
public delegate O DoWork(I inValue);
public delegate void CallBack(O outValue);
public delegate void ErrorCallBack(I input, Exception message);
public I input { get; set; }
public ThreadedHelper(DoWork work, CallBack callback, ErrorCallBack errorCallback)
{
this.work = work;
this.callback = callback;
this.errorCallback = errorCallback;
t = new Thread(Process);
}
public void Start()
{
t.Start();
}
private void Process()
{
try
{
O retun = work(input);
callback(retun);
}
catch (Exception ex)
{
errorCallback(input, ex);
t.Abort();
}
}
private DoWork work;
private CallBack callback;
private ErrorCallBack errorCallback;
private Thread t;
// example
// using System.Threading;
// using Helpers;
//class Program
//{
// class Input
// {
// public int a { get; set; }
// public int b { get; set; }
// }
// class Output
// {
// public Input input { get; set; }
// public int result { get; set; }
// }
// static void Main(string[] args)
// {
// var p = new Program();
// new ThreadedHelper<Input, Output>(p.DoWork, p.Callback, p.ErrorCallback)
// {
// input = new Input { a = 4, b = 0 }
// }.Start();
// for (int i = 0; i < 30; i++)
// {
// Console.WriteLine("wait");
// Thread.Sleep(100);
// }
// Console.WriteLine("end");
// Console.ReadKey();
// }
// Output DoWork(Input input)
// {
// try
// {
// Thread.Sleep(2000);
// int s = input.a / input.b;
// return new Output { input = input, result = s };
// }
// catch (Exception ex)
// {
// throw ex;
// }
// }
// void Callback(Output output)
// {
// Console.WriteLine($@"output : {output.input.a},{output.input.b},{output.result}");
// }
// void ErrorCallback(Input i, Exception ex)
// {
// Console.WriteLine($@"output error : {i.a}, {i.b}, {ex.Message}, {ex.StackTrace}");
// }
//}
}
}