Переделать из консоли в Windows Forms - C#
Формулировка задачи:
Доброго времени!Недавно изучаю c#,помогите,пожалуйста справиться с переводом кода из консоли в Forms...Заранее спасибо.
Исходный код:
мои корявые наработки
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace SRT_laba1 { class Program { // A semaphore that simulates a limited resource pool. private static Semaphore _pool; // A item count taken by Consumer private static Random _portion; // A product max count. private static int MaxCount; // A product current count. private static int Count; // A padding interval to make the output more orderly. private static int _padding; // A locker object static object locker = new object(); public static void Main() { _pool = new Semaphore(0, 2); _portion = new Random(); MaxCount = 100; Count = 25; Console.WriteLine("Current product count =" + Count); Thread S = new Thread(Producer); S.Start(); Thread C1 = new Thread(Consumer); C1.Start(); Thread C2 = new Thread(Consumer); C2.Start(); Thread.Sleep(500); Console.WriteLine("Main thread calls Release(2)."); _pool.Release(2); Console.WriteLine("Main thread exits."); String str = System.Console.ReadLine(); } private static void Producer() { int NewCount=0; while (true) { Console.WriteLine("Thread Producer begins " + "and waits for the semaphore."); _pool.WaitOne(); Console.WriteLine("Thread Supplier enters the semaphore."); lock (locker) { if (Count < MaxCount) { NewCount = MaxCount-Count; Count += NewCount; Console.WriteLine("Producer produces "+ NewCount +" item(s)"); Console.WriteLine("Product count = " + Count); } } int padding = Interlocked.Add(ref _padding, 100); Thread.Sleep(1000 + padding); Console.WriteLine("Thread Supplier releases the semaphore."); _pool.Release(); } } private static void Consumer() { int CountToTake = 0; while (true) { Console.WriteLine("Thread Consumer begins " + "and waits for the semaphore."); _pool.WaitOne(); Console.WriteLine("Thread Consumer enters the semaphore."); lock (locker) { if ((Count - CountToTake) >0) { CountToTake = _portion.Next(1, 10); Console.WriteLine("Product to take = " + CountToTake); Count -= CountToTake; Console.WriteLine("Thread Consumer takes " + CountToTake +" item(s)."); Console.WriteLine("Product count = " + Count); } } int padding = Interlocked.Add(ref _padding, 100); Thread.Sleep(1000 + padding); Console.WriteLine("Thread Consumer releases the semaphore."); _pool.Release(); } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Лаб_2 { public partial class Form1 : Form { private static Semaphore _pool; private static bool is_produser; private static int info = 100; private static Random portion; private static int MaxCount=100; private static int Count=25; private static int _padding; static object locker = new object(); public Form1() { InitializeComponent(); } public void Info(int i, string info) { if (this.Controls[i].InvokeRequired==true) { this.Controls[i].Invoke(new Action<string>((s) => this.Controls[i].Text += s), info); } else { this.Controls[i].Text += info; } } private void Produser() { int i = 0; int NewCount=0; while (i<=10) { i += 1; _pool.WaitOne(); string write = string.Format("Thread Producer begins" + "and waits for the semaphore.\n"); Info(6, write); string name = string.Format("writer{0}Box"); int index = this.Controls.IndexOfKey(name); lock (locker) { if (Count < MaxCount) { NewCount = MaxCount - Count; Count += NewCount; write = string.Format("Producer produces " + NewCount + " item(s)"); Info(6, write); write = string.Format("Product count = " + Count); Info(6, write); } } int padding = Interlocked.Add(ref _padding, 100); Thread.Sleep(1000 + padding); write = string.Format("Thread Supplier releases the semaphore."); Info(6,write); _pool.Release(); } } private void Consumer(object N) { int i = 0; while (i <= 10) { i += 1; _pool.WaitOne(); string write = string.Format("Thread Consumer begins " + "and waits for the semaphore.",N); Info(6, write); string name = string.Format("reader{0}Box",N); int index = this.Controls.IndexOfKey(name); if (!is_produser) { write = string.Format("Reader {0} is reading.\n",N); Info(6, write); Info(index, "Read: " + info.ToString() + "\n"); } else { write = string.Format("Reader {0} cannot read cause of writer is working.\n",N); Info(6, write); } write = string.Format("Reader {0} went out.\n",N); Info(6, write); _pool.Release(); } } private void button1_Click(object sender, EventArgs e) { _pool = new Semaphore(0, 2); is_produser = false; for (int i = 1; i <= 2; i++) { Thread W = new Thread(new ParameterizedThreadStart(Produser)); W.Start(i); } for (int i = 1; i <= 3; i++) { Thread R = new Thread(new ParameterizedThreadStart(Consumer)); R.Start(i); } _pool.Release(2); } } }
Решение задачи: «Переделать из консоли в Windows Forms»
textual
Листинг программы
var mypool = new MyPool(); ... myPool.Producer();
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д