Note that there are some explanatory texts on larger screens.

plurals
  1. POOdd Task Parallel Library InvalidOperationException
    primarykey
    data
    text
    <p>I have the following code to build an advanced data structure which is pulled from SQL Server, then when the retrevial of that data is complete I update the UI. The code used is </p> <pre><code>private void BuildSelectedTreeViewSectionAsync(TreeNode selectedNode) { // Initialise. SqlServer instance = null; SqlServer.Database database = null; // Build and expand the TreeNode. Task task = null; task = Task.Factory.StartNew(() =&gt; { string[] tmpStrArr = selectedNode.Text.Split(' '); string strDatabaseName = tmpStrArr[0]; instance = SqlServer.Instance(this.conn); database = instance.GetDatabaseFromName(strDatabaseName); }).ContinueWith(cont =&gt; { instance.BuildTreeViewForSelectedDatabase(this.customTreeViewSql, selectedNode, database); selectedNode.Expand(); task.Dispose(); }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, this.MainUiScheduler); } </code></pre> <p>This works as it should on my main development machine; that is, it completes the build of the <code>database</code> object, then in the continuation update the UI and disposes the <code>task</code> (Task object).</p> <p>However, I have been doing some testing on another machine and I get an <code>InvalidOperationException</code>, this is due to the <code>task.Dispose()</code> on <code>task</code> which still in the <code>Running</code> state, but the continuation <code>cont</code> should never fire unless the task has ran to completion. </p> <p>Here's what the code looks like in the debugger when the exception is thrown:</p> <p><img src="https://i.stack.imgur.com/CMRcg.png" alt="enter image description here"></p> <p><a href="http://blogs.msdn.com/b/pfxteam/archive/2012/03/25/10287435.aspx" rel="nofollow noreferrer">I am aware that it almost always unneccessary to call <code>Dispose</code> on tasks</a>. This question is more about why the continuation is firing at all here?**</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload