Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm working with web services so its necessary for me to extend session length/reconnect and get large datasets back etc. Sometimes this can be lengthy so I wanted it in an separate thread that updates the UI asynchronously.

I can't seem to get my head around using the synchronizationContext to invoke a method on my UI thread. I have it whereby I have passed my UIThread context to my thread and now I want to update some labels etc on UI Thread. I've read tons of posts but none seem to explain how to simply pass some parameters back to a method, or maybe they do but i'm too tired/stupid to have seen it.

//On main UI Thread

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

//on separate Thread

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

if someone could simply explain how I link the sendOrPostCallBack to the original method I would be very grateful.

Thanks

Edit:

I managed to get the code to run and try to fire the event, it populates my custom eventArgs okay but either its saying that updateUIConnStatus has not been instantiated, needs more investigation :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 {};

//on UI Thread Now

 public void updateConnStatus(object sender, MyEventArgs<String,Boolean> e)
    {
        switch (e.val1)
        {
            case "Conn1" :
                if (e.val2 == true)
                {
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need a delegate of type SendOrPostCallback. Which is pretty awkward, it only takes a single argument of type object. You definitely ought to look at the Task<> class available in .NET 4 to make this easier. Or use a lambda, like this:

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

The code between the { braces } executes on the UI thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...