Is that code correct?

Nov 18, 2011 at 8:48 AM
Edited Nov 18, 2011 at 8:59 AM

Hello!

I'm too lazy to write ITask implementation (I mean Initialize method with complex parameters), so i wonder is this code correct?

 

class InlineTask :ITask
	{
		private Action _action;
		public void HandleConditionsMetEvent(object sender, ConditionsMetEventArgs e)
		{
			try
			{
				if (_action != null) _action.Invoke();
			}
			catch
			{
				throw new Exception("Error during executing inline task");
			}
			
		}

		public void Initialize(ScheduleDefinition schedule, object parameters)
		{
			try
			{
				_action = (Action) parameters;
			}
			catch
			{
				throw new Exception("Cannot initialize task");
			}
		}
	}
	static class SchExtensions
	{
		public static void AddExecution(this Schedule sche, Action inlineTask)
		{
			sche.Execute<InlineTask>(inlineTask);
		}
	}

 

and then using it as below:

 

var schedule = new Schedule("Diagnostic");

var parameters = new {Name = "Joe", Id = 42};
schedule.AddExecution(
				delegate
					{
						Console.WriteLine(parameters.Name);
						Console.WriteLine(parameters.Id);
					}
				);

 

Coordinator
Nov 19, 2011 at 10:46 PM

I think it "should" work. I'm not sure about the scope & lifetime of var parmeters, but the delegate ought to fire. Also check out the discussion of therading on the home page, make sure you're okay with how these tasks will execute if you have multiple InlineTasks.

Nov 21, 2011 at 6:43 AM
Edited Nov 21, 2011 at 6:48 AM

Thanks for reply!