0

I am using Task class in my app. This is NOT WPF application! The question is are there any possibilities of calling function from Task body on UI thread, like this:

var task = new Task(() => DoSmth(1));
task.Start();

public void DoSmth(int arg)
    {
        //smth
        CallNotifFuncOnUIThread(() => Notify(1));
        //smth ELSE
        CallNotifFuncOnUIThread(() => Notify(2));//notify AGAIN
        //smth ELSE
    }

public void Notify(int arg)
    {
        progressBar1.Value = arg;
    }

Or maybe there are other solutions of this problem? I know about BackgroundWorker class, but what about Tasks?

iamnp
  • 510
  • 8
  • 23
  • Related: http://stackoverflow.com/questions/5971686 – dtb Mar 28 '13 at 12:26
  • 1
    Is it WPF? If so, use a [`Dispatcher`](http://msdn.microsoft.com/pl-pl/library/system.windows.threading.dispatcher.aspx), namely the `Application.Current.Dispatcher` which will give you the UI dispatcher. That's of course if you want to update the UI somewhere in the middle of the background processing, if you just want to update it at the very end, use the appropriate scheduler context in the continuation. – Patryk Ćwiek Mar 28 '13 at 12:26
  • Here is answer for similiar question : http://stackoverflow.com/questions/15327717/algorithm-progress-callback/15661639#15661639 use TPL INotify interface – semeai Mar 28 '13 at 12:32

4 Answers4

1

You can always call other methods inside your DoSth()

Dispatcher.Invoke(...);
Dispatcher.BeginInvoke(...);

You can also user Task.ContinueWith(...) to do sth after the task is finished processing ...

David
  • 15,894
  • 22
  • 55
  • 66
1

If you have a task you can start it on the gui thread by providing the correct scheduler:

Task.Factory.StartNew(() => DoSomethingOnGUI(), TaskScheduler.FromCurrentSynchronizationContext());
Oliver
  • 43,366
  • 8
  • 94
  • 151
0

With windows Form and progressBar1 component on ityou can use TPL IProgress interface for Task.

    private void Form1_Load(object sender, EventArgs e)
    {
        Progress<int> progress = new Progress<int>();
        var task = Alg(progress);
        progress.ProgressChanged += (s, i) => { UpdateProgress(i); };
        task.Start();
    }

    public void Notify(int arg)
    {
        progressBar1.Value = arg;
    }

    public static Task Alg(IProgress<int> progress)
    {
        Task t = new Task
        (
            () =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(100);
                    ((IProgress<int>)progress).Report(i);
                }
            }
        );
        return t;
    }
semeai
  • 96
  • 1
  • 7
0

UI is usually STA see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms680112(v=vs.85).aspx

so in order to do something from none UI thread in the UI you need to inject somehow the msg into the thread see for example htis winform example:

http://weblogs.asp.net/justin_rogers/articles/126345.aspx

watever UI you are using you will need a similar system.

Nahum
  • 6,959
  • 12
  • 48
  • 69