Wednesday, February 10, 2016

Update Wpf StatusLabel from other thread - Status Label auf richtigem Thread updaten

        ///


updates StatusLabel on Gui Thread
        /// StatusText to set
        private void UpdateStatus(string sStatus)
        {
            Trace.WriteLine(sStatus);
            if (!LblStatus.CheckAccess()) //Check if correct Thread
            {
                LblStatus.Dispatcher.BeginInvoke(DispatcherPriority.Send, //Wrong Thread
                    new Action(UpdateStatus), sStatus); //start again on correct thread
                return; //and leave
            }
            LblStatus.Content = sStatus; //only on correct thread the label is set
        }



call like this:
1) start longrunning Task:

Task.Factory.StartNew(MyLong,TaskCreationOptions.LongRunning);


2) call Update Status from Longrunning Task
        private void MyLong()       
{
            UpdateStatus("do some Long time ...");       

}

No comments: