tagged [thread-safety]

c# lock on reference passed to method - bad practice?

c# lock on reference passed to method - bad practice? I have a method similar to: A few points: 1. Is locking in this way bad practice? 2. Should I lock on a private static object instead? 3. If so, w...

Why aren't classes like BindingList or ObservableCollection thread-safe?

Why aren't classes like BindingList or ObservableCollection thread-safe? Time and time again I find myself having to write thread-safe versions of BindingList and ObservableCollection because, when bo...

What makes instance members thread-unsafe vs public static?

What makes instance members thread-unsafe vs public static? So we've all seen the Threading notification on MSDN for many available generic objects: "Public static (Shared in Visual Basic) members of ...

8 Aug at 20:37

How to make a class Thread Safe

How to make a class Thread Safe I am writing a C# application. I have (a kind of) logging class. and this logging class will be used by many threads. How to make this class thread safe? Should I make ...

27 Aug at 22:13

Method lock in c#

Method lock in c# I have one class with these three methods. This class is used by many threads. I would like the Method1 to wait, if Method2 and/or Method3 are running in any threads. Any suggestions...

20 Aug at 19:3

msdn: What is "Thread Safety"?

msdn: What is "Thread Safety"? In many MSDN documents, this is written under the Thread Safety heading; "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance m...

29 Jun at 05:0

Are C# delegates thread-safe?

Are C# delegates thread-safe? If you have a class instance with a delegate member variable and multiple threads invoke that delegate (assume it points to a long-running method), is there any contentio...

How to increment (add value to) decimal in a thread-safe way?

How to increment (add value to) decimal in a thread-safe way? I have a `decimal` variable that is accessed from multiple threads at the same time. `Interlocked` class functions do not support decimals...

22 Oct at 12:2

Under C# is Int64 use on a 32 bit processor dangerous

Under C# is Int64 use on a 32 bit processor dangerous I read in the MS documentation that assigning a 64-bit value on a 32-bit Intel computer is not an atomic operation; that is, the operation is not ...

24 Jun at 23:55

How to create a task (TPL) running a STA thread?

How to create a task (TPL) running a STA thread? Using Thread is pretty straightforward How to accomplish the same using Tasks in a WPF application? Here is some code: ``` Task.Factory.StartNew ( ...

Is CreateDirectory() in C# thread-safe?

Is CreateDirectory() in C# thread-safe? Can I safely attempt to create the directory from two different threads, without having one of them throw an exception, or run into other issues? Note that acco...

5 Mar at 19:33

Thread safety and System.Text.Encoding in C#

Thread safety and System.Text.Encoding in C# Is it safe to use the same `Encoding` object from different threads? By "using" I mean, calling `Encoding.GetString()`, `Encoding.GetBytes()` and write som...

11 Jun at 16:12

What is the difference between SynchronizedCollection<T> and the other concurrent collections?

What is the difference between SynchronizedCollection and the other concurrent collections? How does `SynchronizedCollection` and the concurrent collections in the `System.Collections.Concurrent` name...

Is a read-only HashSet inherently threadsafe?

Is a read-only HashSet inherently threadsafe? If I initialize a `HashSet` inside a `Lazy` initializer and then never change the contents, is that `HashSet` inherently threadsafe? Are there read action...

23 May at 11:45

Designing a Thread Safe Class

Designing a Thread Safe Class When reading the MSDN documentation it always lets you know if a class is thread safe or not. My question is how do you design a class to be thread safe? I am not talking...

28 Jun at 00:4

How can I make a JUnit test wait?

How can I make a JUnit test wait? I have a JUnit test that I want to wait for a period of time synchronously. My JUnit test looks like this: I tried `Thread.currentThread().wait()`, but it throws

2 Nov at 19:35

difference between Java atomic integer and C# Interlocked.Increment method

difference between Java atomic integer and C# Interlocked.Increment method Im just wondering, is there a difference between how you increment a static variable in Java and C# in an threaded enviroment...

27 May at 13:41

Thread safety in String class

Thread safety in String class Is it thread safe to build strings from local variables using the `String` class like in the methods below? Suppose that the methods below are called from several threads...

8 May at 09:47

Modifying list from another thread while iterating (C#)

Modifying list from another thread while iterating (C#) I'm looping through a List of elements with foreach, like this: However, in an another thread I am calling something like During runtime, this c...

4 Apr at 19:17

Thread safety on readonly static field initialisation

Thread safety on readonly static field initialisation If one creates a readonly static member like this: We know that the static constructor will initialise the MyClass.Instance field if some thread a...

28 Aug at 12:54

How to implement a multi-index dictionary?

How to implement a multi-index dictionary? Basically I want something like Dictionary, but not (as I've seen here in other question) with the keys in AND, but in OR. To better explain: I want to be ab...

Thread-safe use of a singleton's members

Thread-safe use of a singleton's members I have a C# singleton class that multiple classes use. Is access through `Instance` to the `Toggle()` method thread-safe? If yes, by what assumptions, rules, e...

Is this non-locked TryGetValue() dictionary access thread-safe?

Is this non-locked TryGetValue() dictionary access thread-safe? ``` private object lockObj = new object(); private Dictionary dict = new Dictionary(); public string GetOrAddFromDict(int key) { strin...

How to access c# WPF control in thread safe way?

How to access c# WPF control in thread safe way? I've tried using the examples from MSDN for this but they seem to only be applicable to Windows Forms. For instance the method of using .InvokeRequired...

16 Aug at 12:18

Thread safety of a Dictionary<TKey, TValue> with multiple concurrent readers and no writers

Thread safety of a Dictionary with multiple concurrent readers and no writers If I initialize a generic dictionary once, and no further adds/updates/removes are allowed, is it safe to have multiple th...

22 Aug at 04:50