From 46383e7d11481d05b77bc82085f47672b4849e31 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 4 May 2019 11:07:41 +0200 Subject: [PATCH] implemented db, cache, and tasks --- .../WindowsFormsAppCache/Form1.Designer.cs | 101 ++++++- .../WindowsFormsAppCache/Form1.cs | 286 +++++++++++++++++- .../WindowsFormsAppCache/Form1.resx | 120 ++++++++ .../WindowsFormsAppCache/Program.cs | 6 + .../WindowsFormsAppCache.csproj | 3 + 5 files changed, 514 insertions(+), 2 deletions(-) create mode 100644 WindowsFormsAppCache/WindowsFormsAppCache/Form1.resx diff --git a/WindowsFormsAppCache/WindowsFormsAppCache/Form1.Designer.cs b/WindowsFormsAppCache/WindowsFormsAppCache/Form1.Designer.cs index a20dbc9..54f9c60 100644 --- a/WindowsFormsAppCache/WindowsFormsAppCache/Form1.Designer.cs +++ b/WindowsFormsAppCache/WindowsFormsAppCache/Form1.Designer.cs @@ -28,13 +28,112 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.pictureBox2 = new System.Windows.Forms.PictureBox(); + this.pictureBox3 = new System.Windows.Forms.PictureBox(); + this.pictureBox4 = new System.Windows.Forms.PictureBox(); + this.button1 = new System.Windows.Forms.Button(); + this.button2 = new System.Windows.Forms.Button(); + this.button3 = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit(); + this.SuspendLayout(); + // + // pictureBox1 + // + this.pictureBox1.Location = new System.Drawing.Point(67, 30); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(221, 181); + this.pictureBox1.TabIndex = 0; + this.pictureBox1.TabStop = false; + // + // pictureBox2 + // + this.pictureBox2.Location = new System.Drawing.Point(524, 30); + this.pictureBox2.Name = "pictureBox2"; + this.pictureBox2.Size = new System.Drawing.Size(221, 181); + this.pictureBox2.TabIndex = 1; + this.pictureBox2.TabStop = false; + // + // pictureBox3 + // + this.pictureBox3.Location = new System.Drawing.Point(67, 257); + this.pictureBox3.Name = "pictureBox3"; + this.pictureBox3.Size = new System.Drawing.Size(221, 181); + this.pictureBox3.TabIndex = 2; + this.pictureBox3.TabStop = false; + // + // pictureBox4 + // + this.pictureBox4.Location = new System.Drawing.Point(524, 257); + this.pictureBox4.Name = "pictureBox4"; + this.pictureBox4.Size = new System.Drawing.Size(221, 181); + this.pictureBox4.TabIndex = 3; + this.pictureBox4.TabStop = false; + // + // button1 + // + this.button1.Location = new System.Drawing.Point(370, 188); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 4; + this.button1.Text = " Start (Lock)"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.Button1_Click); + // + // button2 + // + this.button2.Location = new System.Drawing.Point(354, 230); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(113, 23); + this.button2.TabIndex = 5; + this.button2.Text = "Start (RWLS)"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.Button2_Click); + // + // button3 + // + this.button3.Location = new System.Drawing.Point(354, 273); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(109, 23); + this.button3.TabIndex = 6; + this.button3.Text = "Start (NoCache)"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.Button3_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.button3); + this.Controls.Add(this.button2); + this.Controls.Add(this.button1); + this.Controls.Add(this.pictureBox4); + this.Controls.Add(this.pictureBox3); + this.Controls.Add(this.pictureBox2); + this.Controls.Add(this.pictureBox1); + this.Name = "Form1"; this.Text = "Form1"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit(); + this.ResumeLayout(false); + } #endregion + + private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.PictureBox pictureBox2; + private System.Windows.Forms.PictureBox pictureBox3; + private System.Windows.Forms.PictureBox pictureBox4; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.Button button3; } } diff --git a/WindowsFormsAppCache/WindowsFormsAppCache/Form1.cs b/WindowsFormsAppCache/WindowsFormsAppCache/Form1.cs index 6277297..0840abc 100644 --- a/WindowsFormsAppCache/WindowsFormsAppCache/Form1.cs +++ b/WindowsFormsAppCache/WindowsFormsAppCache/Form1.cs @@ -5,6 +5,7 @@ using System.Data; using System.Drawing; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -16,5 +17,288 @@ namespace WindowsFormsAppCache { InitializeComponent(); } + + private void Button1_Click(object sender, EventArgs e) + { + Object lockobject = new Object(); + + var t1 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + lock (lockobject) + { + pb2.Image = Cache.getImageFromCache(number); + } + } + }, pictureBox1); + ; + + + var t2 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + lock (lockobject) + { + pb2.Image = Cache.getImageFromCache(number); + } + } + }, pictureBox2); + ; + + var t3 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + lock (lockobject) + { + pb2.Image = Cache.getImageFromCache(number); + } + } + }, pictureBox3); + ; + + var t4 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + lock (lockobject) + { + pb2.Image = Cache.getImageFromCache(number); + } + } + }, pictureBox4); + ; + + + t1.Start(); + t2.Start(); + t3.Start(); + t4.Start(); + + + Task.WaitAll(t1, t2, t3, t4); + } + + private void Button2_Click(object sender, EventArgs e) + { + ReaderWriterLockSlim rwls = new ReaderWriterLockSlim(); + + var t1 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = Cache.getImageFromCache(number); + rwls.ExitReadLock(); + } + }, pictureBox1); + ; + + + var t2 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = Cache.getImageFromCache(number); + rwls.ExitReadLock(); + } + }, pictureBox2); + ; + + var t3 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = Cache.getImageFromCache(number); + rwls.ExitReadLock(); + } + }, pictureBox3); + ; + + var t4 = new Task((pb) => + { + PictureBox pb2 = (PictureBox) pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = Cache.getImageFromCache(number); + rwls.ExitReadLock(); + } + }, pictureBox4); + ; + + + t1.Start(); + t2.Start(); + t3.Start(); + t4.Start(); + + + Task.WaitAll(t1, t2, t3, t4); + } + + private void Button3_Click(object sender, EventArgs e) + { + + + ReaderWriterLockSlim rwls = new ReaderWriterLockSlim(); + + var t1 = new Task((pb) => + { + PictureBox pb2 = (PictureBox)pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = DB.getImageFromDB(number); + rwls.ExitReadLock(); + } + }, pictureBox1); + ; + + + var t2 = new Task((pb) => + { + PictureBox pb2 = (PictureBox)pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = DB.getImageFromDB(number); + rwls.ExitReadLock(); + } + }, pictureBox2); + ; + + var t3 = new Task((pb) => + { + PictureBox pb2 = (PictureBox)pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = DB.getImageFromDB(number); + rwls.ExitReadLock(); + } + }, pictureBox3); + ; + + var t4 = new Task((pb) => + { + PictureBox pb2 = (PictureBox)pb; + while (true) + { + Random ran = new Random(); + int number = ran.Next(0, 6); + rwls.EnterReadLock(); + pb2.Image = DB.getImageFromDB(number); + rwls.ExitReadLock(); + } + }, pictureBox4); + ; + + + t1.Start(); + t2.Start(); + t3.Start(); + t4.Start(); + + + Task.WaitAll(t1, t2, t3, t4); + + + + } } -} + + + public static class DB + { + public static Bitmap getImageFromDB(int colorNumber) + { + Thread.Sleep(1000); + + Color[] color = new Color[7]; + + color[0] = Color.White; + color[1] = Color.Blue; + color[2] = Color.Green; + color[3] = Color.Yellow; + color[4] = Color.Black; + color[5] = Color.Chocolate; + color[6] = Color.Brown; + + + + + var b = new Bitmap(1, 1); + b.SetPixel(0, 0, color[colorNumber]); + var result = new Bitmap(b, 128, 128); + return result; + } + } + + + public static class Cache + { + private static List colorCache = new List(); + + public static Bitmap getImageFromCache(int colorNumber) + { + Thread.Sleep(100); + if (colorCache.Contains(colorNumber)) + { + Color[] color = new Color[7]; + + color[0] = Color.White; + color[1] = Color.Blue; + color[2] = Color.Green; + color[3] = Color.Yellow; + color[4] = Color.Black; + color[5] = Color.Chocolate; + color[6] = Color.Brown; + + + var b = new Bitmap(1, 1); + b.SetPixel(0, 0, color[colorNumber]); + var result = new Bitmap(b, 128, 128); + return result; + } + else + { + colorCache.Add(colorNumber); + return DB.getImageFromDB(colorNumber); + } + } + } +} \ No newline at end of file diff --git a/WindowsFormsAppCache/WindowsFormsAppCache/Form1.resx b/WindowsFormsAppCache/WindowsFormsAppCache/Form1.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/WindowsFormsAppCache/WindowsFormsAppCache/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/WindowsFormsAppCache/WindowsFormsAppCache/Program.cs b/WindowsFormsAppCache/WindowsFormsAppCache/Program.cs index 530dff2..0d22dbe 100644 --- a/WindowsFormsAppCache/WindowsFormsAppCache/Program.cs +++ b/WindowsFormsAppCache/WindowsFormsAppCache/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.AccessControl; using System.Threading.Tasks; using System.Windows.Forms; @@ -18,5 +19,10 @@ namespace WindowsFormsAppCache Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } + + + + + } } diff --git a/WindowsFormsAppCache/WindowsFormsAppCache/WindowsFormsAppCache.csproj b/WindowsFormsAppCache/WindowsFormsAppCache/WindowsFormsAppCache.csproj index d95e0f2..40364b5 100644 --- a/WindowsFormsAppCache/WindowsFormsAppCache/WindowsFormsAppCache.csproj +++ b/WindowsFormsAppCache/WindowsFormsAppCache/WindowsFormsAppCache.csproj @@ -54,6 +54,9 @@ + + Form1.cs + ResXFileCodeGenerator Resources.Designer.cs