Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Other Tricks You Can Perform

Don't get the idea that Server Explorer limits your interaction with the objects it presents. If you can drill down to an object and want to determine whether you can use it, simply drag it to a form and see what happens. For example, you don't have to select all the fields in a table. You can highlight just the fields you want to use and drag them to the form. However, make sure you Ctrl+Click to select all the fields you want to use and drag them all at the same time, or you'll end up with one data adapter for each field.

Sometimes you need to work with a performance monitor. For example, you might want to sense when the system is overloaded and cause nonessential threads to sleep, to free system resources. Server Explorer can help. All you need to do is drag the performance monitor you want to use to the form. I'll use the Processor\% User Time\_Total counter for this example, but you can use any counter.

To the Performance Counter, you need to add a timer (to time the counter intervals), a DataSet (to hold the intermediate timer values), and a DataGrid (to the display the values). Configure the DataSet to hold a single table, PctUser, with a single column, UserTime. Set the timer1 Interval property to some reasonable value, such as 500 milliseconds. All you need to do to make this example functional is the timer1_Tick() event handler, shown in Listing 3.

Listing 3: Defining a Performance Counter Event Handler

private: System::Void timer1_Tick(System::Object * sender, 
         System::EventArgs * e)
   {
   DataTable* CounterTable;
   DataRow* NewRow;

   // Create the data table object.
   CounterTable = 
    dataSet1->Tables->get_Item("PctUser");

   // Add a new row to the data table.
   NewRow = CounterTable->NewRow();

   // Obtain the current performance counter value.
   NewRow->set_Item("Total % User Time", 
    __box(performanceCounter1->NextValue()));

   // Store the value in the data table.
   CounterTable->Rows->Add(NewRow);
   
   // Remove old rows from the data table.
   if (CounterTable->Rows->Count >= 
     dataGrid1->VisibleRowCount)
    CounterTable->Rows->RemoveAt(0);
   }

Every time timer1 ticks, the application generates another dataSet1 entry based on the current performanceCounter1 value. To perform this task, the code creates a copy of the PctUser DataTable, creates a new row for that table, adds the value to the "Total % User Time" column, and adds the row to the table. If the table is getting too long, the code automatically shortens it. Figure 3 shows typical output from this example.

Figure 3Figure 3 Performance monitors have a number of useful purposes.

Of course, timer1 won't start by itself, so you'll need to perform this task as well. I used the simple FormLoad event handler shown in Listing 4.

Listing 4: Starting the Performance Monitor Timer

private: System::Void Form1_Load(System::Object * sender, 
         System::EventArgs * e)
   {
   // Start the timer.
   timer1->Start();
   }
  • + Share This
  • 🔖 Save To Your Account