02-03-2014 10:31 AM
Hi,
I am learning to use Notification to communicate between thread. I still don't know how to use it. In my current project I have a case to use this so I think I should implement this technique. My example is I have a main sequence in that I make a new thread. What I want is the new thread will do a task A when receive a notfication or to exit the thread when the main sequence stop.
I am not sure if using notification is a best practice here? Or should I use a reference data?
What about if I want my thread to do task B if it receive another notification. Is notification still be helpful?
Regards,
Thang Nguyen
Solved! Go to Solution.
02-03-2014 11:12 AM - edited 02-03-2014 11:18 AM
Hello,
You can use Notification to handle parallel threads. I often create two sequences : one containing the process A of your example (StartProcessA). and a second one to stop this process (StopProcessA). In StartProcessA, I create a Notification, with a defined name (as string constante, "StopProcessANotifier" for instance) in the setup group. Then I have the process, in a loop. At the end of this loop, I check the Notification (Wait) with a timeout enabled, and no error if a timeout occurs. this way, the Notification Wait can be used as loop timing, and if the notification is Set in MainSequence, this exit the loop (pick a look at Notification property Step.Result.TimeoutOccured).
To sum up, use a first sequence call in MainSequence, calling StartProcessA, with NewThread option, and do not forget to call StopProcessA at the end of you MainSequence. The Cleanup group is a very good location, because it will be executed even in case of error.
This is a very simple use of Notification. Note, I believe that make it simple is often the most important but also the most complicated thing to do
Hope this helps....
02-03-2014 12:30 PM
Mathieu,
Thank you for your reply.
Could you please tell more specific how to set up the notification? Or it will be best if you have a sample code. Is it based on the string name of the notification? As I understand, in the parallel sequence you create a notification and wait for the event from this notification. On the main sequence you just use only a notification step with the identification string name with the same with the one create inside the parallel sequence and set event to it. Am I correct?
Regards,
Thang Nguyen
02-03-2014 01:10 PM
That's correct, you can call the Notification - Set directly from the MainSequence indeed.
Here is a example : the sequence StopProcessA is not absolutely required - it is just for modularity purposes.
Best regards,
02-03-2014 01:15 PM - edited 02-03-2014 01:15 PM
Mathieu,
Thank you for your sample code!
Regards,
Thang Nguyen