Very weird bug... Nevermind, I figured it out.

Aug 4, 2016 at 4:32 PM
I keep getting a NotV1SupportedException when calling the RegisterChanges() function on a Task object.

This is the code that I'm using...
Using taskServiceObject As TaskScheduler.TaskService = New TaskScheduler.TaskService()
  Dim task As TaskScheduler.Task = taskServiceObject.GetTask("my task")

  If task IsNot Nothing Then
    ' Do some changes to the task here
    task.RegisterChanges()
    task.Dispose()
  End If
End Using
When it gets to the RegisterChanges() function it immediately crashes.

But if I use this code instead...
Dim taskServiceObject As New TaskScheduler.TaskService()
Dim task As TaskScheduler.Task = taskServiceObject.GetTask("my task")

If task IsNot Nothing Then
  ' Do some changes to the task here
  task.RegisterChanges()
  task.Dispose()
End If

taskServiceObject.Dispose()
It works just fine with no crash upon the call of RegisterChanges().

Theoretically speaking, both code examples should work but it isn't. Unless the .NET Framework is doing something wrong internally.
Aug 4, 2016 at 5:11 PM
Edited Aug 4, 2016 at 7:49 PM
Nevermind, I figured out what I was doing wrong.

Because of the way my code is laid out in the program the Using statement is disposing of the taskServiceObject Object thus when it came time to run the RegisterChanges() function call the function crashes because there's no taskServiceObject for it to reference back to because it's been disposed.

Basically it's like this...
Public Sub doChangeToTask()
  Dim task As TaskScheduler.Task = Nothing
  If doesTaskExist("My Task", task) = True Then
    ' Do changes to task.
    task.RegisterChanges() ' This is where it would crash.
  End If
End Sub

Private Function doesTaskExist(ByVal nameOfTask As String, ByRef taskObject As TaskScheduler.Task) As Boolean
  Using taskServiceObject As TaskScheduler.TaskService = New TaskScheduler.TaskService()
    taskObject = taskServiceObject.GetTask(nameOfTask)

    If taskObject Is Nothing Then
      Return False
    Else
      Return True
    End If
  End Using
End Function
I feel very stupid right now. I should have known that by using a Using construct it would cause the RegisterChanges() function to fail since there's no taskServiceObject for it to act on; it was disposed of at the end of the Using construct. Sometimes I want to put my head through the wall.

The right way to do it is this way...
Public Sub doChangeToTask()
  Dim task As TaskScheduler.Task = Nothing
  If doesTaskExist("My Task", task) = True Then
    ' Do changes to task.
    task.RegisterChanges() ' This is where it would crash.
  End If
End Sub

Private Function doesTaskExist(ByVal nameOfTask As String, ByRef taskObject As TaskScheduler.Task) As Boolean
  Dim taskServiceObject As New TaskScheduler.TaskService()
  taskObject = taskServiceObject.GetTask(nameOfTask)

  If taskObject Is Nothing Then
    ' It's OK to dispose of the taskServiceObject Object here since
    ' we know that the task we were looking for doesn't exist.
    taskServiceObject.Dispose()
    Return False
  Else
    ' Don't dispose of the taskServiceObject Object since we will need it
    ' for the doChangeToTask() function that's calling this function.
    Return True
  End If
End Function
Like I said... Sometimes I want to put my head through the wall.
Marked as answer by trparky on 8/4/2016 at 10:11 AM