SynchronizationContext.Post Method for UI

I work with web services, so I need to extend the duration of the session / reconnect and get large datasets back. Sometimes this can be lengthy, so I wanted it in a separate thread that updates the user interface asynchronously.

I can't seem to get around using synchronizationContext to call a method in my user interface thread. I have this when I passed my UIThread context to my thread, and now I want to update some tags, etc. In the user interface thread. I read a lot of posts, but none of them explains how easy it is to pass some parameters back to the method, or maybe they do, but I'm too tired / stupid to see it.

// In the main thread of the user interface

public void updateConnStatus(string conn_name, bool connected)
{
        switch (conn_name)
        {
            case "Conn" : if (connected == true){ //do something} break;

// on a separate topic

uiContext.Post( //something to do with delegates in here that eludes me );

if someone can just explain how I associate sendOrPostCallBack with the original method, I would really appreciate it.

thank

Edit:

I managed to run this code and try to fire the event, it fills my custom eventArgs in order, but either its assertion that updateUIConnStatus was not created requires further study: o

public void updateUIThread(string conn, bool connected)
    {
       uiContext.Post(new SendOrPostCallback((o) => { updateConnStatus(this, new MyEventArgs<String, Boolean>(conn, connected)); }), null);
    }

public class MyEventArgs<T, U> : EventArgs
    {
        private T _val1; private U _val2;
        public  MyEventArgs(T value1, U value2) { _val1 = value1; _val2 = value2; }
        public T val1 { get { return _val1;} }
        public U val2 { get {return _val2;} }
    }

public event EventHandler<MyEventArgs<String, Boolean>> updateConnStatus = Delegate {};

// in UI Thread Now

 public void updateConnStatus(object sender, MyEventArgs<String,Boolean> e)
    {
        switch (e.val1)
        {
            case "Conn1" :
                if (e.val2 == true)
                {
+5
source share
2 answers

You need a delegate like SendOrPostCallback. Which is rather inconvenient, it takes only one argument of the type object. You should definitely consider the Task <> class available in .NET 4 to make this easier. Or use a lambda, for example:

        string conn_name = "foo";
        uiContext.Post(new SendOrPostCallback((o) => {
            updateConnStatus(conn_name, true);
        }), null);

{braces} .

+12

(, ViewModels) , SynchronizationContext TaskScheduler ( IMHO) , ...

private readonly SynchronizationContext _syncContext = SynchronizationContext.Current;
private readonly TaskScheduler _scheduler = TaskScheduler.Current;

void OnSomeEvent(object sender, EventArgs e)
{
    if (_syncContext != SynchronizationContext.Current)
    {
        // Use Send if you need to get something done as soon as possible.
        // We'll be polite by using Post to wait our turn in the queue.
        _syncContext.Post(o => DoSomething(), null);
        return;
    }
    // Call directly if we are already on the UI thread
    DoSomething();
}

void OnSomeOtherEvent(object sender, MyEventArgs e)
{
    var arg1 = e.Arg1; // "Hello "
    var arg2 = e.Arg2; // {"World", "!"};

    // Process args in the background, and then show the result to the user...
    // NOTE: We don't even need to check the context because we are passing
    // the appropriate scheduler to the continuation that shows a MessageBox.

    Task<string>.Factory.StartNew(() => ReturnSomething(arg1, arg2))
        .ContinueWith(t => MessageBox.Show(t.Result), _scheduler);
}

void DoSomething() { MessageBox.Show("Hello World!"); }

string ReturnSomething(string s, IEnumerable<string> list)
{
    return s + list.Aggregate((c, n) => c + n);
}
+3

All Articles