I have one button name called Submit. I would like to trigger Auto Button_Click event for every 5sec.
Eg:
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to WPF....");
}
Every 5sec I need to call this Button_Click
event to show Message like "Welcome to Google...." automatically.
Please help me to solve.
create a timer to run every 5 seconds and send: Button_Click(null, null);
public static void Main()
{
var timer = new Timer();
timer.Elapsed+= OnTimedEvent;
timer.Interval=5000;
timer.Enabled=true;
Console.ReadKey();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Button_Click(null, null);
}
In Wpf
, you could use DispatcherTimer
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//Timer
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += (s, ev) => btnClickMe.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
timer.Interval = new TimeSpan(0, 5, 0);
timer.Start();
}
It is as simple as this. Create a timer of 5 seconds duration and do this on the timer_tick event.
buttonName.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.