fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. class Program {
  8. static void Main(string[] args) {
  9. int i = 5;
  10.  
  11. Console.WriteLine("before: {0}", i);
  12.  
  13. var thread = new myThread<int>(
  14. (state) => {
  15. state.value += 1;
  16. }
  17. );
  18.  
  19. thread.Start(
  20. (obj) => { i = obj; },
  21. () => i
  22. );
  23.  
  24. while (thread.IsAlive)
  25. ;
  26.  
  27. Console.WriteLine("after: {0}", i);
  28. }
  29.  
  30. class myThread<T>
  31. {
  32. public delegate void ThreadStart(StateObject state);
  33.  
  34. private Thread _thread;
  35.  
  36. public myThread(ThreadStart start)
  37. {
  38. _thread = new Thread((obj) => { start.Invoke((StateObject)obj); });
  39. }
  40.  
  41. public void Start(StateObject obj)
  42. {
  43. _thread.Start(obj);
  44. }
  45.  
  46. public void Start(Action<T> f_set, Func<T> f_get) {
  47. _thread.Start(new StateObject(f_set, f_get));
  48. }
  49.  
  50. public bool IsAlive { get { return _thread.IsAlive; } }
  51.  
  52. public class StateObject {
  53. private Action<T> _f_set;
  54. private Func<T> _f_get;
  55. public StateObject(Action<T> f_set, Func<T> f_get)
  56. {
  57. _f_set = f_set;
  58. _f_get = f_get;
  59. }
  60.  
  61. public T value {
  62. set { _f_set(value); }
  63. get { return _f_get(); }
  64. }
  65. }
  66. }
  67. }
  68.  
  69.  
Success #stdin #stdout 0.03s 38016KB
stdin
Standard input is empty
stdout
before: 5
after: 6