1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| class Program
{
class Toto
{
private delegate void action();
public Toto()
{
MethodInfo mi1 = typeof(Toto).GetMethod("Action",
BindingFlags.NonPublic | BindingFlags.Instance);
Delegate act = Delegate.CreateDelegate((typeof(action)), this, mi1);
AutoStopThread t = new AutoStopThread(1000, act);
}
private void Action()
{
Console.WriteLine("One Action is done in thread : " + Thread.CurrentThread.ManagedThreadId);
}
}
static void CreateStarterObject()
{
Toto t = new Toto();
Thread.Sleep(5000);
}
static void Main(string[] args)
{
Console.WriteLine("Creating object in thread : " + Thread.CurrentThread.ManagedThreadId);
CreateStarterObject();
Console.WriteLine("forcing garbage collection in thread : " + Thread.CurrentThread.ManagedThreadId);
GC.Collect();
Console.ReadLine();
}
}
class WeakReferenceThread
{
Thread t;
WeakReference fishRef;
WeakReference traitement;
public ManualResetEvent terminate = new ManualResetEvent(false);
private volatile bool stop ;
public WeakReferenceThread(AutoStopThread fish, Delegate d)
{
this.fishRef = new WeakReference(fish);
this.traitement = new WeakReference(d);
}
public void Start()
{
t = new Thread(new ThreadStart(BackgroundWork));
t.IsBackground = true;
stop = false;
t.Start();
}
public void Stop()
{
stop = true;
}
public void BackgroundWork()
{
//bool done = false;
while (! stop)
{
CallWork(); // Test
//stop = terminate.WaitOne(1000, false);
}
}
// this is pulled out into a helper method to ensure
// the Fish object is referenced for the minimal amount of time
private void CallWork()
{
//bool done;
AutoStopThread fish = Fish;
if (fish != null)
{
((Delegate)this.traitement.Target).DynamicInvoke();
//fish.Swim();
stop = false;
}
else
{
stop = true;
}
///return done;
}
public AutoStopThread Fish
{
get { return fishRef.Target as AutoStopThread; }
}
}
class AutoStopThread : IDisposable
{
internal class AutoStopThreadWork
{
Thread t;
WeakReference fishRef;
WeakReference actionDelegate;
public ManualResetEvent terminate = new ManualResetEvent(false);
int actionFrequenceMlSec;
public AutoStopThreadWork(AutoStopThread fish, int actionFrequenceMlSec, Delegate action)
{
this.fishRef = new WeakReference(fish);
this.actionDelegate = new WeakReference(action);
t = new Thread(new ThreadStart(BackgroundWork));
this.actionFrequenceMlSec = actionFrequenceMlSec;
t.IsBackground = true;
t.Start();
}
public void BackgroundWork()
{
bool done = false;
while (!done)
{
done = Swim();
if (!done)
{
done = terminate.WaitOne(this.actionFrequenceMlSec, false);
}
}
}
// this is pulled out into a helper method to ensure
// the Fish object is referenced for the minimal amount of time
private bool Swim()
{
bool done;
AutoStopThread fish = Fish;
if (fish != null)
{
//fish.Swim();
((Delegate)(this.actionDelegate.Target)).DynamicInvoke();
done = false;
}
else
{
done = true;
}
return done;
}
public AutoStopThread Fish
{
get { return fishRef.Target as AutoStopThread; }
}
}
AutoStopThreadWork autoStopThread;
//private delegate void action();
public AutoStopThread(int actionFrequenceMlSec, Delegate action)
{
//MethodInfo mi1 = typeof(Program).GetMethod("Swim",
//BindingFlags.Public | BindingFlags.Static);
//Delegate act = Delegate.CreateDelegate((typeof(action)),this, mi1);
autoStopThread = new AutoStopThreadWork(this, actionFrequenceMlSec, action);
}
//public void Swim()
//{
// Console.WriteLine("The Action is done");
//}
volatile bool disposed = false;
public void Dispose()
{
if (!disposed)
{
autoStopThread.terminate.Set();
disposed = true;
GC.SuppressFinalize(this);
}
}
~AutoStopThread()
{
if (!disposed)
{
Dispose();
}
}
} |
Partager