Jan 17, 2012

[How to] Refresh UI in Threads (Delegate)

private delegate void Updater(Control ctrl);


private void Update(Control ctrl)
{
    if (this.InvokeRequired)
    {
        Updater u = new Updater(Update);
        this.Invoke(u, ctrl);
    }
    else
    {
        ctrl.Refresh();
    }
}


And call this method in threads
this.Update(this);

In fact, this could be a delegate template liks this:
private delegate void Updater(UI, param1, param2, ...);


private void Update(UI, param1, param2, ...){
    if (this.InvokeRequired){
        Updater u = new Updater(Update);
        this.Invoke(u, UI, param1, param2, ...);
    }else{
        UI.DoSomething(param1);
        UI.Brabrabra(param2);
        ...
    }
}

No comments:

Post a Comment