Delegates and Events... please help

P

pbd22

Hi.

I am stuck on a part of my program and could use some help.
I have a block of code that opens and writes to a text file.
The idea is to keep the file open and keep writing to it until
the EndOfStream event is fired.

My project is a carry-over from a VB6 project and the Extractor()
classes comes with a number of events for this particular part of
the program. The ones of note are the DataEventHandler that
keeps writing data to the text file until the EndOfStreamEventHandler
is fired.

I am not too strong in delegates and event handlers and I would
really
appreciate some guidance as to how to use these two event handlers
to loop, creating more text, until the end of stream is reached.

Below is what I have so far but this doesn't work (I get an empty
file):


private static void Test()
{

data = new Extractor();

data.OnData += new _DataEventHandler(data_Data);
data.EndOfStream += new
_EndOfStreamEventHandler(data_EndOfStream);

using (sw =
System.IO.File.CreateText(videofile.Replace(".mpg", ".txt")))
{

data_Data(strTimeCC, strCC);

}

}

void data_Data(string strTime, string strText)
{
try
{

sw.WriteLine(strTime + Constants.vbTab +
strText.Replace("<br>", " "));
sw.Flush();

}
catch
{
throw new Exception("The method or operation
data_OnCCData is not implemented.");
}

}


void data_EndOfStream()
{
try
{
sw.WriteLine(" ");
sw.Close();
}
catch
{
throw new Exception("The method or operation
data_EndOfStream() is not implemented.");
}

}
 
J

Jon Skeet [C# MVP]

pbd22 said:
I am stuck on a part of my program and could use some help.
I have a block of code that opens and writes to a text file.
The idea is to keep the file open and keep writing to it until
the EndOfStream event is fired.

One problem is that you're using a "using" statement which is also
closing the StreamWriter. It's not entirely clear to me what the
sequence of actions is in your code - you haven't shown anything that
actually uses the extractor.

I'd also suggest removing the try/catch from your two event handlers -
all they do is mask the original exception, and give incorrect messages
(as data_EndOfStream() most certainly *is* implemented, for example).
 
P

pbd22

I'd also suggest removing the try/catch from your two event handlers -
all they do is mask the original exception, and give incorrect messages
(as data_EndOfStream() most certainly *is* implemented, for example).


Thanks for your reply John.
sequence of actions is in your code - you haven't shown anything that
actually uses the extractor.

I thought I was using the extractor when I referenced the data.OnData
handler
and then called:

data_Data(strTimeCC, strCC);

inside the using block. Can you tell me where I am going wrong here?

Also, I feel like I need some sort of loop inside the using block.
Here is the sequence I am looking for in pseudo code:

using (create the file)
{
while we are not at the end of the stream
{

keep extracting data and writing that data to the .txt file.

}

}

I don't really have much to offer you in the way of errors. I get
a .txt file
with no contents and not run-time errors. Of course, nothing got
written.

Thanks a ton.
Peter
 
J

Jon Skeet [C# MVP]

pbd22 said:
I thought I was using the extractor when I referenced the data.OnData
handler
and then called:

data_Data(strTimeCC, strCC);

Well, that's adding the handler - but it's not calling any methods on
"data" (the Extractor).
inside the using block. Can you tell me where I am going wrong here?

Also, I feel like I need some sort of loop inside the using block.

It's really hard to say without knowing what the Extractor code looks
like, or what it's meant to do. I'd expect you to just call a method on
the Extractor though.
 
P

pbd22

Thanks again...
Well, that's adding the handler - but it's not calling any methods on
"data" (the Extractor).

So, this code adds the handler, right?:

data.OnData += new _DataEventHandler(data_Data);

And the data_Data method executes when the event fires, right?

But, how do I trigger the event? I guess that is what you mean by
"calling any methods on 'data' ". There should be an additional
method that fires the event, right? I thought this was the case but
when I intellisense data I just get "handlers" that require that I
keep
them on the right hand side of the "+= or -=" sign.

I guess I am wondering how I fire the event given my set-up. As for
what the Extractor looks like, I would like to know also. Is it
possible
to know what the code in a DLL looks like?

Thanks again.
 
P

Peter Duniho

[...]
But, how do I trigger the event? I guess that is what you mean by
"calling any methods on 'data' ". There should be an additional
method that fires the event, right? I thought this was the case but
when I intellisense data I just get "handlers" that require that I keep
them on the right hand side of the "+= or -=" sign.

An event can be raised for any reason a class decides is appropriate. A
class might include some public method that will raise the event (for
example, Button.PerformClick), but more commonly an event exists so that
it can be raised as a consequence of some actual operation that the class
is doing, or in a situation where the class needs the handler to perform
some work for it.

In those latter cases, there wouldn't be a specific method per se that
raises the event. Rather, you subscribe to the event so that your code
can be called at appropriate times to do whatever's needed. The raising
of the event would be a natural consequence of whatever the class is doing.

As Jon says, since we know nothing about the Extractor class, it's
impossible to say what specifically will cause the Extractor.OnData event
to be raised. Just judging from the names, it _seems_ like the Extractor
class is something that you point it at something that has data, tell it
to work on that, and when it's done (or perhaps at certain intervals), it
raises the OnData event to allow your own code to do something useful.
I guess I am wondering how I fire the event given my set-up. As for
what the Extractor looks like, I would like to know also. Is it possible
to know what the code in a DLL looks like?

If the DLL is managed code, you can use the Reflector tool to decompile
it. That might give you some insight. Of course, a better option would
be to contact the author/publisher of Extractor and get documentation on
how the class is supposed to be used. You should not have to look at the
implementation in order to correctly use the OnData event or any other
aspect of the class for that matter.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top