title: ThreadJob: The WriteObject and WriteError cannot be called from outside the overrides of the BeginProcessing, ... EndProcessing methods?
body: <p>I've installed ThreadJob and the following code works fine.</p>
  
  <pre><code>$commands = 
      {$r1 = Start-Sleep -Seconds 3; 'r1' }, 
      {$r2 = Start-Sleep -Seconds 3; 'r2' }
  
  $jobs = $commands | ForEach-Object { Start-ThreadJob $_ }
  $jobs | Receive-Job -Wait -AutoRemoveJob
  </code></pre>
  
  <p>Now I tried to change it to accessing database.</p>
  
  <pre><code>$commands = 
      {$r1 = Invoke-Sqlcmd -ServerInstance Server1 'select 1 a' }, 
      {$r2 = Invoke-Sqlcmd -ServerInstance Server2 'select 2 a' }
  
  $jobs = $commands | ForEach-Object { Start-ThreadJob $_ }
  $jobs | Receive-Job -Wait -AutoRemoveJob
  </code></pre>
  
  <p>And now it got the error of</p>
  
  <blockquote>
    <p>The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread. Validate that the cmdlet makes these calls correctly, or contact Microsoft Customer Support Services.</p>
  </blockquote>
  
  <p>It works after changing <code>Start-ThreadJob</code> to <code>Start-Job</code>.</p>