implemented db, cache, and tasks

This commit is contained in:
Hendrik Schutter 2019-05-04 11:07:41 +02:00
parent d9424be20e
commit 46383e7d11
5 changed files with 514 additions and 2 deletions

View File

@ -28,13 +28,112 @@
/// </summary>
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;
}
}

View File

@ -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<int> colorCache = new List<int>();
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);
}
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -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());
}
}
}

View File

@ -54,6 +54,9 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>