[dba-VB] C#: Raising events

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Tue Jan 26 00:20:46 CST 2010


John --

As I noted my sample code was one of myriads possible variants - below it
yet another one - just a sample.

As for setting form controls' properties you have to use .InvokeRequired and
.Invoke to check calling context and to properly set controls' properties -
you can google/bing for such samples of wait here for some to appear in this
or related threads.

Thank you.

--
Shamil

P.S.

using System;

// classlib1
namespace My.CoreClassLib
{
    public class SpRunnerStatusReportEventArgs : EventArgs
    {
        public int CurrentCount { get; set; }
        public int MaxCount { get; set; }
    }
}

// classlib2
namespace My.UtilsClassLib
{
    public class SpStatusReporter
    {
        public SpStatusReporter(
             EventHandler<My.CoreClassLib.SpRunnerStatusReportEventArgs>
statusReport)
        {
            _statusReport = statusReport; 
        }

        private EventHandler<My.CoreClassLib.SpRunnerStatusReportEventArgs>
_statusReport;
        public void ReportStatus(int currentCount, int maxCount)
        {
            if (_statusReport != null)
            {
                My.CoreClassLib.SpRunnerStatusReportEventArgs e =
                    new My.CoreClassLib.SpRunnerStatusReportEventArgs();
                e.CurrentCount = currentCount ;
                e.MaxCount = maxCount;
                _statusReport(this, e);
            }
        }
    }
}

// classlib3
namespace My.BusinessLayerClassLib
{
    public class SpRunnerManager
    {
        public EventHandler<My.CoreClassLib.SpRunnerStatusReportEventArgs>
StatusReport;
        private My.UtilsClassLib.SpStatusReporter _reporter;
        public void Run()
        {
            if (_reporter == null)
                _reporter = new
My.UtilsClassLib.SpStatusReporter(StatusReport); 
            
            const int MAX_COUNT = 5;
            for (int i = 1; i <= MAX_COUNT; i++)
            {
                _reporter.ReportStatus(i, MAX_COUNT);   
            }
        }
    }
}

// front-end
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            My.BusinessLayerClassLib.SpRunnerManager runner = 
                new My.BusinessLayerClassLib.SpRunnerManager();
            runner.StatusReport +=
                new
EventHandler<My.CoreClassLib.SpRunnerStatusReportEventArgs>(statusReport);
            runner.Run(); 
        }

        private static void statusReport(object sender, 
             My.CoreClassLib.SpRunnerStatusReportEventArgs e)
        {
            System.Console.WriteLine("{0}: Processing {1} of {2}...",
               sender.GetType(), e.CurrentCount, e.MaxCount);  
        }
    }
}
 

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4805 (20100125) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru
 




More information about the dba-VB mailing list