C # Create threads three ways

Source: Internet
Author: User

1, the use of System.Timers.Timer to create a thread, while refreshing the interface display, the use of synchronizingobject properties, there is a card dead state.

System.Timers.Timer t = new System.Timers.Timer (); private void Button_Click (object sender, EventArgs e)
{
T.interval = 100;
t.elapsed + = new System.Timers.ElapsedEventHandler (t_elapsed);
T.enabled = true;
T.synchronizingobject = Label1;
}
void T_elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
for (int i = 0; i < i++)
{
System.Threading.Thread.Sleep (50);
Label1. Text = i.ToString ();
}
t.enabled = false;
}

2, the use of ystem. Threading.thread Create a thread, refresh the interface, and the interface can be displayed in real time
private void Button1_Click (object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread (new System.Threading.ThreadStart (Run));
Th. Start ();
Label1. Text = "";
}
void Run ()
{
for (int i = 0; i < i++)
{
Call (i);
}
T.stop ();
}
delegate void Upcallback (int i);
void call (int i)
{
if (this. invokerequired)
{
Upcallback up = new Upcallback (call);
This. Invoke (UP, new object[] {i});
}
Else
{
System.Threading.Thread.Sleep (200);
Label1. Text = i.ToString ();
}
}

3, the use of System.Timers.Timer to create a thread, while refreshing the interface display, there is a card dead state.
private void Button2_Click (object sender, EventArgs e)
{
System.Timers.Timer Timer = new System.Timers.Timer ();
Timer. Enabled = true;
Timer. Elapsed + = new System.Timers.ElapsedEventHandler (timer_elapsed);
Timer. Interval = 5000;
}
void Timer_elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
for (int i = 0; i < i++)
{
Call (i);
}
}
3, the use of System.Windows.Forms.Timer to create a thread, while refreshing the interface display, there is a card dead state.

private void Button3_Click (object sender, EventArgs e)
{
System.Windows.Forms.Timer tr = new System.Windows.Forms.Timer ();
Tr. Interval = 5000;
Tr. Tick + = new EventHandler (Tr_tick);
Tr. Start ();
}
void Tr_tick (object sender, EventArgs e)
{
for (int i = 0; i < i++)
{
Call (i);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.