I had a lot of functions like this, all running on BackgroundWorker threads, on a status page that refreshed itself every three minutes. Most of them queried different database, most of them also were hooked up to more complicated Dundas Gauges and Charts. Here’s one that’s really simple, but illustrates accessing a particular MSMQ, which needed impersonation of a service account.
private void loadMSMQGauge()
{
int numInQueue = -1;
try
{
Utils.ImpersonateUser iu = new Utils.ImpersonateUser();
iu.Impersonate("corp", "someserviceaccount", "password");
//PerformanceCounter pc = new PerformanceCounter("MSMQ Queue",
// "Messages in Queue", "someservername\\private$\\ssome_error_queue", "someservername");
// HP openview kept horking the msmq perf counter, after re-registering the counters several times
// and getting horked again after reboots we will take the long route.
using (MessageQueue mq = new MessageQueue("FormatName:DIRECT=OS:someservername\\private$\\some_error_queue", QueueAccessMode.Peek))
{
Message[] messages = mq.GetAllMessages();
numInQueue = messages.Length;
}
this.GaugeContainer1.LinearGauges[4].Pointers[0].Value = numInQueue; // pc.RawValue;
this.GaugeContainer1.NumericIndicators[4].Value = this.GaugeContainer1.LinearGauges[4].Pointers[0].Value;
iu.Undo();
}
catch (System.InvalidOperationException) // this was from PerfCounter object
{
//empty queue, do nothing
}
catch (MessageQueueException ex) //this comes from mq object, just write it out also
{
Response.Write(ex.ToString());
}
catch (Exception ex)
{
Simple hookup, but it showed on a Widescreen TV hanging on the wall of our department, the current state of one of our systems, at a glance, with big digital numbers and red and yellow colors.