I am writing a Translation Manager Event Handler to write some custom business logic on Start and Finish of the translation job. I have tried below code to analyse the different states of the translation job :
[TranslationManagerPlugIn]
public class TMPlugin
{
public TMPlugin()
{
//Subscribe to Translation Manager events
TranslationJobManager.TranslationJobCreated += JobCreated;
TranslationJobManager.TranslationJobLoaded += JobCreated;
}
private void JobCreated(object sender, TranslationJobEventArgs e)
{
TranslationJob job = e.TranslationJob;
job.StateChanged += State_Changed;
}
private void State_Changed(object sender, EventArgs e)
{
var job = (TranslationJob)sender;
if (job.State == TranslationJobState.Completed)
{
log.Debug("Transalation Job Completed ");
}
else if(job.State == TranslationJobState.Canceled)
{
log.Debug("Transalation Job Canceled ");
}
else if (job.State == TranslationJobState.Definition)
{
log.Debug("Transalation Job Definition ");
}
else if (job.State == TranslationJobState.InTranslation)
{
log.Debug("Transalation Job InTranslation " );
}
}
}
The code works well, but the problem is that the code is being invoked several times for a single Translation Job and that too for a single state.
Let’s say I have initiated a job with ID 123, then I am getting the debug msg for “Definition” state but after few minutes I am getting the same Debug message for the same job. The same thing happened with “In Translation” state, it printed somewhere around 15 times the same debug message for the that job ID.
Also I have noticed that if I am initiating a new job say 456, the code runs and print the debug logs for all the previous jobs which are in queue with their current states.
My understanding was that for a particular job ID the code will execute only once & when it will change the state it will print the Debug msg for the next state, in that way I can write my Start logic on “Definition” or “In Translation” state and Finish logic on “Completed” state.
Can someone suggest if I am doing something wrong or subscribing to the wrong events?
Thanks