[dba-VB] C#: Raising events (Part 2)

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Tue Jan 26 10:00:33 CST 2010


Hi John --

Here is a promised WinForms sample code with .InvokeRequired and .Invoke
(Part 2) - you can put code from Part 1 and Part 1 into one code file
replacing Program.cs in Console app, set references to System.Windows.Forms
and System.Drawing and run it:


// 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);
            }
        }
    }
}


 

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4807 (20100126) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru
 




More information about the dba-VB mailing list