SynchronizationContext.Post到用户界面的方法用户界面、方法、SynchronizationContext、Post

2023-09-03 05:36:07 作者:゛╰帅の一太糊涂

我正在使用的Web服务,它必须由我来延长会议长/重新​​连接并获得大型数据集回来等等。有时这可能是漫长的,所以我想它是异步更新UI一个单独的线程。

我似乎无法让我的头周围使用的SynchronizationContext调用我的UI线程的方法。我有它,由此我已经通过我的UIThread方面给我的线索,现在我想更新一些唱片公司等的UI线程。我读过吨的职位,但没有似乎解释了如何简单地传递一些参数恢复的方法,也许他们这样做,但我太累/愚蠢已经看到了。

//在主UI线程

 公共无效updateConnStatus(字符串conn_name,布尔连接)
{
        开关(conn_name)
        {
            案康恩:如果(连==真){//做一些事情}打破;
 

//在不同的线程

  uiContext.Post(//东西在这里做代表在逃避我);
 

如果有人可以简单地解释sendOrPostCallBack我如何链接到原始的方法,我将非常感激。

感谢

编辑:

我设法得到code运行,并试图触发事件,它填充我的自定义EventArgs的不错,但无论它说updateUIConnStatus没有被实例化,需要更多的调查:○

 公共无效updateUIThread(字符串康涅狄格州,布尔连接)
    {
       uiContext.Post(新SendOrPostCallback((0)=> {updateConnStatus(这一点,新MyEventArgs<字符串,布尔值>(康涅狄格州,已连接));}),NULL);
    }

公共类MyEventArgs< T,U> :EventArgs的
    {
        私人牛逼_val1;私人ü_val2;
        公共MyEventArgs(T值1,U值2){_val1 =值; _val2 =值; }
        公共牛逼VAL1 {{返回_val1;}}
        公共üval2的{{返回_val2;}}
    }

公共事件的EventHandler< MyEventArgs<字符串,布尔值>> updateConnStatus =代表{};
 
POST注入之用户登录

//在UI线程现在

 公共无效updateConnStatus(对象发件人,MyEventArgs<字符串,布尔值> E)
    {
        开关(e.val1)
        {
            案CONN1:
                如果(e.val2 ==真)
                {
 

解决方案

您需要的类型SendOrPostCallback的委托。这是pretty的尴尬,只需要一个单一类型的对象的参数的。你绝对应该看看任务<>在.NET 4提供类来使它更容易些。或者使用lambda,像这样的:

 字符串conn_name =富;
        uiContext.Post(新SendOrPostCallback((0)=> {
            updateConnStatus(conn_name,真正的);
        }), 空值);
 

在大括号{}之间的code在UI线程上执行。

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)
                {

解决方案

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.