Sunday, February 21, 2010

.Net Framework Application Development Foundation(3)--Sample


1. Graphics sample
WinForm Sample: ( ColorDialog Control , fontDialog Constrol used)

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Pen aPen = new Pen(Color.Navy, 3);
private int? lastX = null;
private int? lastY = null;
// store last line drawn
private class myPath
{
public Pen newPen;
public float lastX, lastY, currentX, CurrentY;
}
List paths = new List();

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
var g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.HighQuality;

if (e.Button == MouseButtons.Left)
{
if (lastX.HasValue && lastY.HasValue)
{
g.DrawLine(aPen, (float) lastX, (float) lastY, e.X, e.Y);
paths.Add(new myPath()
{
newPen = aPen,
currentX = e.X,
CurrentY = e.Y,
lastX = (float) lastX,
lastY = (float) lastY
});
}
else
{
g.DrawRectangle(aPen, e.X, e.Y, 1, 1);
}
lastX = e.X;
lastY = e.Y;
}
else
{
lastX = null;
lastY = null;
}
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
var g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.HighQuality;
// when clear the graphics , keep the color white
this.BackColor = Color.White;
foreach (var s in paths)
{
g.DrawLine(s.newPen , s.lastX , s.lastY , s.currentX,s.CurrentY);
}
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
var g = this.CreateGraphics();
if( e.Button == System.Windows.Forms.MouseButtons.Right)
{
g.Clear(Color.White);
paths.Clear();
}
}

private void button1_Click(object sender, EventArgs e)
{
if(colorDialog1.ShowDialog(this) == DialogResult.OK)
{
aPen = new Pen(colorDialog1.Color);
button1.BackColor = colorDialog1.Color;
}
}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
aPen = new Pen(aPen.Color, (float)numericUpDown1.Value);
}

private void button2_Click(object sender, EventArgs e)
{
var g = this.CreateGraphics();

Font font;

if( fontDialog1.ShowDialog() == DialogResult.OK)
{
font = fontDialog1.Font;
}
else
{
font = new Font("Arial" , 60 , FontStyle.Bold | FontStyle.Underline);
}

g.DrawString(textBox1.Text ,font ,Brushes.Navy , 100f , 100f);
}

}

2. Multiple threading programming
class Program
{
static void Main(string[] args)
{
//utilize the automatical threading method
ThreadPool.QueueUserWorkItem(DemoThreading.Proc);
ThreadPool.QueueUserWorkItem(DemoThreading.ProcWithArgument, 5);

Thread.Sleep(2000);// have backgroud thread finished first
int anum, cnum;
ThreadPool.GetMaxThreads(out anum , out cnum);
;
Console.WriteLine("The Main thread exist. The threadPool has {0} threads availabe. {1} completionPortThreads.",
anum , cnum);
}
}

public class DemoThreading
{
//show how many days from 2000 to 2010
public static void Proc(object input) //input is uesless only to meet the satisfaction of signature
{
DateTime oldTime = new DateTime(2000, 1 , 1);
DateTime newTime = new DateTime(2010 , 1 , 1);
TimeSpan ts = newTime - oldTime;

Console.WriteLine("There are total {0} days from 2000 to 2010" , ts.TotalDays);

}
//return the factorial of a
public static void ProcWithArgument( object input )
{
int a = Convert.ToInt32(input);
int fab = 1;

if (a == 1)
fab = 1;
else
{
checked
{
int i;
for (i = 1; i <= a; i++) { fab = fab * i; } } } Console.WriteLine("The factorial of number {0} is {1} .", a , fab); } }

2.2 managing threads
class Program
{
static void Main(string[] args)
{
//managing threads methods
var thread = new Thread(new ParameterizedThreadStart(DemoThreading.Proc));
var another = new Thread(new ParameterizedThreadStart(DemoThreading.ProcWithArgument));

another.Priority = ThreadPriority.Highest;
thread.Priority = ThreadPriority.BelowNormal;
thread.Start();
another.Start(5);

Thread.Sleep(2000);
Console.WriteLine("The main thread is ending.");
}
}

public class DemoThreading
{
//show how many days from 2000 to 2010
public static void Proc(object input) //input is uesless only to meet the satisfaction of signature
{
DateTime oldTime = new DateTime(2000, 1 , 1);
DateTime newTime = new DateTime(2010 , 1 , 1);
TimeSpan ts = newTime - oldTime;

Console.WriteLine("There are total {0} days from 2000 to 2010" , ts.TotalDays);

}
//return the factorial of a
public static void ProcWithArgument( object input )
{
int a = Convert.ToInt32(input);
int fab = 1;

if (a == 1)
fab = 1;
else
{
checked
{
int i;
for (i = 1; i <= a; i++)
{
fab = fab * i;
}
}
}

Console.WriteLine("The factorial of number {0} is {1} .", a , fab);
}

private string _file = "Hello";//private field
private ReaderWriterLock rwl = new ReaderWriterLock();//syn lock

public void WriteFile()
{
lock (_file)
{
_file += " It's good.";
}

}

public void ReadFile()
{
rwl.AcquireReaderLock(10000);
for( int i=1; i<=3 ; i++)
{
Console.WriteLine(_file);
Thread.Sleep(1000);
}
rwl.ReleaseReaderLock();
}
}

No comments:

Post a Comment