- Код: Выделить всё
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:winexe /out:FooBar.exe *.cs
- Код: Выделить всё
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace FooBarThreads
{
public class FooBar : Form
{
private IContainer components = null;
private TableLayoutPanel tableLayoutPanel1;
private ListBox listBoxThreads;
private Panel panel1;
private Button buttonStart;
private Button buttonExit;
private List<int> idl = new List<int>(10);
private List<Thread> ts = new List<Thread>(11);
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.tableLayoutPanel1 = new TableLayoutPanel();
this.listBoxThreads = new ListBox();
this.panel1 = new Panel();
this.buttonExit = new Button();
this.buttonStart = new Button();
this.tableLayoutPanel1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
this.tableLayoutPanel1.Controls.Add(this.listBoxThreads, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 1);
this.tableLayoutPanel1.Dock = DockStyle.Fill;
this.tableLayoutPanel1.Location = new Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 40F));
this.tableLayoutPanel1.Size = new Size(184, 242);
this.tableLayoutPanel1.TabIndex = 0;
this.listBoxThreads.Dock = DockStyle.Fill;
this.listBoxThreads.FormattingEnabled = true;
this.listBoxThreads.IntegralHeight = false;
this.listBoxThreads.Location = new Point(3, 3);
this.listBoxThreads.Name = "listBoxThreads";
this.listBoxThreads.Size = new Size(178, 196);
this.listBoxThreads.TabIndex = 0;
this.panel1.Controls.Add(this.buttonStart);
this.panel1.Controls.Add(this.buttonExit);
this.panel1.Dock = DockStyle.Fill;
this.panel1.Location = new Point(3, 205);
this.panel1.Name = "panel1";
this.panel1.Size = new Size(178, 34);
this.panel1.TabIndex = 1;
this.buttonExit.Location = new Point(94, 3);
this.buttonExit.Name = "buttonExit";
this.buttonExit.Size = new Size(75, 28);
this.buttonExit.TabIndex = 2;
this.buttonExit.Text = "Exit";
this.buttonExit.UseVisualStyleBackColor = true;
this.buttonExit.Click += new EventHandler(this.buttonExit_Click);
this.buttonStart.Location = new Point(9, 3);
this.buttonStart.Name = "buttonStart";
this.buttonStart.Size = new Size(75, 28);
this.buttonStart.TabIndex = 3;
this.buttonStart.Text = "Start";
this.buttonStart.UseVisualStyleBackColor = true;
this.buttonStart.Click += new EventHandler(this.buttonStart_Click);
this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(184, 242);
this.Controls.Add(this.tableLayoutPanel1);
this.MaximizeBox = false;
this.MaximumSize = new Size(200, 280);
this.MinimumSize = new Size(200, 280);
this.Name = "Form1";
this.StartPosition = FormStartPosition.CenterScreen;
this.Text = "FooBar";
this.Load += new System.EventHandler(this.Form1_Load);
this.tableLayoutPanel1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
public FooBar()
{
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
this.idl.Add(1);
this.listBoxThreads.Items.Add("1");
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
Thread tDysplayIndex = new Thread(DisplayIndex);
tDysplayIndex.IsBackground = true;
tDysplayIndex.Start(null);
this.ts.Add(tDysplayIndex);
for (int i = 0; i < 10; i++)
{
Thread tChangeIndex = new Thread(ChangeIndex);
tChangeIndex.IsBackground = true;
tChangeIndex.Start(null);
this.ts.Add(tChangeIndex);
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
foreach (Thread t in this.ts) { t.Abort(); }
this.Dispose();
}
private void ChangeIndex(object state)
{
while (true)
{
lock (this.idl)
{
Random random = new Random();
this.idl[random.Next(0, 10)]++;
}
}
}
private void DisplayIndex(object state)
{
while (true)
{
lock (this.idl)
{
for (int i = 0; i < this.idl.Count; i++)
{
this.listBoxThreads.BeginUpdate();
this.listBoxThreads.Items[i] = this.idl[i].ToString();
this.listBoxThreads.EndUpdate();
}
}
}
}
}
public static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FooBar());
}
}
}
Подскажите - как избавиться от ряби заранее благодарен.