|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFTransactionExample.BindingUtilities;
using EFTransactionExample.Models;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace EFTransactionExample.ViewModels
{
class MainWindowViewModel : ViewModelBase
{
// Thread safe locker
static readonly object statuslocker = new object();
static readonly object employeeInformationlocker
= new object();
// Private field
int EmployeeID = 1;
string EmployeeName = "Barack Hussein Obama II";
int EmployeeOriginalSalary = 100;
// Properties
private IEnumerable<Employee> employeeInformation;
public IEnumerable<Employee> EmployeeInformation
{
get { return employeeInformation; }
private set
{
employeeInformation = value;
NotifyPropertyChanged("EmployeeInformation");
}
}
private void UpdateEmployeeInformation()
{
lock (employeeInformationlocker)
{
ApplicationModel model = new ApplicationModel();
try
{
EmployeeInformation = model.GetEmployeeInformation();
}
catch (Exception ex)
{
string msg = "Unable to obtain employee information | "
+ ex.Message;
UpdateStatusInformation(msg);
}
}
}
private string statusInformation;
public string StatusInformation
{
get { return statusInformation; }
private set
{
statusInformation = value;
NotifyPropertyChanged("StatusInformation");
}
}
private void UpdateStatusInformation(string msg)
{
lock (statuslocker)
{
StatusInformation = msg;
}
}
private void InitiateViewModel()
{
UpdateEmployeeInformation();
}
// Commands
public RelayCommand ResetSalaryCommand { get; private set; }
private void ResetSalary()
{
ApplicationModel model = new ApplicationModel();
model.SetEmployeeSalary(EmployeeID, EmployeeName, EmployeeOriginalSalary);
EmployeeInformation = model.GetEmployeeInformation();
UpdateStatusInformation("Reset employee salary completed ..");
}
public RelayCommand RaisetSalaryNonSerializedCommand { get; private set; }
private void RaisetSalaryNonSerialized()
{
System.Threading.Thread
CPOThread = new System.Threading.Thread(
new System.Threading.ParameterizedThreadStart(WorkerNonSerialized));
CPOThread.Name = "CPO Thread";
System.Threading.Thread
CEOThread = new System.Threading.Thread(
new System.Threading.ParameterizedThreadStart(WorkerNonSerialized));
CEOThread.Name = "CEO Thread";
CPOThread.Start(new object[3] { 1, 50, 0 });
CEOThread.Start(new object[3] { 1, 50, 2500 });
}
private void WorkerNonSerialized(object parameters)
{
object[] parameterArray
= (object[])parameters;
int employeeID = (int)parameterArray[0];
int amount = (int)parameterArray[1];
int sleepBeforeStart = (int)parameterArray[2];
Thread.Sleep(sleepBeforeStart);
UpdateStatusInformation(Thread.CurrentThread.Name + " Started ..");
try
{
ApplicationModel model = new ApplicationModel();
model.RaiseEmployeeSalaryNonSerialized(employeeID, amount);
EmployeeInformation = model.GetEmployeeInformation();
}
catch (Exception ex)
{
string msg = "Unable to raise the salary for the employee | "
+ ex.Message;
UpdateStatusInformation(msg);
return;
}
UpdateEmployeeInformation();
UpdateStatusInformation(Thread.CurrentThread.Name + " Finished ..");
}
public RelayCommand RaisetSalarySerializedCommand { get; private set; }
private void RaisetSalarySerialized()
{
System.Threading.Thread
CPOThread = new System.Threading.Thread(
new System.Threading.ParameterizedThreadStart(WorkerSerialized));
CPOThread.Name = "CPO Thread";
System.Threading.Thread
CEOThread = new System.Threading.Thread(
new System.Threading.ParameterizedThreadStart(WorkerSerialized));
CEOThread.Name = "CEO Thread";
CPOThread.Start(new object[3] { 1, 50, 0 });
CEOThread.Start(new object[3] { 1, 50, 2500 });
}
private void WorkerSerialized(object parameters)
{
object[] parameterArray
= (object[])parameters;
int employeeID = (int)parameterArray[0];
int amount = (int)parameterArray[1];
int sleepBeforeStart = (int)parameterArray[2];
Thread.Sleep(sleepBeforeStart);
UpdateStatusInformation(Thread.CurrentThread.Name + " Started ..");
int NoOfTries = 0;
ApplicationModel model = new ApplicationModel();
try
{
model.RaiseEmployeeSalarySerialized(employeeID, amount);
}
catch (Exception ex)
{
NoOfTries++;
if (NoOfTries < 3)
{
string msg = Thread.CurrentThread.Name
+ " Failed to update the salary for the employ, re-trying ...";
UpdateStatusInformation(msg);
}
else
{
string msg = "Unable to raise the salary for the employee" +
", we have tried 3 times | " + ex.Message;
UpdateStatusInformation(msg);
return;
}
model.RaiseEmployeeSalarySerialized(employeeID, amount);
}
UpdateEmployeeInformation();
UpdateStatusInformation(Thread.CurrentThread.Name + " Finished ..");
}
private void WireCommands()
{
ResetSalaryCommand = new RelayCommand(ResetSalary);
ResetSalaryCommand.IsEnabled = true;
RaisetSalaryNonSerializedCommand
= new RelayCommand(RaisetSalaryNonSerialized);
RaisetSalaryNonSerializedCommand.IsEnabled = true;
RaisetSalarySerializedCommand
= new RelayCommand(RaisetSalarySerialized);
RaisetSalarySerializedCommand.IsEnabled = true;
}
public MainWindowViewModel()
{
InitiateViewModel();
WireCommands();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.