Reputation: 322
How can I run a function, after 10 seconds, after the opening of the program.
This is what I tried, and I'm not able to make it work.
private void button1_Click(object sender, EventArgs e)
{
Timer tm = new Timer();
tm.Enabled = true;
tm.Interval = 60000;
tm.Tick+=new EventHandler(tm_Tick);
}
private void tm_Tick(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
Upvotes: 3
Views: 6895
Reputation: 612794
You have a few problems:
Load
event rather than a button click handler.10000
for a 10 second wait.In other words, something like this:
private Timer tm;
private void Form1_Load(object sender, EventArgs e)
{
tm = new Timer();
tm.Interval = 10 * 1000; // 10 seconds
tm.Tick += new EventHandler(tm_Tick);
tm.Start();
}
private void tm_Tick(object sender, EventArgs e)
{
tm.Stop(); // so that we only fire the timer message once
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
Upvotes: 14
Reputation: 609
Is will be good for your program something like that?
namespace Timer10Sec
{
class Program
{
static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(After10Sec));
t.Start();
}
public static void After10Sec()
{
Thread.Sleep(10000);
while (true)
{
Console.WriteLine("qwerty");
}
}
}
}
Upvotes: 0