1

I am experimenting Parallel Programming for the first time and i am using it to improve the speed filtering on the Telerik Scheduler control.

this is the code i am using to filter appointments by room, ex:

Task.Factory.StartNew(() =>
{
    SchedulerView view = this.radScheduler1.ActiveView;
    //ConcurrentBag<Classes.Appointment> _bag;

    if (InvokeRequired) Invoke((Action)(delegate
    {
        this.radScheduler1.Appointments.BeginUpdate();

        _itemsview = appointments.AsEnumerable().Where(app => app.Start >= view.StartDate && app.End < view.EndDate.AddDays(1)).ToList();

        //_bag = new ConcurrentBag<Classes.Appointment>(_itemsview);    
        Parallel.ForEach(_itemsview, item =>
        {
            if (_unidades.Contains(item.Room.ToString()))
            {
                 item.Visivel = true;
            }
            else
            {
                 item.Visivel = false;
            }
        });

        this.radScheduler1.Appointments.EndUpdate();
        this.radScheduler1.Select();
    }));
 });

What happens next, is that i can see sometimes other appointments that felled of the filter. I know that the number of appointments filtered is always right, but not always i see the right appointments on the screen. What is happening ?

Noe Rocha
  • 13
  • 2

1 Answers1

0

In general, you'll need to update your UI elements on the main thread. Even if you are using WPF and update a bound property on a background thread, the WPF system still has to marshall that back to the UI.

This means, in your case, since your "work" is completely updating the UI and bound properties, you'd be better off just doing it directly on the UI thread. The Parallel.ForEach, in this case, is likely to make the entire operation less stable and likely slower, as well.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Less stable is probably right, but its not slower than before. I am updating the UI in the main thread, that's what happens when i make the updates inside an "if (InvokeRequired) Invoke((Action)(delegate", correct ? – Noe Rocha Oct 03 '13 at 16:39
  • @NoeRocha Right now, you're making a background thread, then instantly marshalling BACK to the UI thread (Invoke), then working on the UI thread, then using Parallel.ForEach to work on background threads, but the work is all work that needs to happen on the UI thread anyways... – Reed Copsey Oct 03 '13 at 16:46
  • @NoeRocha The entire thing will be faster (and work properly) if you just wrote the code directly and took out the Task/Invoke/Parallel calls – Reed Copsey Oct 03 '13 at 16:46
  • If it needs to be like that, than it will be like...i just witnessed such an improve of speed and just feels a bit sad to give up that speed. Anyway, you got something else i could improve ? – Noe Rocha Oct 03 '13 at 19:09
  • its 'List' and appointments its a 'BindingList' of custom class (binded to the scheduler control) – Noe Rocha Oct 03 '13 at 19:20