Sunday, February 8, 2015
Thread Using System Threading
using System.Threading.Tasks;
using System.Threading; //added
using System.Windows.Forms;
namespace Threads_Using_System_Threading
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
//define 2 methods
private void DisplayOddRandomNumbers()
{
//Display 1,000 odd random
int counter = 0;
do
{
int num = rand.Next();
if (num % 2 != 0)
{
//richTextBox1.AppendText(counter + " " + num + "\n");
SetText(counter + " " + num, richTextBox1);
//scroll down automaically
//rtb.ScrollToCaret();
counter++;
}
} while (counter < 1000);
}
private void DisplayEvenRandomNumbers()
{
//Display 1,000 odd random
int counter = 0;
do
{
int num = rand.Next();
if (num % 2 == 0)
{
// richTextBox2.AppendText(counter + " " + num + "\n");
SetText(counter + " " + num, richTextBox2);
//scroll down automaically
//rtb.ScrollToCaret();
counter++;
}
} while (counter < 1000);
}
//Define a delegate with same signature as the cross-threading
//handling method (the SetText method)
private delegate void RichTextBoxHandler(string s, RichTextBox rtb);
//method to allow other threads to access the UI RichTextBox
//this method should accept the string that you want to display in the UI
private void SetText(string s, RichTextBox rtb)
{
if (rtb.InvokeRequired)
{
//create a delegate object
RichTextBoxHandler handler = SetText;
Invoke(handler, s, rtb);
}
else
{
rtb.AppendText(s + "\n");
//scroll down automaically
rtb.ScrollToCaret();
}
}
private void btnStartTwoTasks_Click(object sender, EventArgs e)
{
//1.
//ThreadStart start = DisplayOddRandomNumbers;
//Thread oddThread = new Thread(start);
//or
//2.
Thread oddThread = new Thread(DisplayOddRandomNumbers);
//Create the even thread
Thread evenThread = new Thread(DisplayEvenRandomNumbers);
//start the thread
oddThread.Start();
evenThread.Start();
}
private void btnNumberOfThreadPool_Click(object sender, EventArgs e)
{
int workerThreads, completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
richTextBox3.Text = "workerThreads: " + workerThreads + "\n" +
"portThreads: " + completionPortThreads;
}
private void btnGraphicsThreadPool_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(DisplayDots));
//Gets a thread from the threadpool and start it
}
private void DisplayDots(object obj)
{
Graphics g = panel1.CreateGraphics();
Brush brush = new SolidBrush(Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)));
int diameter = rand.Next(5, 10);
for (int i = 1; i < 100; i++)
{
Point point = new Point(rand.Next(panel1.Width), rand.Next(panel1.Height));
g.FillEllipse(brush, point.X, point.Y, diameter, diameter);
Thread.Sleep(100);
}
}
private void btnStartTwoTaskWithThreadpool_Click(object sender, EventArgs e)
{
}
}
}
//Readmore...Lists
//https://msdn.microsoft.com/en-us/library/xx3ezzs2(v=vs.110).aspx
Labels:
C#Advance
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment