Skip to content
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.

Limitations

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.

Notes

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();
  }
}

Examples

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();
  };
}

Known Issues

In IE8 Monitor.Exit(object) is not supported when object is Window - hence do not lock on such objects.

Clone this wiki locally