Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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?

share|improve this question
    
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. – Heslacher Jan 2 '15 at 9:00
    
@Heslacher, done. And BTW, it's not a threadsafe UI control. – user626528 Jan 2 '15 at 9:12
    
I have reverted the title change. – Heslacher Jan 2 '15 at 9:21

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

share|improve this answer
    
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. – user626528 Jan 2 '15 at 8:58
    
"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? – user626528 Jan 7 '15 at 3:37
    
@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" – almaz Feb 4 '15 at 16:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.