Ben
Ben
CC#
Created by Ben on 11/16/2023 in #help
Best approach to SetThreadExecutionState
Hey! I got an app that takes a periodic screenshot of open windows. Some customers reported that after being idle for a while the screenshots seems to freeze. Did some googling and it seems like once the OS/Monitor goes into idle/sleep state it stops rendering new frames (somewhat similar to what happens when you minimize a video and check its preview via winkey+tab). This is the solution I found - https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate?redirectedfrom=MSDN Using the above my goal is to keep the OS rendering new frames so the screenshots are up to date but I don't actually need the monitor itself to be visible. I want to make sure I plan to use it correctly - On the app main thread I'll run -
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
If I understood correctly, using ES_CONTINUOUS means I don't have to run this command periodically, as long as the thread is alive the state will remain. ES_AWAYMODE_REQUIRED is basically exactly what I'm looking for to keep rendering new frames but not to force active monitor. ES_SYSTEM_REQUIRED is this necessary? And on application shutdown I'll run -
SetThreadExecutionState(ES_CONTINUOUS);
SetThreadExecutionState(ES_CONTINUOUS);
To revert changed settings. Sounds correct?
1 replies
CC#
Created by Ben on 5/7/2023 in #help
❔ Naming Conventions N-Tier architecture, conflicts?
Hey! Would like to hear additional feedbacks regarding this naming approach and a suggestion to solve conflicts. The general naming idea is explained in details here - https://softwareengineering.stackexchange.com/a/259840/429884 The summarize example would be (Taking User entity as example) -
ProjectName ->

ProjectName.API ->
Controllers ->
User ->
UserController.cs
UserStatisticsReport.cs

ProjectName.Model ->
User ->
User.cs

ProjectName.Business ->
User ->
UserLogic.cs

ProjectName.Data ->
DataLayer.cs
IRepository.cs
User ->
UserRepository.cs
ProjectName ->

ProjectName.API ->
Controllers ->
User ->
UserController.cs
UserStatisticsReport.cs

ProjectName.Model ->
User ->
User.cs

ProjectName.Business ->
User ->
UserLogic.cs

ProjectName.Data ->
DataLayer.cs
IRepository.cs
User ->
UserRepository.cs
*If the logic class gets too big we break it down to CQRS classes. Following the convention above how would you avoid explicitly writing namespaces between the Model layer and the Business layer? For example, you have this namespace in your Model layer - ProjectName.Models.User With a class called - User.cs. And this namespace in your Logic layer - ProjectName.Logic.User with a class called - UserLogic.cs. You can't use User.cs in UserLogic.cs without specifying its entire namespace as User is part of the Logic namespace What do you think? thx!
8 replies
CC#
Created by Ben on 3/5/2023 in #help
❔ MemoryCache
Added MemoryCache to my dotnet 6 app. Works fine on windows, ubuntu suddenly throws -
System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=7.0.0.0
System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=7.0.0.0
On library projects (that's referenced from the API) I need to install Microsoft.Extensions.Caching.Memory in order to use MemoryCache. On the API itself I don't, is that cause of the additional Framework thats installed on the API project? I've also checked earlier dotnet core versions, where I wasn't required to install this package, could that be a dotnet6 thing?
2 replies
CC#
Created by Ben on 2/2/2023 in #help
❔ MongoDB Update nested elements
MongoDB question, I'm trying to add a property to all of my existing documents. The thing is, my document has a property called Events, this property is an array of Event that might have multiple or 0 elements. I want to update all the nested elements of that property if exists. This is what I tried which doesn't work -
db.collectionName.updateMany(
{Events:{$ne: []}},
{ $set: { "Events.$[].NewPropertyTest" : false } }
)
db.collectionName.updateMany(
{Events:{$ne: []}},
{ $set: { "Events.$[].NewPropertyTest" : false } }
)
First part I'm making sure that events isn't an empty array. Second part I'm adding to all the child elements the NewPropertyTest and initializing it to false. My attempt fails with matchedCount:0. Any tips?
15 replies
CC#
Created by Ben on 9/13/2022 in #help
Download file, decompress and keep in memory
I have an endpoint that serves a gzipped .tsv file. I want to download the file, decompress it and work on the data, without saving it on disk, keeping everything in memory. Tried -
HttpClient _client = new HttpClient(
new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
});

var data = await _client.GetStringAsync(url + fileName);
HttpClient _client = new HttpClient(
new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
});

