-
Notifications
You must be signed in to change notification settings - Fork 5
threads
Michael Kolarz edited this page Sep 19, 2011
·
1 revision
Allows using .NET thread syntax instead of standard JavaScript setTimeout, setInterval, clearTimeout and clearInterval methods.
Only following methods are supported
Thread.Thread(ParametrizedThreadStart)
Thread.Thread(ThreadStart)
Thread.Start()
Thread.Start(object)
Thread.Abort()
Thread.Sleep(int)
Monitor.Enter(object)
Monitor.Exit(object)
Since in our framework we do not have our own scheduler, we base only on JavaScript interpreter's scheduler. In partucular - our threads are not real threads (since JavaScript interpreter is single thread only) and during one thread execution - other threads sleeps.
In C#, following two methods are equivalent at intermidiate language level:
void m1() {
Monitor.Enter(obj);
try {
doSomething();
} finally {
Monitor.Exit(obj);
}
}
void m2() {
lock(obj) {
doSomething();
}
}
void createRotator(int x, int y, int r, int timeout) {
var square = new Division { InnerHTML = "*", ClassName = "square", Title= "Click to stop rotator." };
Document.Body.Add(square);
// New Thread is created.
var thread = new Thread(() => {
int n = 0;
while (true) {
var fi = ++n * Math.PI / 50;
square.Style.Left = x + r * Math.Sin(fi);
square.Style.Top = y + r * Math.Cos(fi);
// Suspend in thread.
Thread.Sleep(timeout);
}
});
// Thread is started.
thread.Start();
square.Click += e => {
// Thread is aborted, i.e. if sleeped - it will newer wake up.
thread.Abort();
};
}
In IE8 Monitor.Exit(object)
is not supported when object is Window - hence do not lock on such objects.