Know that a event was fired

rfazendeiro

Centurion
Joined
Mar 8, 2004
Messages
110
hi to all,

I have a dll that i use to execute a dts through a c# application. The dll has 2 classes in it. One to execute the dts and the other to "catch" / treat the events that the dts fires.

While the dts is executing it fires an event (among others) that indicates the progress of the execution. The event is something like this

[csharp]
public void OnProgress(string EventSource, string ProgressDescription, int PercentComplete, int ProgressCountLow, int ProgressCountHigh)
{
//code here
}
[/csharp]

Now in the other class in the same dll i have a method called ExecutePackage that executes the dts.

im my form all i do is create a instance o the class that has this method and execute it

[csharp]
DTS execute = new DTS();
execute.ExecutePackage();
[/csharp]


very well. Now comes the question. How can my main app know that the OnProgress events was fired so i can increment the progressbar?


NOTE: The event OnProgress is working because i can create in the Eventlog (System.Diagnostics.EventLog) a event telling me the progress
 
Is there some reason why you can't simply create a handler in your Form with the ProgressBar and attatch that handler to the OnProgress event? The only concern that comes to mind is that you need to ensure that the GUI is not updated from a separate thread, meaning (I think) that you need to use Control.Invoke or Control.BeginInvoke/Control.EndInvoke to ensure that the code is executed on the appropriate thread.
C#:
class MyForm: Form
{
    public MyForm() {
        InitializeComponents();
        
        // Put this wherever appropriate
        DTS execute = new DTS();
        // If OnProgress is an event of another object substitute it here.
        // C# 2.0 +
        DTS.OnProgress += MyHandler;
        // C# 1.0 +
        DTS.OnProgress += new WhateverDelegate(MyHandler);
    }
    void MyHandler(string EventSource, string ProgressDescription, 
    int PercentComplete, int ProgressCountLow, int ProgressCountHigh) {
        //Do whatever
    }
}
 
well.

I currently don't fully understant exactly how delegates and handlers work so i ask for some help. Is there any book you would recomend to read and understand a little better this part?


i'm trying to implemente what you said but i'm getting a error. I guess this maybe easy but i dont quite understand so any adicional help would very much be appreciated

(36): 'ExecuteDTS.PackageEventsSink.OnProgress(string, string, int, int, int)' denotes a 'method' which is not valid in the given context


thx for the help
 
Last edited:
Why not post the code where the event is declared (find the definition in the object browser or the code "generated from metadata") and the code where you attempt to attatch the handler.
 
the following code is the class that handles the events fired by the dts. I currently send information to the eventlog to teste it
C#:
//This class is responsible for handling DTS Package events. When an event is fired, a message is sent to the EventLog
//This class is responsible for handling DTS Package events. When an event is fired, a message is sent to the EventLog
public class PackageEventsSink : PackageEvents
{
	public PackageEventsSink()
	{
	}

	public void OnQueryCancel(string EventSource, ref bool pbCancel)
	{
		EventLog.WriteEntry("TestSource"," In OnQueryCancel  : Event Source  : "  + EventSource);				
			pbCancel = false;
	}

	public void OnStart(string EventSource)
	{	
		EventLog.WriteEntry("TestSource"," In OnStart  : Event Source  : "  + EventSource);				
	}

	public void OnProgress(string EventSource, string ProgressDescription, int PercentComplete, int ProgressCountLow, int ProgressCountHigh)
	{
		EventLog.WriteEntry("TestSource"," In OnProgress  : Event Source  : "  + EventSource +
				" Progress Description : " + ProgressDescription +
				" PercentComplete : " + PercentComplete + 
				" ProgressCountLow : " + ProgressCountLow +
				" ProgressCountHigh : " + ProgressCountHigh);								
	}

	public void OnError(string EventSource, int ErrorCode, string Source, string Description, string HelpFile, int HelpContext, string 		
			IDofInterfaceWithError, ref bool pbCancel)
	{
		EventLog.WriteEntry("TestSource"," In OnError : Event Source  : "  + EventSource +
				" Error Code : " + ErrorCode.ToString() + " Source : " + Source.ToString() + 
				" Description : " + Description + " HelpFile : " + HelpFile +
				" HelpContext : " + HelpContext +  " InterfaceError " + IDofInterfaceWithError);
			pbCancel = false;
	}

	public void OnFinish(string EventSource)
	{
		EventLog.WriteEntry("TestSource", "In OnFinish");
	}
}



this class handles the dts execution

C#:
public class DTS
{
	public Microsoft.SqlServer.DTSPkg80.PackageClass package;
	public Microsoft.SqlServer.DTSPkg80.DataPumpTask2 dataPump;

	public DTS()
	{
	}

