int a=0;
void Foo()
{
for ( int i=0;i<100000;i++)
{
a++;
}
MessageBox.Show(a.ToString());
}
* This source code was highlighted with Source Code Highlighter .
using System.Threading;
…
Thread thr= new Thread(Foo);
thr.Start();
…
* This source code was highlighted with Source Code Highlighter .
int a=0;
void Foo()
{
for ( int i=0;i<100000;i++)
{
a++;
label1.Text=a.ToString();
}
}
* This source code was highlighted with Source Code Highlighter .
SmartDeviceProject1.exe
NotSupportedException
Control.Invoke must be used for a separate thread.
...
at System.Windows.Forms.Control.get_Text ()
at System.Windows.Forms.Control.set_Text (String value)
at SmartDeviceProject.Form1.Foo ()
ThreadPool.QueueUserWorkItem( delegate { Foo(); });
* This source code was highlighted with Source Code Highlighter .
void ChangeLabel1Text( string text)
{
label1.Text = text;
}
* This source code was highlighted with Source Code Highlighter .
delegate void ChangeLabel1TextDelegate( string text);
ChangeLabel1TextDelegate cltd;
cltd = new ChangeLabel1TextDelegate (ChangeLabel1Text);
* This source code was highlighted with Source Code Highlighter .
label1. Text = a.ToString ();
* This source code was highlighted with Source Code Highlighter .
label1.Invoke(cltd, a.ToString());
* This source code was highlighted with Source Code Highlighter .
ThreadPool .QueueUserWorkItem( delegate { Foo(); });
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/65111/
All Articles