Someone asked for a much clear post. I am trying to update my UI from Update function which is on my main thread. I tried to make it by using delegate but it did not work. My subscribers are sometimes called from a secondary thread and sometimes from the main thread, how can I solve this? What I have in my update function will cause lots of problems.
private void Update()
{
    if (update != null)
    {
        lock (update)
        {
            update.Invoke(oldState, state);
            update = null;
        }
    }
}
private void LoginSubscirber(AppState oldState, AppState state)
{
    lock (state)
    {
        if (oldState.loginState != state.loginState)
        {
            if (state.loginState == loginStateEnum.loadingLogin)
            {
                update = LoadingLoginCallback;
            }
            else if (state.loginState == loginStateEnum.successLogin)
            {
                update = LoginSuccescCallback;
            }
        }
    }
}
LoginSubscrirber (ignore my miss spelling of it) is called sometimes with Task.Factory.StartNew()