WPF, WinForm (C #) multithreaded Programming and update interface (UI) (reproduced accumulation)

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Drawing;
Using System.Linq;
Using System.Windows.Forms;
Using System.Threading;

Namespace Doworker
{
public partial class Form1:form
{
delegate void MyDelegate (int value);
Thread T;
int i = 0;
Public Form1 ()
{
InitializeComponent ();
}

Do a "long time" job in a new thread
private void Button1_Click (object sender, EventArgs e)
{
t = new Thread (doWork);
T.start ();
}

Work to be done for a long time
void DoWork ()
{
MyDelegate d = new MyDelegate (setValue);
while (true)
{
++i;

---winform--
This. Invoke (d, I);
----WPF---added by wonsoft.cn---
This. Dispatcher.invoke (d, I);

Thread.Sleep (100);
}
}

Update user interface
void SetValue (int value)
{
Label1. Text = value. ToString ();
}

Terminating the execution of a thread
private void Button2_Click (object sender, EventArgs e)
{
T.abort ();
}
}
}

WPF, WinForm (C #) multithreaded Programming and update interface (UI) (reproduced accumulation)

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.