Hi,
I’m running a simple C# program for modifying objects within RobotStudio 5.12 I and want to call a method regularly within VSTA using the System.Timers.Timer Class. I have setup the a private timer, set the interval, enabled the timer and set it to call and ElapsedEventHandler every time the ElapsedEventArg occurs (currently set to 1 second intervals). I can call methods within the VSTA Macro public partial class, and can run some methods within the ElapsedEventHandler, but when I want to do something such as setting a station signal from within the ElapsedEventHandler, nothing happens. What I am doing wrong here? If I set the station signals from another method or macro, it works fine.
///// SAMPLE CODE /////
//Create the timer
private Timer myTimer;
//Setup Timer and ElapsedEventHandler and output enabled status to Output Window.
public void Macro_StartTimer()
{
myTimer = new Timer();
myTimer.Interval = 1000;
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;
Logger.AddMessage(new LogMessage("myTimer Enabled: " + myTimer.Enabled.ToString()));
}
// Send a message to Output Window everytime timer elapses.
// Change signal of digital station signal “Digital1”
void myTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Logger.AddMessage(new LogMessage("myTimer elapsed at " + e.SignalTime.TimeOfDay.ToString()));
Station station = Project.ActiveProject as Station;
if (station.VirtualSignals[“Digital1”].Equals(0))
{
station.VirtualSignals[“Digital1”] = 1;
}
else
{
if (station.VirtualSignals[“Digital1”].Equals(1))
{
station.VirtualSignals[“Digital1”] = 0;
}
}
throw new Exception(“The method or operation is not implemented.”);
}
// Stop the timer
public void Macro_StopTimer()
{
myTimer.Enabled = false;
Logger.AddMessage(new LogMessage("myTimer Enabled: " + myTimer.Enabled.ToString()));
}
///// END SAMPLE CODE /////
Thanks,
Matt.