Threading rockt

This commit is contained in:
BlubbFish 2011-09-21 22:01:22 +00:00
parent 42e167bdef
commit 5ebf0ad6a6
7 changed files with 91 additions and 7 deletions

View File

@ -15,7 +15,7 @@ namespace Matomat
public Automat() {
lcd = Factory.getLCDDisplay();
inp = new Input();
inp = Factory.getInput();
}
internal void doJob(InputData input)
{
@ -54,7 +54,7 @@ namespace Matomat
private void showUserInfo(User user)
{
throw new NotImplementedException();
lcd.print("User x",5);
}
private User findUser(int userId)

View File

@ -8,6 +8,7 @@ namespace Matomat
class Factory
{
static LCDDisplay lcd_i = null;
static Input inp_i = null;
internal static LCDDisplay getLCDDisplay()
{
if (lcd_i == null)
@ -20,5 +21,18 @@ namespace Matomat
LCDDisplay lcd = LCDDisplay.getInstance("lpd1");
return lcd;
}
internal static Input getInput()
{
if (inp_i == null)
inp_i = Factory._createInput();
return inp_i;
}
private static Input _createInput()
{
Input inp = Input.getInstance();
return inp;
}
}
}

View File

@ -2,11 +2,17 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Matomat
{
class InputData
{
public InputData()
{
this.id = 0;
this.type = types.None;
}
public enum types
{
Card,
@ -18,14 +24,75 @@ namespace Matomat
}
class Input
{
private class instance
{
public Input input;
public double signature;
public instance(Input input, double signature)
{
this.input = input;
this.signature = signature;
}
}
static List<instance> instances;
private InputData data;
private void doWorkCard()
{
while (true)
{
Thread.Sleep(1);
data.type = InputData.types.Card;
data.id = 3;
}
}
private void doWorkCode()
{
while (true)
{
Thread.Sleep(1);
}
}
internal InputData getAnyInput(int timeout)
{
throw new NotImplementedException();
if (timeout == 0)
timeout = Int32.MaxValue;
data = new InputData();
Thread wtCard = new Thread(this.doWorkCard);
Thread wtCode = new Thread(this.doWorkCode);
wtCard.Start();
wtCode.Start();
while (!wtCard.IsAlive) ;
while (!wtCode.IsAlive) ;
DateTime time = DateTime.Now.AddSeconds(timeout);
while (time.Ticks < DateTime.Now.Ticks || data.type == InputData.types.None)
{
Thread.Sleep(1);
}
wtCard.Abort();
wtCode.Abort();
return data;
}
internal InputData getProdInput(int timeout)
{
throw new NotImplementedException();
}
internal static Input getInstance()
{
if (instances == null)
instances = new List<instance>();
double signature = 1.0;
foreach (instance i in instances)
{
if (i.signature == signature)
return i.input;
}
instance instance = new instance(new Input(), signature);
instances.Add(instance);
return instance.input;
}
}
}

View File

@ -47,7 +47,9 @@
<Compile Include="BarcodeReader.cs" />
<Compile Include="Factory.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Input.cs" />
<Compile Include="LCDDisplay.cs" />
<Compile Include="Prod.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RFIDReader.cs" />

View File

@ -17,7 +17,7 @@ namespace Matomat
while (!workerThread.IsAlive) ;
while (workerThread.ThreadState == ThreadState.Running)
while (workerThread.IsAlive)
{
Thread.Sleep(1);
}

View File

@ -20,7 +20,7 @@ namespace Matomat
private void load()
{
if (true)
this.found = false;
this.found = true;
}
internal bool vaild()

View File

@ -19,17 +19,18 @@ namespace Matomat
barcode = new BarcodeReader();
automat = new Automat();
lcd = Factory.getLCDDisplay();
inp = new Input();
inp = Factory.getInput();
}
public void DoWork()
{
while (!_shouldStop)
{
lcd.print("Matomat, wikommen: Beste Säufer: x x x");
InputData input = inp.getAnyInput(0);
/*int userId = rfid.getCardID();
* lcd.print("Bitte Sudierendenausweis über den RFID Leser halten.");
* ;
long codeId = barcode.getCodeID();*/
automat.doJob(input);
}