Multithreading 4-Tier Mixed Producer Consumer Pattern - Threads Not Alternating Properly - c#

Here is the problem I am trying to model: I have an incoming call center (no outbound calls). There are 3 employee levels - Freshers, Technical Lead, Project Manager. In our call center there is only one TL, one PM and multiple Freshers. Calls that the Freshers cannot handle are escalated to the TL, and calls that the TL cannot handle are escalated to the PM.
I need to take an OO C# approach.
Here is my current attempt: I used C# ConcurrentQueue since I thought it would take care of the 'locks'. I want FIFO for call center. I made a queue for each level.
I have a producer (callers) adding calls to the first queue. Mixed prod/consumer freshers check the call (take or escalate to the next queue). Mixed prod/consumer tl, then a pure consumer project manager.
The output is not what I expect. Only the first fresher runs and the project manager does not run at all. I expect a lot more alternation as the calls are added to the queue.
My code is below. Does anyone have a better approach to this problem or is there something wrong with my code?
The CallCenter Class is where most of the action takes place.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
namespace CallCenterThreaded
{
/// <summary>
///
/// MIXED CONSUMER/PRODUCER TYPE
/// </summary>
class Fresher
{
// add a class variable for the number of Employee objects instantiated
private static int fresherNum = 0;
private int fresherId;
private ConcurrentQueue<Call> incommingCalls;
private ConcurrentQueue<Call> outgoingCalls;
public Fresher(ConcurrentQueue<Call> calls, ConcurrentQueue<Call> outcalls)
{
fresherId = ++fresherNum;
incommingCalls = calls;
outgoingCalls = outcalls;
//start the producer thread
Thread thread = new Thread(new ThreadStart(HandleCalls));
thread.Start();
}
public int ID
{
get { return fresherId; }
}
/// <summary>
///
/// </summary>
public void HandleCalls()
{
while (incommingCalls.Count > 0)
{
Call call;
incommingCalls.TryDequeue(out call);
if (call != null)
{
if (call.EscalationLevel == 0)
{
TakeCalls(call);
}
else
{
TransferCalls(call);
}
}
}
}
/// <summary>
/// Transfer to the TL
/// </summary>
public void TransferCalls(Call call)
{
outgoingCalls.Enqueue(call);
Console.WriteLine(".......Fresher {0} escalated call {1}", ID, call.CallID);
}
/// <summary>
/// Consume the calls
/// </summary>
public void TakeCalls(Call call)
{
Console.WriteLine("Fresher {0} handled call {1}", ID, call.CallID);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
namespace CallCenterThreaded
{
/// <summary>
/// MIXED CONSUMER/PRODUCER TYPE
/// </summary>
class TechnicalLead
{
// add a class variable for the number of Employee objects instantiated
private static int tlNum = 0;
private int tlId;
private ConcurrentQueue<Call> incommingCalls;
private ConcurrentQueue<Call> outgoingCalls;
public TechnicalLead(ConcurrentQueue<Call> calls, ConcurrentQueue<Call> outcalls)
{
tlId = ++tlNum;
incommingCalls = calls;
outgoingCalls = outcalls;
//start the producer thread
Thread thread = new Thread(new ThreadStart(HandleCalls));
thread.Start();
}
public int ID
{
get { return tlId; }
}
/// <summary>
///
/// </summary>
public void HandleCalls()
{
while (incommingCalls.Count > 0)
{
Call call;
incommingCalls.TryDequeue(out call);
if (call != null)
{
if (call.EscalationLevel == 1)
{
TakeCalls(call);
}
else
{
TransferCalls(call);
}
}
}
}
/// <summary>
/// Transfer to the PM
/// </summary>
public void TransferCalls(Call call)
{
//Console.WriteLine(".......Technical Lead {0} escalated call {1}", ID, call.CallID);
outgoingCalls.Enqueue(call);
}
/// <summary>
/// Consume the calls
/// </summary>
public void TakeCalls(Call call)
{
Console.WriteLine("Technical Lead {0} handled call {1}", ID, call.CallID);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CallCenterThreaded
{
class Call
{
private static int numberCalls = 0;
private int callno;
private int esclataion;
public Call()
{
callno = ++numberCalls;
esclataion = 0;
if(callno % 3 == 0)
{
esclataion = 1;
}
if(callno % 5 == 0)
{
esclataion = 2;
}
}
public int CallID { get { return callno; } }
public int EscalationLevel { get { return esclataion; } }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
namespace CallCenterThreaded
{
/// <summary>
///
/// </summary>
class CallCenter
{
private ConcurrentQueue<Call> fresherCalls;
private ConcurrentQueue<Call> tlCalls;
private ConcurrentQueue<Call> pmCalls;
private List<Caller> myCallers;
private List<Fresher> myFreshers;
private TechnicalLead tl;
private ProjectManager pm;
public CallCenter()
{
//initial incomming calls to the fresher queue
fresherCalls = new ConcurrentQueue<Call>();
tlCalls = new ConcurrentQueue<Call>();
pmCalls = new ConcurrentQueue<Call>();
myFreshers = new List<Fresher>();
myCallers = new List<Caller>();
generate_callers();
generate_freshers();
tl = new TechnicalLead(tlCalls, pmCalls);
pm = new ProjectManager(pmCalls);
}
/// <summary>
///
/// </summary>
private void generate_freshers()
{
for (int i = 0; i < 5; i++ )
{
myFreshers.Add(new Fresher(fresherCalls, tlCalls));
}
}
/// <summary>
///
/// </summary>
private void generate_callers()
{
for (int i = 0; i < 5; i++ )
{
myCallers.Add(new Caller(fresherCalls));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
namespace CallCenterThreaded
{
/// <summary>
/// PURE CONSUMER
/// </summary>
class ProjectManager
{
// add a class variable for the number of Employee objects instantiated
private static int pmNum = 0;
private int pmId;
private ConcurrentQueue<Call> incommingCalls;
public ProjectManager(ConcurrentQueue<Call> calls)
{
pmId = ++pmNum;
incommingCalls = calls;
//start the producer thread
Thread thread = new Thread(new ThreadStart(HandleCalls));
thread.Start();
}
public int ID
{
get { return pmId; }
}
/// <summary>
///
/// </summary>
public void HandleCalls()
{
while (incommingCalls.Count > 0)
{
Call call;
incommingCalls.TryDequeue(out call);
if (call != null && call.EscalationLevel == 2)
{
TakeCalls(call);
}
}
}
/// <summary>
/// Consume the calls
/// </summary>
public void TakeCalls(Call call)
{
Console.WriteLine("Project Manager {0} handled call {1}", ID, call.CallID);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
namespace CallCenterThreaded
{
class MainClass
{
static void Main(string[] args)
{
CallCenter myCenter = new CallCenter();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;
namespace CallCenterThreaded
{
/// <summary>
/// This is a producer. It produces calls and adds them to the queue.
/// PURE PRODUCER TYPE
/// </summary>
class Caller
{
private ConcurrentQueue<Call> incommingCalls;
public Caller(ConcurrentQueue<Call> calls)
{
incommingCalls = calls;
//start the producer thread
Thread thread = new Thread(new ThreadStart(placeCalls));
thread.Start();
}
public void placeCalls()
{
//place 10 calls
for (int callno = 0; callno < 4; callno++)
{
//Console.WriteLine("Call {0} was added to the queue", callno);
incommingCalls.Enqueue(new Call());
}
}
}
}

The problem is in the HandleCalls() methods:
public void HandleCalls()
{
while (incommingCalls.Count > 0)
{
…
}
}
What this code does is to check incomingCalls and if there are none, it immediately exits the thread. It's as if the worker came to work, looked at his queue of calls, found nothing there (because he came to work at the same time as the freshers who are supposed to redirect the calls) and so he left and went home.
What you need to do is to wait if there is no work at the moment. Probably the best way to do that is to use BlockingCollection instead of ConcurrentQueue.
Also, your design doesn't seem to that good. For example, there is lots of repeated code.

Related

DllNotFoundException: hid.dll C# Linux

I am connecting a RFIDReader using USB and trying to communicate with it. It works well on windows. But this program should run on my raspberry pi whose operating system is raspbian and it tells me that a dll called hid.dll cannot be found when the program tried using the API of the RFIDReader. I guess its because the hid.dll is specific to windows. Since Linux doesn't have hid.dll, is there anything I can do to solve the problem to still get my program run on Linux? Is there anything similar to Hid.dll in linux?
My code dealing with the reader is as follows. When it executes the method InitReader() the problem will appear. Hope there's anyone can help me, thanks a lot!
using SkyeTek.Devices;
using SkyeTek.Tags;
using SkyeTek.STPv3;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Threading;
namespace RFIDReader
{
/// <summary>
/// this class represents the RFID reader, used to set the reader, send commond to the reader and ask response from the reader
/// </summary>
public class RFIDReader : EventArgs
{
public Device rfidReader { get; set; }
public String tagID;
public String buffer;
public static int count;
Thread timer; //the thread responsible for counting the timeout
public static bool tagRead = false; // determine if a person can pay for a product when a tag is detected
public event EventHandler<RFIDReader> TagDetected; //Tag detected event, raised once a tag is detected by the reader
/// <summary>
/// initiate the reader, set it as auto scanning tags
/// </summary>
public void InitReader()
{
if (FindLinkedDevices())
{
Console.WriteLine("RFIDReader: Opening device: " + rfidReader.ToString());
rfidReader.Open();
AutoScanTags();
rfidReader.Close();
}
}
/// <summary>
/// scanning tags automatically
/// </summary>
public void AutoScanTags()
{
STPv3Request request;
STPv3Response response;
SkyeTek.Tags.Tag tag = new SkyeTek.Tags.Tag();
tag.Type = TagType.AUTO_DETECT;
// Build the SelectTag request
request = new STPv3Request();
request.Command = STPv3Commands.SELECT_TAG;
request.Tag = tag;
//set the reader's flag as loop mode and inventory mode. So The reader reads all the tags in the field and then continuously
//scans for any tags that enter the field and reports back their tag IDs.
request.Loop = true;
request.Inventory = true;
// Issue the request to the output device
while (true)
{
request.Issue(rfidReader);
response = request.GetResponse();
if (response.ResponseCode == STPv3ResponseCode.SELECT_TAG_PASS)
{
//paymentValid = false;
buffer = BitConverter.ToString(response.TID);
if (buffer.Length > 0 && !buffer.Equals(tagID)) //if the tagID is a new ID, rasie tagdetected event, send data to RFIDData instance and start the timer
{
if (timer != null && timer.IsAlive)
{
timer.Abort();
}
tagID = buffer;
OnTagDetected(tagID);
timer = new Thread(Timing);
timer.Start();
}
else
{
if (buffer.Length > 0 && count >= 5) // if the tagID is not a new ID, check the timeout. If time is over, rasie tagdetected event, send data to RFIDData instance and start the timer
{
tagID = buffer;
OnTagDetected(tagID);
timer = new Thread(Timing);
timer.Start();
}
/*else
{
if(buffer.Length >0 && count >= 3 && request.GetResponse() != null)
{
paymentValid = true;
}
}*/
}
}
}
}
/// <summary>
/// Find the readers connected to the computer
/// </summary>
/// <returns></returns>
public bool FindLinkedDevices()
{
Console.WriteLine("RFIDReader: Enumerating linked devices");
Device[] LinkedDevices = USBDeviceFactory.Enumerate();
//Identify the RFID reader
if (LinkedDevices.Length == 0)
{
Console.WriteLine("RFIDReader: No USB devices found");
return false;
}
else
{
foreach (Device i in LinkedDevices)
{
Console.WriteLine("RFIDReader: USB devices found ---> " + i.ToString());
if (i.ToString().Equals("SkyeTek.Devices.USBDevice"))
{
rfidReader = i;
}
}
return true;
}
}
/// <summary>
/// Rasie an tagDetected event once the reader detects a tag
/// </summary>
/// <param name="TID"></param>
protected virtual void OnTagDetected(String TID)
{
if (TagDetected != null)
{
TagDetected(this, new RFIDReader() { tagID = TID });
}
}
/// <summary>
/// timing method running on the timer thread
/// </summary>
public void Timing()
{
for (count = 0; count < 5; count++)
{
System.Threading.Thread.Sleep(1000);
}
}
}
}

Server startup error

Server start up error when trying to add a purger timer ( game feature) seems that the server is not allowed access?
PurgeTimer:
using System;
using System.Threading;
using System.Collections.Concurrent;
using Plus.HabboHotel.GameClients;
using Plus.HabboHotel.Rooms;
namespace Plus.HabboHotel.Minigames.Purge
{
/// <summary>
/// This will do a countdown before the match starts
/// </summary>
public class PurgeTimer
{
/// <summary>
/// Timer for our operation
/// </summary>
private Timer Timer;
public bool On = false;
/// <summary>
/// Constructor
/// </summary>
public PurgeTimer()
{
// Method to call when completed
TimerCallback TimerCallback = Ticked;
// Create a new instance of timer
Timer = new Timer(TimerCallback, null, 30000, Timeout.Infinite);
}
/// <summary>
/// Method is call when timer is finished
/// </summary>
/// <param name="info">The information</param>
public void Ticked(object info)
{
try
{
if (PurgeManager.Running)
{
foreach (GameClient client in PlusEnvironment.GetGame().GetClientManager()._clients.Values)
{
try
{
if (client == null)
{
continue;
}
client.SendWhisper("[Automatic Event Alert]: The hotel is currently under Purge Mode. All crime is legal.");
}
catch (Exception e)
{
}
}
Timer.Change(30000, Timeout.Infinite);
}
else
{
return;
}
}
catch { }
}
}
}
PurgeManager:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plus.HabboHotel.Minigames.Purge;
using System.Threading.Tasks;
namespace Plus.HabboHotel.Minigames.Purge
{
public class PurgeManager
{
public static PurgeTimer MainTimer;
public static bool Running;
}
}
Error:
http://prntscr.com/9ss0qb I don't get while it's not accessible!
Firstly, please post the error directly into the question, not as a linked image.
The error Plus.HabboHotel.GameClients.GameClientManager._clients is inaccessible due to its protection level seems pretty clear. From the naming convention used I'd take a guess that the _clients collection is private.
Here is your problem.
From your GameClientManager class, you're trying to access a private field.
private ConcurrentDictionary<int, GameClient> _clients;
For more details regarding your problem, review this. What are Access Modifiers in C#?

Matching MEF imports to exports

In the code below I am attempting to use MEF to match an import to a matching export. The TestMEFClass has an import and an export which share a matching contract name. The export should increment a counter every time it's called.
When I printed the export to the console, it did not increment the counter. Could someone point out my mistake?
Thank you very much,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace MEFConsoleTest {
public class TestMEFClass {
/// <summary>
/// This counter should increment everytime the getter in the ExportString property gets called.
/// </summary>
private int counter = 0;
[Export("Contract_Name")]
public string ExportString {
get {
return "ExportString has been called " + counter++.ToString();
}
}
[Import("Contract_Name")]
public string ImportString { get; set; }
/// <summary>
/// Default Constructor.
/// Make a catalog from this assembly, add it to the container and compose the parts.
/// </summary>
public TestMEFClass() {
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
class Program {
static void Main(string[] args) {
TestMEFClass testClass = new TestMEFClass();
Console.WriteLine(testClass.ImportString);
Console.WriteLine(testClass.ImportString);
Console.ReadLine();
}
}
For reasons I cannot explain at this moment I wasn't able to get MEF and properties imports/exports to work on a mutable property. However, using functions did the trick. I hope this code helps someone else.
Thanks,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace MEFConsoleTest {
public class TestMEFClass {
/// <summary>
/// This counter should increment everytime the getter in the ExportString property gets called.
/// </summary>
private int counter = 0;
[Export("Contract_Name")]
string ExportMethod() {
return ExportString;
}
public string ExportString {
get {
return "ExportString has been called " + counter++.ToString();
}
}
[Import("Contract_Name")]
Func<string> ImportMethod;
public string ImportString { get { return ImportMethod(); } }
/// <summary>
/// Default Constructor.
/// Make a catalog from this assembly, add it to the container and compose the parts.
/// </summary>
public TestMEFClass() {
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
class Program {
static void Main(string[] args) {
TestMEFClass testClass = new TestMEFClass();
for (int x = 0; x < 10; x++) {
Console.WriteLine(testClass.ImportString);
}
Console.ReadLine();
}
}
}

Unit testing in visual studio 2010 without a main method

Trying to unit test some simple code for a class project, however every time I try to run the test I get an error that there is no home.exe and no main static main method. However, we haven't gotten to the point where we are supposed to have either of those things yet, so how can I run the test without them?
My code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Home
{
class InventoryType
{
/// <summary>
/// Selects the inventory type and returns the selected value
/// </summary>
public class InventorySelect
{
private string inventoryTypes;
public String InventoryTypes
{
set
{
inventoryTypes = value;
}
get
{
return inventoryTypes;
}
}
/// <summary>
/// Validate that the inventory is returning some sort of value
/// </summary>
/// <returns></returns>
public bool Validate()
{
if (InventoryTypes == null) return false;
return true;
}
}
}
}
My Test Code
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Home.InventoryType.InventorySelect;
namespace HomeTest
{
[TestClass]
public class TestInventoryTypeCase
{
[TestMethod]
public void TestInventoryTypeClass()
{
InventorySelect select = new InventorySelect();
select.inventoryTypes = "Collection";
if (Validate() = true)
Console.WriteLine("Test Passed");
else
if (Validate() = false)
Console.WriteLine("Test Returned False");
else
Console.WriteLine("Test Failed To Run");
Console.ReadLine();
}
}
}
OK, a couple things here.
Make sure that your Output type for your main project (the project to be tested) is ClassLibrary
Use Assertions in your tests
I created a ClassLibrary solution called ExampleLibrary. Created a class called InventoryType and copied in your code e.g.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExampleLibrary
{
class InventoryType
{
/// <summary>
/// Selects the inventory type and returns the selected value
/// </summary>
public class InventorySelect
{
private string inventoryTypes;
public String InventoryTypes
{
set
{
inventoryTypes = value;
}
get
{
return inventoryTypes;
}
}
/// <summary>
/// Validate that the inventory is returning some sort of value
/// </summary>
/// <returns></returns>
public bool Validate()
{
if (InventoryTypes == null) return false;
return true;
}
}
}
}
I then created a Unit Test and coded it as follows:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ExampleLibrary;
namespace HomeTest
{
[TestClass]
public class TestInventoryTypeCase
{
[TestMethod]
public void TestInventoryTypeClass()
{
InventoryType.InventorySelect select = new InventoryType.InventorySelect();
select.InventoryTypes = "Collection";
Assert.IsTrue(select.Validate());
select.InventoryTypes = null;
Assert.IsFalse(select.Validate());
}
}
}
I compile and run the test as described above and it runs and returns Test Passed.
To run a test on the main menu bar at the top of your Visual Studio...
Test - Windows - Test Explorer
In the Test Explorer window, select the test you wish to run and click on the run Icon at the top of the window.

UWP - Perform long view model creation and keep UI responsive

I've build this example to show my issue.
I need to create a hierarchy to be shown in a treeview, treeview to be bound to the view model.
In the good old days of VB6 I would have used DoEvents to unlock the UI but here I'm not able to understand how to proceed.
On VS19 create a blank UWP project and call TestLoadSync it then copy paste this in the files:
App.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace TestLoadSync
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </su
/// mmary>
sealed partial class App : Application
{
public List<PropertyModel> _Properties;
public List<ImageModel> _ImagesHirerachy;
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
_Properties = new List<PropertyModel>();
_ImagesHirerachy = new List<ImageModel>();
}
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
}
/// <summary>
/// Invoked when Navigation to a certain page fails
/// </summary>
/// <param name="sender">The Frame which failed navigation</param>
/// <param name="e">Details about the navigation failure</param>
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}
/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
deferral.Complete();
}
}
}
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace TestLoadSync
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private ObservableCollection<PropertyViewModel> _PropVM;
public MainPage()
{
DataLayer aDAL = new DataLayer();
_PropVM = new ObservableCollection<PropertyViewModel>();
this.InitializeComponent();
ProgB.Maximum = 1;
aDAL.loadData();
Debug.WriteLine(((App)App.Current)._Properties.Count());
Debug.WriteLine(((App)App.Current)._ImagesHirerachy.Count());
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ProgB.Value = 0;
ProgB.Maximum = ((App)App.Current)._Properties.Count() + 1;
foreach (PropertyModel aProperty in ((App)App.Current)._Properties)
{
ProgB.Value++;
_PropVM.Add(new PropertyViewModel(aProperty));
}
ProgB.Value = ProgB.Maximum;
}
}
}
MainPage.xaml
<Page
x:Class="TestLoadSync.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:TestLoadSync"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
<Button
Width="296"
Height="143"
Margin="245,214,0,0"
VerticalAlignment="Top"
Click="Button_Click"
Content="Button" />
<ProgressBar
x:Name="ProgB"
Width="296"
Height="82"
Margin="245,82,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
</Page>
On the project add new... - Class - call it: DataLayer.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestLoadSync
{
public class PropertyModel
{
public int id;
public PropertyModel(int _id)
{
id = _id;
}
}
public class ImageModel
{
public int id;
public int property;
public int parent;
public string desc;
public ImageModel(int _id, int _property, int _parent, string _desc)
{
id = _id;
property = _property;
parent = _parent;
desc = _desc;
}
}
class PropertyViewModel
{
private PropertyModel _Property;
List<ImageViewModel> _Images;
public PropertyViewModel()
{
}
public PropertyViewModel(PropertyModel aProperty)
{
List<ImageModel> _SubImages;
_Property = aProperty;
Debug.WriteLine("Property: " + aProperty.id);
_Images = new List<ImageViewModel>();
_SubImages = ((App)App.Current)._ImagesHirerachy
.Where(x => x.property == aProperty.id && x.parent == 0)
.ToList();
foreach (ImageModel aImage in _SubImages)
{
_Images.Add(new ImageViewModel(aImage, 1));
}
}
}
class ImageViewModel
{
ImageModel _Image;
List<ImageViewModel> _Images;
public ImageViewModel()
{
}
public ImageViewModel(ImageModel aImage, int level)
{
List<ImageModel> _SubImages;
_Image = aImage;
string aS = new string('-', level);
Debug.WriteLine(" " + aS + aImage.id);
_Images = new List<ImageViewModel>();
_SubImages = ((App)App.Current)._ImagesHirerachy
.Where(x => x.parent == aImage.id && x.property == aImage.property)
.ToList();
foreach (ImageModel aSImage in _SubImages)
{
_Images.Add(new ImageViewModel(aSImage, ++level));
}
}
}
class DataLayer
{
private int maxProperties = 1000;
private int maxSubItems = 100;
public void loadData()
{
for (int i = 0; i < maxProperties; i++)
{
((App)App.Current)._Properties.Add(new PropertyModel(i));
}
for (int i = 0; i < maxSubItems; i++)
{
for (int j = 0; j < (i > maxSubItems / 2 ? maxSubItems / 2 : i); j++)
{
((App)App.Current)._ImagesHirerachy.Add(new ImageModel(maxProperties + i * (maxSubItems / 2) + j, i, 0, "-" + (((App)App.Current)._ImagesHirerachy.Count() + 1).ToString()));
}
}
for (int i = 0; i < maxSubItems; i++)
{
for (int j = 0; j < (i > maxSubItems / 4 ? maxSubItems / 4 : i); j++)
{
((App)App.Current)._ImagesHirerachy.Add(new ImageModel(maxProperties*2+ i * (maxSubItems/2) + j, i, maxProperties + i * (maxSubItems / 2) + j, "--" + (((App)App.Current)._ImagesHirerachy.Count() + 1).ToString()));
if (i == j)
{
((App)App.Current)._ImagesHirerachy.Add(new ImageModel(maxProperties * 4 + i * (maxSubItems / 2) + j, i, maxProperties*2 + i * (maxSubItems / 2) + j, "---" + (((App)App.Current)._ImagesHirerachy.Count() + 1).ToString()));
}
}
}
}
}
}
If you run it the ProgressBar will not fill in smoothly :( but it does not.
I've used simple numbers (1000/100) in the procedure to create the test structure but in my real case are much higher.
In the final app I'll use the MVVM light model and, obviously, the data will be read and saved from DB/File.
Note that the Models are both flat. The hierarchy is given thanks to the "parent" field in the image class that if is <>0 refers to the parent image. If is =0 then the image has to be attached to the property.
What I'm focusing here is how to create the ViewModel structures in the correct hierarchy so that I can bind the Page to the
_PropVM
and have the whole tree being built.
The ProgressBar can't response because your loop and the UI are executing on the same thread. While the loop is busy, it blocks the thread and can't update the UI. So you can calls the Task.Run( ) method to create a task and put time-consuming operations inside, then use await to perform asynchronous operations, like below:
private async void Button_Click(object sender, RoutedEventArgs e)
{
ProgB.Value = 0;
ProgB.Maximum = ((App)App.Current)._Properties.Count() + 1;
foreach (PropertyModel aProperty in ((App)App.Current)._Properties)
{
await Task.Run(() => _PropVM.Add(new PropertyViewModel(aProperty)));
ProgB.Value++;
}
ProgB.Value = ProgB.Maximum;
}

Categories