Programming Threads under the Compact Framework
Probably every person who programs under the .NET Framework or Compact Framework, at least once in his life, came across threads (Thread). And often there was the problem of launching a function with a call to a static method, in particular this applies to those methods where form controls change. In this article I will tell you how to solve this problem.
Consider the following example. Is there any Foo () function:
The easiest way to execute this function in a stream:
Now change our function like this:
The result will be an error:
This means that the interaction with the control must be in a separate thread.
Another way is to use the thread pool and delegate:
Under Windows, everything is fine, but under Windows Mobile the same error pops up.
To solve this problem, you need to use the Invoke method for each control.
Create a function to change the text Label1:
Add a delegate for this function:
In the function Foo () we change the line
on the:
Run the thread to execute:
All. Now when you start the program on a PDA or smartphone, you will see numbers from 1 to 100000.
Original: forum.wce.by/viewtopic.php?f=15&t=14221&start=0
Consider the following example. Is there any Foo () function:
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.
The easiest way to execute this function in a stream:
using System.Threading;
…
Thread thr=new Thread(Foo);
thr.Start();
…
* This source code was highlighted with Source Code Highlighter.
Now change our function like this:
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.
The result will be an error:
SmartDeviceProject1.exe
NotSupportedException
Control.Invoke must be used to interact with controls created on a separate thread.
...
at System.Windows.Forms.Control.get_Text ()
at System.Windows.Forms.Control.set_Text (String value)
at SmartDeviceProject.Form1.Foo ()
This means that the interaction with the control must be in a separate thread.
Another way is to use the thread pool and delegate:
ThreadPool.QueueUserWorkItem( delegate { Foo(); });
* This source code was highlighted with Source Code Highlighter.
Under Windows, everything is fine, but under Windows Mobile the same error pops up.
To solve this problem, you need to use the Invoke method for each control.
Create a function to change the text Label1:
void ChangeLabel1Text(string text)
{
label1.Text = text;
}
* This source code was highlighted with Source Code Highlighter.
Add a delegate for this function:
delegate void ChangeLabel1TextDelegate(string text);
ChangeLabel1TextDelegate cltd;
cltd = new ChangeLabel1TextDelegate (ChangeLabel1Text);
* This source code was highlighted with Source Code Highlighter.
In the function Foo () we change the line
label1. Text = a.ToString ();
* This source code was highlighted with Source Code Highlighter.
on the:
label1.Invoke(cltd, a.ToString());
* This source code was highlighted with Source Code Highlighter.
Run the thread to execute:
ThreadPool .QueueUserWorkItem( delegate { Foo(); });
* This source code was highlighted with Source Code Highlighter.
All. Now when you start the program on a PDA or smartphone, you will see numbers from 1 to 100000.
Original: forum.wce.by/viewtopic.php?f=15&t=14221&start=0