Tải bản đầy đủ - 0 (trang)
14-12. Display a Task Dialog

14-12. Display a Task Dialog

Tải bản đầy đủ - 0trang

CHAPTER 14 ■ WINDOWS INTEGRATION



Table 14-8. Selected Properties of the TaskDialog Class



Property



Description



Cancelable



Determines if the user can dismiss the dialog.



Controls



The set of controls embedded within the task dialog. See the recipe code for an

example.



InstructionText



The summary text displayed in the dialog.



StandardButtons



The standard buttons for the dialog. Set with the values of the

TaskDialogStandardButtons enumeration. See the recipe code for an example.



Text



The detailed text, displayed below the InstructionText.



The Code

The following example is a Windows Forms application comprising a button and a text area. To compile

the example, you must build the Windows API CodePack for Microsoft .NET Framework and reference

the Microsoft.WindowsAPICodePack.dll assembly. The user interface is shown in Figure 14-2. We have

added an event handler such that the showElevatedTaskRequest method is called when the button is

clicked.



Figure 14-2. Windows Forms interface



718



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



When the button is clicked, we create a new instance of TaskDialog and configure the basic settings,

allowing the user to cancel the dialog and specifying which buttons should be displayed at the bottom of

the dialog window. We then create an instance of TaskDialogCommandLink, using sample text as the

constructor parameters and add it to the TaskDialog.Controls property. We register an event handler so

that we can add a line of text to the text box on the main window when the elevated task button is

clicked. The dialog interface is shown in Figure 14-3.



Figure 14-3. Elevated task dialog



■ Note Using the task dialog to allow your application to indicate to a user that a particular task requires elevated

privileges does not elevate the privilege level for your application. See recipe 14-15 for details of how to perform

an elevated task.



using

using

using

using

using

using

using

using

using



System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Linq;

System.Text;

System.Windows.Forms;

Microsoft.WindowsAPICodePack.Dialogs;



namespace Recipe14_12

{

public partial class Form1 : Form

{



719



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



public Form1()

{

InitializeComponent();

}

void showElevatedTaskRequest(object sender, EventArgs args)

{

// Create the TaskDialog and configure the basics.

TaskDialog taskDialog = new TaskDialog();

taskDialog.Cancelable = true;

taskDialog.InstructionText = "This is a Task Dialog";

taskDialog.StandardButtons =

TaskDialogStandardButtons.Ok | TaskDialogStandardButtons.Close;

// Create the control that will represent the elevated task.

TaskDialogCommandLink commLink = new TaskDialogCommandLink(

"adminTask", "First Line Of Text", "Second Line of Text");

commLink.ShowElevationIcon = true;

// Add the control to the task dialog.

taskDialog.Controls.Add(commLink);

// Add an event handler to the command link so that

// we are notified when the user presses the button.

commLink.Click += new EventHandler(performElevatedTask);

// display the task dialog

taskDialog.Show();

}

void performElevatedTask(object sender, EventArgs args)

{

textBox1.AppendText("Elevated task button pressed\n");

}

}

}



14-13. Write Custom Performance Counters

Problem

You need to create and write to performance counters to instrument your application.



720



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



Solution

To set up the counters, add one or more instances of System.Diagnostics.CounterCreateData, add them

to an instance of System.Diagnostics.CounterCreationDataCollection, and pass the collection as an

argument to the Create method of the System.Diagnostics.PerformanceCounterCategory class.



■ Note Creating new counters requires administrator privileges.



To write to a counter, create an instance of System.Diagnostics.PerformanceCounter using the same

details you specified when creating the corresponding CounterCreateData instance. Ensure that the

ReadOnly property is false. Use the Increment, IncrementBy, Decrement, and DecrementBy methods to

change the value of the counter.



How It Works

Counters are grouped together in categories. You can determine if a category already exists by using the

PerformanceCategory.Exists method—an exception will be thrown if you try to create a category that

already exists. An individual counter is created using the CounterCreationData class. The three key

properties are CounterName (the name of the counter), CounterHelp (a descriptive string that can be

displayed to the user), and CounterType, which defines the kind of counter that will be created. There are

many kinds of counters available, ranging from simple 32- and 64-bit values to pairs of counters that

must be created together so that Windows can calculate rate information (see the recipe code for an

example of this). The range of counter types available is described in the System.Diagnostic.

PerformanceCounterType enumeration.

Writing to performance counters uses a different set of classes. To write to a counter, create an

instance of the PerformanceCounter class, setting the CategoryName property and CounterName properties

to those you used when creating the category and counters. PerformanceCounter values can be

incremented using the Increment and IncrementBy methods, decremented using the Decrement and

DecrementBy methods, and set to a specific value using the RawValue property.



The Code

The following example creates a new performance counter category called Recipe 14-13 Performance

Counters and populates it with three counters: NumberOfItems32, AverageTimer32, and AverageBase.

Two of the counters are closely related. When creating a counter of the AverageTimer32 type, the

next counter that is created must be of the AverageBase type. The two counters are used together to

report the number of occurrences of an operation over time. We update the AverageBase value to report

how many operations have been performed and the AverageTimer32 value to report how many ticks have

passed since you last updated the AverageBase value.

Having created the category and counters, we then create three instance of PerformanceCounter and

enter a loop so that the counter values are updated randomly.



721



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

14-12. Display a Task Dialog

Tải bản đầy đủ ngay(0 tr)

×