var data = await _client.GetStringAsync(url + fileName);
Which eventually contains gibberish so I'll guess the decompression doesn't work? any ideas? If I download the file manually and extract it I get a folder with the .tsv file in it. maybe my issue is that I'm trying to read the string value of the folder and not its content? Iv'e tried removing the AutoDecompression configuration and it seems to have the same result. Thanks!
17 replies
CC#
Created by Ben on 8/29/2022 in #help
LeetCode - Clone Graph [Answered]
Hey, trying to solve - https://leetcode.com/problems/clone-graph/ basically - question is to deep-clone a graph. Could anyone helped me figure out why my solution isn't valid? Error message - You must return a copy of all the nodes in the original graph My code -
public Node CloneGraph(Node node)
{
var map = new Dictionary<int, Node>();
var clone = new Node(node.val);
dfs(node, clone, map);
return clone;
}

public void dfs(Node source, Node target, Dictionary<int, Node> map)
{
foreach(var sourceNeighbor in source.neighbors)
{
Node targetNeighbor;
if(!map.TryGetValue(sourceNeighbor.val, out targetNeighbor))
{
targetNeighbor = new Node(sourceNeighbor.val);
map.Add(targetNeighbor.val, targetNeighbor);
dfs(sourceNeighbor, targetNeighbor, map);
}
target.neighbors.Add(targetNeighbor);
}
}
public Node CloneGraph(Node node)
{
var map = new Dictionary<int, Node>();
var clone = new Node(node.val);
dfs(node, clone, map);
return clone;
}

public void dfs(Node source, Node target, Dictionary<int, Node> map)
{
foreach(var sourceNeighbor in source.neighbors)
{
Node targetNeighbor;
if(!map.TryGetValue(sourceNeighbor.val, out targetNeighbor))
{
targetNeighbor = new Node(sourceNeighbor.val);
map.Add(targetNeighbor.val, targetNeighbor);
dfs(sourceNeighbor, targetNeighbor, map);
}
target.neighbors.Add(targetNeighbor);
}
}
6 replies
CC#
Created by Ben on 8/16/2022 in #help
How to locally loadbalance threadsafe [Answered]
Hola, I got an instance of a class that holds a list of end-points. The class performs and awaits an http request to one of the end-points in the list. The same instance of the class will be accessed async from multiple threads (about 300 times a second) I need to round-robin load-balance the endpoints in the list. Initially I thought of using a concurrentQueue and to dequeue/enqueue before and after every request. but the locking will hurt my performance as I have no need to wait for each request to finish before using the same end-point again.. I want that eventually within the 1 second, the 300 requests will be balanced between all the endpoints without delaying them. I thought of using counter field as an index accessor, that I keep adjusting its value in a circle according to the end-points length. what do you think? is there a better approach? Simplified example -
public class MyClass
{
List<string> _endpoints;
int _indexCounter = 0;
public MyClass(List<string> endpoints)
{
_endpoints = endpoints;
}

public Task SendRequest()
{
var endpoint = _endpoints[_indexCounter];
indexCounter = _indexCounter < _endpoints.Length-1 ? _indexCounter+1 : 0;

await client.Request(endpoint);
}
}
public class MyClass
{
List<string> _endpoints;
int _indexCounter = 0;
public MyClass(List<string> endpoints)
{
_endpoints = endpoints;
}

public Task SendRequest()
{
var endpoint = _endpoints[_indexCounter];
indexCounter = _indexCounter < _endpoints.Length-1 ? _indexCounter+1 : 0;

await client.Request(endpoint);
}
}
19 replies
CC#
Created by Ben on 8/14/2022 in #help
How would you design a solution to this problem?
unsure what would be the best approach - I have a SQL table that stores SMS 3rd party providers. Each provider support different protocols for its integration - (HTTP/SMPP). According to the provider supported protocol it requires different parameters to be stored. the HTTP providers stores - 1. URL endpoint 2. Api Key The SMPP providers stores - 1. IpAddress 2. Port 3. Username 4. Password Although they are different, they serve the same purpose therefore needs to exist in the same table. So my question is, how would you handle this scenario both in the db and in the code? Option 1 - In the DB have a single table for Provider with such schema -
ProviderId
AccountId
.... (some more columns that are shared between all provider)
Protocol
Custom1
Custom2
Custom3
Custom4
ProviderId
AccountId
.... (some more columns that are shared between all provider)
Protocol
Custom1
Custom2
Custom3
Custom4
Protocol is an enum of HTTP/SMPP And the Custom fields are nvarchar fields that are used according the the Protocol value. I can select all the providers into a generic Provider class. Implement an IProvider interface and concrete types - ProviderSMPP and ProviderHTTP Have a factory method that receives a list of Provider and returns a list of IProvider where I initialize each concrete type using a switch-case on the Protocol field mapping the custom fields into its concrete values. Option2 - Break the Provider table. Leaving the Provider table only with the shared fields, and create a relationship tables for ProviderSMPPCredentials and ProviderHTTPCredentials And join both tables? What would you do?
11 replies