“The requested reader was not valid. The reader either does not exist or has expired” Error while fetching Performance data in SCOM

Snippet of the script that I am executing :

 $reader = $managementgroupobj.GetMonitoringPerformanceDataReader() 
 while ($reader.Read()) 
 { 
      $perfData = $reader.GetMonitoringPerformanceData() 
      $valueReader = $perfData.GetValueReader($starttime,$endtime) 
      while ($valueReader.Read()) 
      { 
           $perfValue = $valueReader.GetMonitoringPerformanceDataValue()
      } 
 }

Here, $managementgroupobj is an instance of class ManagementGroup.

The difference of $starttime and $endtime veries from 15 minutes to 1 hour depending on the last execution of the same script.

The snippet collects the performance the data successfully for long time. but then, out of nowhere it throws following error:

“The requested reader was not valid. The reader either does not exist or has expired”

  • What is the cause of the mentioned error.?
  • I still have confusion with Read() method. What is the significance of it.?
  • It would be great if I get to know the mechanism of the PerformanceDataReader.
Note:
  • The amount of data it fetched before getting error was 100k+. and it took almost an hour to fetch that data.
  • I think the possible issue was with amount of data it has to fetch, It might be a kind of TimoutException.
  • It would be great if I get atleast some knowledge of all 3 questioned mention above.
Thanks.