Пользовательский элемент управления. Создание клиентского приложения. Развитие серверного проекта, страница 12

SetDefaults();

SetStyle(

ControlStyles.UserPaint |

ControlStyles.AllPaintingInWmPaint |

ControlStyles.DoubleBuffer, true);

int  w = pbPos.Width / 2,

h = pbPos.Height / 2,

y = pbPos.Location.Y,

dx = 140;

org.X = ClientRectangle.Right / 2;

org.Y = y + h;

pbNeg.Location = new Point (org.X + dx - w, y);

pbPos.Location = new Point (org.X - dx - w, y);

pPos = new Point2D (-dx, 0);

pNeg = new Point2D ( dx, 0);

}

private void Draw (Graphics g)

{

g.DrawLine (isDragging ? penDrag : penFixed, x0, y0, x, y);

if (!isDragging)

{

g.FillEllipse (Brushes.Black, x0-4, y0-4, 8, 8);

g.FillEllipse (Brushes.Black, x-4, y-4, 8, 8);

}

}

private void MainForm_MouseDown (object sender, MouseEventArgs e)

{

if (!isCalculating)

{

x0 = e.X; y0 = e.Y;

pFirst = new Point2D (x0-org.X, y0-org.Y);

isDragging = true;

Text = title + String.Format (", x = {0}, y = {1}", x, y);

}

}

private void MainForm_MouseMove (object sender, MouseEventArgs e)

{

if (isDragging)

{

x = e.X; y = e.Y;

Invalidate();

Text = title + String.Format (", x = {0}, y = {1}", x, y);

}

}

private void MainForm_MouseUp (object sender, MouseEventArgs e)

{

if (isDragging)

{

x = e.X; y = e.Y;

pLast = new Point2D (x-org.X, y-org.Y);

isDragging = false;

Graphics g = Graphics.FromHwnd(Handle);

Draw(g);

CalcField field = new CalcField (this, new ProgressHandler(OnProgress));

thread = new Thread (new ThreadStart (field.Recalc));

thread.Start();

isCalculating = true;

progress.Show();

lblPercent.Show();

}

}

public void OnProgress (int id, PointF pt)

{

int pos = (int) Math.Ceiling (100 * (double)id / nodes);

progress.Value = pos;

lblPercent.Text = pos.ToString() + '%';

points[id] = pt;

if (id == nodes - 1)

{

plot.Graph.SetData (points,

"Electrostatic field along the line (v/m)", "dist (m)");

plot.Invalidate();

isCalculating = false;

progress.Hide();

lblPercent.Hide();

thread.Abort();

}

}

protected override void OnPaint(PaintEventArgs e)

{

Draw (e.Graphics);

}

// Здесь следуют Dispose, Windows Form Designer generated code и Main

}

Новый делегатный тип должен быть объявлен вне класса формы. Но подобные объявления нельзя вставлять до объявления самой формы (класса MainForm). Компилятор пропустит, но на этапе выполнения возникнет ошибка. В проектах типа Windows Application класс формы должен стоять первым (внутри общего пространства имен).

Introduction

In order to understand completely what is a thread, how and when to use it we should first see how Windows operating system works.

“Windows is a preemptive multitasking operation system.” (Aitken, Peter; 2002)

Preemptive:

When the operation system decides how much time a task will take to be executed this is a preemptive multitasking operation system. While when the task itself decides for how long to execute this is cooperative multitasking operation system. So the difference between preemptive and cooperative operation system is in the object, which decides for how long a task to be executed. (Aitken, Peter; 2002)

Multitasking:

A system with only one CPU can work over only one task at a time. The memory space, where a given application is executed is called – process. A Process is the memory set aside for an application to be executed in. Within this process the thing, which is really executed is the thread. The operation system allocates CPU’s time to the thread. So a process contains at least one thread but the process can contain many threads, which can be executed “simultaneously” by sharing the CPU. The illusion of simultaneous execution comes when the operation system divides the CPU’s time among all the running processes. (Hinton, Ben; 2002)

Thread:

So a thread is the compound unit of a process, which the operating system allocates CPU’s time to. A process always has at least one thread running, but it also can have many threads running, which share the CPU’s time. (Hinton, Ben; 2002)

Priorities