[Description Event is fired when a full page has been dumped event Page DumpedEvent HandlerA Page Dumped

void Dump()

For this new member in the source code, the C++/CLI compiler emits five new members in the managed FileDumper class. First of all, a private backing storage field is defined. This field is a tracking handle to the event handler delegate. Since the event is defined as a public event, there are two public accessor methods defined. These are called add_PageDumped and remove_PageDumped. The first function registers an event handler delegate by adding it to the invocation list; the latter one removes an event handler delegate from the invocation list to unregister it. To fire the event, a third accessor method, raise_PageDumped, is added to the FileDumper class. This method is protected because it should only be possible to fire the event within the implementation of the FileDumper class or a derived class.

Finally, there is the event metadata that binds all the accessor methods together:

.event specialname SampleLib.PageDumpedEventHandler PageDumped {

.addon instance void SampleLib.FileDumper::add_PageDumped(

class SampleLib.PageDumpedEventHandler) .removeon instance void SampleLib.FileDumper::remove_PageDumped(

class SampleLib.PageDumpedEventHandler) .fire instance void SampleLib.FileDumper::raise_PageDumped( object, class SampleLib.PageDumpedEventArgs)

To call the raise_PageDumped method, you can simply use the delegate invocation syntax:

pagesDumped = 0;

StreamReaderA sr = gcnew StreamReader(FileName);

StringA line = nullptr;

DumpPage(sr, line); ++pagesDumped;

PageDumpedEventArgsA ea = gcnew PageDumpedEventArgs(pagesDumped); PageDumped(this, ea); // calls raise_PageDumped(this, ea) if (ea->Cancel) break;

The metadata of the event, including the Category and Description attributes, is read by Visual Studio to display the event in the events view of the Properties window, as Figure 5-9 shows.

Figure 5-9. The PageDumped event in the Properties window

Double-clicking the PageDumped event adds a new event handler function to the hosting component. This event handler has the signature specified by the event handler delegate type.

private:

void fileDumper1_PageDumped( System::ObjectA sender,

SampleLib::PageDumpedEventArgsA ea)

In addition to that, the wizard adds code to InitializeComponent so that the event handler function is registered with the event:

void InitializeComponent(void) {

this->fileDumper1 = (gcnew SampleLib::FileDumper());

// fileDumper1

this->fileDumper1->FileName = "C:\Xdata\test.txt"; this->fileDumper1->PageDumped +=

Was this article helpful?

0 0

Post a comment