1
\$\begingroup\$
public class WmSync : IDisposable
{
 public WmSync()
 {
 var sync = new Control();
 sync.Handle.GetHashCode(); // ensure handle is created
 _sync = sync;
 Thread.MemoryBarrier();
 }
 public void Dispose()
 {
 _sync.Dispose();
 }
 public void BeginInvoke(Action action)
 {
 _sync.BeginInvoke(action);
 }
 public void Invoke(Action action)
 {
 _sync.Invoke(action);
 }
 public bool InvokeRequired
 {
 get { return _sync.InvokeRequired; }
 }
 private readonly Control _sync;
}

The idea is to create this object in the main thread and use it from background threads to execute pieces of code in the main thread.

For example:

// somewhere in the very beginning of the program, in the main thread
static WmSync SyncQueue = new WmSync();
// somewhere in a background thread
SyncQueue.Invoke(
 () =>
 {
 if (_settingsForm != null)
 _settingsForm.Show();
 });

This is just an artificial sample, but it should give you idea. Is this code thread safe?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 2, 2015 at 5:14
\$\endgroup\$
3
  • \$\begingroup\$ Can you please elaborate on The idea is to create this object in the main thread and use it from background threads to execute pieces of code in the main thread by updating your question. Some real world example would help also. \$\endgroup\$ Commented Jan 2, 2015 at 9:00
  • \$\begingroup\$ @Heslacher, done. And BTW, it's not a threadsafe UI control. \$\endgroup\$ Commented Jan 2, 2015 at 9:12
  • \$\begingroup\$ I have reverted the title change. \$\endgroup\$ Commented Jan 2, 2015 at 9:21

1 Answer 1

1
\$\begingroup\$

Thread.MemoryBarrier()

What do you expect the call to Thread.MemoryBarrier() will help your code ?

What does Thread.MemoryBarrier do ? It prevents that the compiler and the hardware subtly transform a program’s memory operations.

So basically for your code it ensures that then following instructions will be executed in the order they are written.

var sync = new Control();
sync.Handle.GetHashCode(); // ensure handle is created
_sync = sync; 

Thread safety

To answer "Is this code thread safe ?", you first need to set your definition of thread safety aka what does thread safety means for you. See also what-is-this-thing-you-call-thread-safe

General

Glorfindel
1,1133 gold badges14 silver badges27 bronze badges
answered Jan 2, 2015 at 8:45
\$\endgroup\$
3
  • \$\begingroup\$ in the first place, this class was never supposed to be used in UI. The idea is to pass execution to the main thread, it's the one and only function of this class. \$\endgroup\$ Commented Jan 2, 2015 at 8:58
  • \$\begingroup\$ "2. you are misusing a Control" - in the same way everyone (including MS) does. "3. wouldn't a SynchronizationContext be a better fit here" - how exactly? \$\endgroup\$ Commented Jan 7, 2015 at 3:37
  • \$\begingroup\$ @user626528 if it's not a UI application, then what is the definition of "main thread" then? Why can't you run your logic in the "background" threads? Please describe why you need to capture a certain thread and name it "main" \$\endgroup\$ Commented Feb 4, 2015 at 16:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.