Posts

Es werden Posts vom Juli, 2009 angezeigt.

Performing Cross-Thread calls using the SynchronizationContext class

If you ever performed concurrent operations in .net, you might have stumbled across the problems that occur when you try to set/access the properties of an Object that's at home in another thread (e.g. when you try to set a Control state from a data loading BackgroundWorker). A very convenient way of bypassing this is the use of Control.InvokeRequired() and Control.Invoke(), which utilizes a delegate to send a message to the Control's thread. Anyway, this only works for Controls, and you might want just a little more than that. If so, you should look into SynchronizationContexts. It does pretty much the same thing, but works on non-GUI objects as well. The following sample is an approach you could use to keep track of all objects' SynchronizationContexts for being able to call DoSomething() on them later. Please make sure you add Exception handling wherever appropriate. public class StateToggler { private Dictionary<object, SynchronizationContext> synchronizationCon

IOException when deleting files in NUnit teardown methods

Now will you take a look at the following piece of code, dear? [TestFixture] public class FileSystemTests { private static readonly string testPath = "./Test"; [TestFixtureSetUp] public void CreateTestSetEnvironment() { DirectoryInfo dir = new DirectoryInfo(testPath); if (!dir.Exists) { dir.Create(); } } [TestFixtureTearDown] public void DestroyTestSetEnvironment() { DirectoryInfo dir = new DirectoryInfo(testPath); if (dir.Exists) { dir.Delete(true); } } [TearDown] public void DestroyTestEnvironment() { DirectoryInfo dir = new DirectoryInfo(testPath); foreach ( FileInfo file in dir.GetFiles() ) { file.Delete(); } } [Test] public void Test() { FileInfo file = new FileInfo( string.Format( @"{0}\test.txt