Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying to use the TPL in .NET 4.0 for some long running processes. The problem is, those processes update the UI continuously (I've included an example of the update routine). And this has created a huge problem, because task(s) allow you to update the UI once with a continuation task after it's completed. I need those updates to happen when they are called and not after the parent task has completed. If you read the example, you'll see that it's real Mickey Mouse and just stupid. You'll notice it creates a superfluous task to get to a continuation task. And inspite of using the "AttachToParent" options, the task isn't executing immediately. Is there anyway to circumvent deadlocking and force the UI to update when prompted while using the TPL and not wait for the parent task to complete?

Thank you,

C#
public MainForm()
{
    InitializeComponent();

    // Set up the threading value.
    _uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    _synchronizationContext = SynchronizationContext.Current;
}

C#
Action _wait = () => { System.Threading.Thread.Sleep(10); };

C#
private void UpdateLog(string _element, string _text, int _level)
        {
            Action _updateLog = () =>
            {
                logFlexGrid.Redraw = false;

                // Add a new row for this node.
                int _rowCnt = logFlexGrid.Rows.Count;
                logFlexGrid.Rows.Add();

                if (_level == 1)
                {
                    logFlexGrid.Rows[_rowCnt].IsNode = true;
                    logFlexGrid.Rows[_rowCnt].Node.Level = _flexGridLevel;
                    _flexGridLevel++;
                }
                else if (_level == -1)
                    _flexGridLevel--;

                logFlexGrid[_rowCnt, 0] = _element;
                logFlexGrid[_rowCnt, 1] = DateTime.Now.TimeOfDay;
                logFlexGrid[_rowCnt, 2] = _text;

                // Perform simple status update.
                UpdateStatusStrip(_text);

                // System.Windows.Forms.Application.DoEvents();
                logFlexGrid.Redraw = true;
                logFlexGrid.ShowCell(_rowCnt, 0);
            };

            Task _t = Task.Factory.StartNew(_wait, TaskCreationOptions.AttachedToParent).ContinueWith(delegate
            {
                _synchronizationContext.Post(delegate { _updateLog(); }, _uiScheduler);
            }, TaskContinuationOptions.AttachedToParent);
            _t.Wait();
            System.Windows.Forms.Application.DoEvents();
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900