	public void ExecutePackage(string fileName)
	{
		try
		{
			package = new Microsoft.SqlServer.DTSPkg80.PackageClass();
			UCOMIConnectionPointContainer CnnctPtCont = (UCOMIConnectionPointContainer) package;
			UCOMIConnectionPoint CnnctPt;
			PackageEventsSink PES = new PackageEventsSink ();
			Guid guid = new Guid("10020605-EB1C-11CF-AE6E-00AA004A34D5");  // UUID of PackageEvents Interface
			CnnctPtCont.FindConnectionPoint(ref guid, out CnnctPt);
			int iCookie;
			CnnctPt.Advise(PES, out iCookie);
			object pVarPersistStgOfHost = null;
			//Retrieve global settings from the Configuration File
			string serverName = ConfigurationSettings.AppSettings["serverName"];
			string toMailAddress = ConfigurationSettings.AppSettings["toMailAddress"];
			string userName = ConfigurationSettings.AppSettings["userName"];
			string password = ConfigurationSettings.AppSettings["password"];
			string packageName = ConfigurationSettings.AppSettings["packageName"];		

			package.LoadFromSQLServer(serverName,userName, password, DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, null, 
					null, null, packageName, ref pVarPersistStgOfHost);
		        
			//Loop through all the Global variables and remove the variables that are of type string
			foreach(GlobalVariable global in package.GlobalVariables)
			{
				if (global.Name == "SourceFilePath")	
				{
						package.GlobalVariables.Remove(global.Name);						
				}
				if (global.Name == "SqlServer")	
				{
					package.GlobalVariables.Remove(global.Name);					
				}			
			}		
				
			//Re-add all the global variables that are of type string
				package.GlobalVariables.AddGlobalVariable("SourceFilePath",fileName);
				package.GlobalVariables.AddGlobalVariable("SqlServer",serverName);

				//Execute the Package
				package.Execute();
				package.UnInitialize();
				package = null;
		}
		catch(System.Runtime.InteropServices.COMException ex)
		{
			EventLog.WriteEntry("TestSource","Error Code : "  + ex.ErrorCode 
					+ "Error Message :" + ex.Message + " Stack Trace : " +  ex.StackTrace);
		}

		catch(System.Exception ex)
		{
			EventLog.WriteEntry("TestSource", "Error Message :" + ex.Message + " Stack Trace : " +  ex.StackTrace);
		}
	}
}


I have this two classes in a dll that i use in my aplication. I have a button that starts the process (starts executing the dts) and with the event onProgress i want to increment my progress bar.

thois is the code i have to start the process

C#:
private void button1_Click(object sender, System.EventArgs e)
{
	Thread trdProcesso = new Thread(new ThreadStart(StartDTSExecution));
	trdProcesso.Name = "DTS";
	trdProcesso.Start();
}


private void StartDTSExecution()
{
	DTS execute = new DTS();

	execute .ExecutePackage(fileNamePath);
}
 
Last edited by a moderator:
Can anyone give me a help on this? plz?

i'm a little lost here and don't know where to start.

How can i know that the onProgress events was fired in the class PackageEventsSink so i can incremente my progressbar im my main form?

thx to all
 
I've been playing a little with delegates and events and discovered a way to do what i wanted that was in the main form know that the OnProgress was fired. I just wanted to share how acomplished this to know if this is the best way of doing it.


In the PackageEventsSink class i declared a delegate and a event like this

C#:
public delegate void ProgressHandler(string EventSource, string ProgressDescription, int PercentComplete, int ProgressCountLow, int ProgressCountHigh);

public static event ProgressHandler progress;


then i change the OnProgress method like this

C#:
public void OnProgress(string EventSource, string ProgressDescription, int PercentComplete, int ProgressCountLow, int ProgressCountHigh)
{
	progress(EventSource, ProgressDescription, PercentComplete, ProgressCountLow, ProgressCountHigh);
}


Now in the DTS class i just change the construct to recieve a object of type PackageEventsSink


C#:
public class DTS
{
             private PackageEventsSink _PES;

             public DTS(PackageEventsSink PES)
	{
                       _PES = PES;
	}

             ...
}


Now it's time for the main form, where i use the events i did

C#:
class MyForm: Form
{
        PackageEventsSink pes;
        public MyForm() 
        {
                 InitializeComponents();
                 
                 pes = new PackageEventsSink()
                 PackageEventsSink.progress += new PackageEventsSink.ProgressHandler(PackageEventsSink_progress); 
        }

        private void PackageEventsSink_progress(string EventSource, string ProgressDescription, int PercentComplete, int ProgressCountLow, int ProgressCountHigh)
        {
                //increment progress bar
                progressbar1.PerformStep();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
	Thread trdProcesso = new Thread(new ThreadStart(StartDTSExecution));
	trdProcesso.Name = "DTS";
	trdProcesso.Start();
        }
 
 
         private void StartDTSExecution()
         {
	DTS execute = new DTS(pes);
 
	execute .ExecutePackage(fileNamePath);
         }
 }


just a question about this forum.

whem i put c# code i use the tags [ccsharp][/ccsharp] (the double cc is to show the tag i use) but the color don't seem to get it right. How can i put the colors in the code?
 
Last edited by a moderator:
Back
Top