[dba-VB] C#: Raising events

jwcolby jwcolby at colbyconsulting.com
Mon Jan 25 12:32:21 CST 2010


Shamil,

Unfortunately you are doing the same thing every other example is doing, writing the data to the 
console.  I need to write to a control on a form.

What I can apparently see from this is that the delegate and the event args need to be in the class 
that raises the event?  These exact same things (delegate and event args class) will be used over 
and over again in other classes / forms so I was TRYING to place them down in the class that 
actually implements my stored procedure class.

IOW I have a completely separate project that implements the stored procedure class.  In that I 
placed the eventargument class as well as the delegate.  these three things will be used in many 
many different classes / forms.  Everything needs to run stored procedures and I was TRYING to 
"encapsulate" the delegate as well as the eventargs class in the same container as the stored 
procedure class.

What I am seeing you do is to encapsulate the eventargs class and the delegate in the object that 
raises the event?

John W. Colby
www.ColbyConsulting.com


Shamil Salakhetdinov wrote:
> Hi John --
> 
> Here is one of myriads possible solutions how to handle events in caller
> classes - for form's(/their controls') code behind it will be similar with
> an exception that you might have to use .Invoke while processing events in
> event sinks as the latter could run in a different thread than your form's
> main thread:
> 
> using System;
> 
> namespace ConsoleApplication
> {
> 
>     public class SpRunner
>     {
>         public class  SpRunnerStatusReportEventArgs:EventArgs 
>         {
>             public int CurrentCount {get; set;}
>             public int MaxCount {get;set; }
>         }
> 
>         public EventHandler<SpRunnerStatusReportEventArgs> StatusReport;  
>         public void Run()
>         {
>             const int MAX_COUNT = 5;
>             for (int i = 1; i <= MAX_COUNT; i++)
>             {
>                 if (StatusReport != null)
>                 {
>                     SpRunnerStatusReportEventArgs e = new
> SpRunnerStatusReportEventArgs();
>                     e.CurrentCount = i;
>                     e.MaxCount = MAX_COUNT;
>                     StatusReport(this, e); 
>                 }
>             }
>         }
>     }
> 
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             SpRunner runner = new SpRunner();
>             runner.StatusReport +=
>                 new
> EventHandler<SpRunner.SpRunnerStatusReportEventArgs>(statusReport);
>             runner.Run(); 
>         }
> 
>         private static void statusReport(object sender,
> SpRunner.SpRunnerStatusReportEventArgs e)
>         {
>             System.Console.WriteLine("{0}: Processing {1} of {2}...",
>                sender.GetType(), e.CurrentCount, e.MaxCount);  
>         }
>     }
> }
> 
> Thank you.
> 
> --
> Shamil




More information about the dba-VB mailing list