I have one button name called Submit. I would like to trigger Auto Button_Click event for every 5sec.
我有一个名为Submit的按钮名称。我想每5秒触发一次Auto Button_Click事件。
E.g:
例如:
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.
每隔5秒钟,我需要调用此Button_Click事件以自动显示消息,如“欢迎使用Google ....”。
Please help me to solve.
请帮我解决。
3 个解决方案
#1
0
In Wpf
, you could use DispatcherTimer
在Wpf中,您可以使用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();
}
#2
0
create a timer to run every 5 seconds and send: Button_Click(null, null);
创建一个每5秒运行一次的计时器并发送: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);
}
#3
0
It is as simple as this. Create a timer of 5 seconds duration and do this on the timer_tick event.
就这么简单。创建一个持续时间为5秒的计时器,并在timer_tick事件上执行此操作。
buttonName.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
#1
0
In Wpf
, you could use DispatcherTimer
在Wpf中,您可以使用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();
}
#2
0
create a timer to run every 5 seconds and send: Button_Click(null, null);
创建一个每5秒运行一次的计时器并发送: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);
}
#3
0
It is as simple as this. Create a timer of 5 seconds duration and do this on the timer_tick event.
就这么简单。创建一个持续时间为5秒的计时器,并在timer_tick事件上执行此操作。
buttonName.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));