midrange.com code scratchpad |
Name:
Threading
|
Scriptlanguage:
C#
|
Tabwidth:
2
|
Date:
10/31/2008 02:13:41 pm
|
IP:
Logged
|
|
Description:
The user was to lazy to give a description
|
Code:
- using System;
- using System.Windows.Forms;
- using System.Threading;
-
- namespace WinForm1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- ParameterizedThreadStart pts = new ParameterizedThreadStart(Background.DoLongProcess);
- Thread getDataThread = new Thread(pts);
- getDataThread.Name = "DataQGet";
-
-
- getDataThread.Priority = ThreadPriority.BelowNormal;
-
-
- getDataThread.IsBackground = true;
-
-
- getDataThread.SetApartmentState(ApartmentState.STA);
-
-
- getDataThread.Start(this);
- }
-
- public void GotData(string TheData)
- {
-
-
- listBox1.Items.Add(TheData);
- }
- }
-
- class Background
- {
- static public void DoLongProcess(object StateInfo)
- {
- Form1 theForm = (Form1)StateInfo;
-
-
- while (true)
- {
-
- System.Threading.Thread.Sleep(5000);
- string theData = DateTime.Now.ToString();
-
-
-
- if (theForm.InvokeRequired)
- theForm.Invoke((MethodInvoker)delegate { theForm.GotData("Invoked: " + theData); });
- else
- theForm.GotData(theData);
- }
- }
- }
- }
-
|
|
|