Friday, April 15, 2005

C# Course day five

I installed the latest version of Mono on my Mac and the latest version of NAnt (an open source build tool) and managed to compile some of the Lab sample code from this course.
  • System.Threading namespace for creating threads, mutexes, etc.
  • Thread class takes a ThreadStart object in its constructor. A ThreadStart class is a delegate that points to the method you want to run in the thread. Call Thread.Start() to actually kick things off.
  • There are foreground and background threads and a Thread.IsBackground property. An AppDomain will not close as long as there is one foreground thread.
  • Signalling between threads can be done with the WaitHandle class.
  • You can create thread local storage by putting an attribute on a static variable [ThreadStatic] public static in count;
  • Visual Studio kinda sucks for working with Threads. Eclipse is better for this.
  • You can call Thread.Abort() to abort a thread, however a magic ThreadAbortException will get thrown and rethrown out of any catch block unless you call ResetAbort().
  • To Synchronize a method use [MethodImpl(MethodImplOptions.Synchronized}] I think its easier to use Java's synchronized keyword.
  • Can use lock{...} to define a critical section.
  • The Interlocked class's methods can create more optimized locking for simple operations on shared data like increment and decrement.
  • Can use a Windows OS Mutex to lock across processes. A Mutex is more expensive to use.
  • A ReaderWriterLock allows multiple reader or only one writer at a time.
  • The Timer class uses a delegate to call a callback method on a particular interval
  • There is a ThreadPool class that is handy for executing short running methods in the background with a normal priority.
  • IOCompletionCallback is used for processing data in a thread from the pool after an IO operation.
  • Use syntax like [DllImport (, Entrypoint=, CharSet= MyMethodName([...]); to call externall DLL methods.
  • tlbimp.exe is for importing a COM DLL.
  • There are roughly 10-40 instructions called when going from/to .NET from/to COM, not including the data marshalling. It is expensive so be careful.
  • SQL & Oracle clients are written all in managed code (kinda like JDBC drivers) & are much faster than going through OLE DB drivers

No comments: