Cryy
Cryy
CC#
Created by Cryy on 11/20/2024 in #help
Asynchronous batch processing (queue) in C#
Hey guys, I have some batches to process and to do so I have to send each of my batches to external APi to process. I have no experience with async programming so I simplified it a bit by using Parallel.Foreach that create thread/task for each API calling and waiting for response. The reason I dont want to call API synchronously Is that processing of one batch can take a one sec or 30min (max). With the Parallel.Foreach solution it looks it works fine but form what I read on internet it's not a good approach so I want to implement kind of async queue that will take a few items from my collection of batches starts processing and after some of them is finished replace it with another etc. I tried to came up with something using ChatGPT but it only process specified number of batches (3 in my case) and than it does nothing but even if the code is short there are lot's of new concepts for me so I have no idea how to fixed this.
public async Task ProcessBatchQueueAsync(IEnumerable<int> batchIds)
{
var semaphore = new SemaphoreSlim(3);
var tasks = new List<Task>();

foreach (var batchId in batchIds)
{
await semaphore.WaitAsync();

var task = Task.Run(async () =>
{
try
{
await ProcessSingleBatchAsync(batchId);
}
finally
{
semaphore.Release();
}
});

tasks.Add(task);
}

await Task.WhenAll(tasks);
}

private async Task ProcessSingleBatchAsync(int batchId)
{
_loggerController.Log(LogLevel.Info, $"Processing batch {batchId}...");
await Task.Delay(2000);
_loggerController.Log(LogLevel.Info, $"Finished processing batch {batchId}...");
}
public async Task ProcessBatchQueueAsync(IEnumerable<int> batchIds)
{
var semaphore = new SemaphoreSlim(3);
var tasks = new List<Task>();

foreach (var batchId in batchIds)
{
await semaphore.WaitAsync();

var task = Task.Run(async () =>
{
try
{
await ProcessSingleBatchAsync(batchId);
}
finally
{
semaphore.Release();
}
});

tasks.Add(task);
}

await Task.WhenAll(tasks);
}

private async Task ProcessSingleBatchAsync(int batchId)
{
_loggerController.Log(LogLevel.Info, $"Processing batch {batchId}...");
await Task.Delay(2000);
_loggerController.Log(LogLevel.Info, $"Finished processing batch {batchId}...");
}
22 replies
CC#
Created by Cryy on 10/29/2024 in #help
Can I check-in virus/trojan to TFS/DevOPS
No description
13 replies
CC#
Created by Cryy on 9/13/2024 in #help
Create data during EF6 migrations
Hey guys, in our project we use some kind of "changelog implementation" to track all changes in our data. For some reason we also need to create changelongs with default values in case we add let says new columns to DB tables and I dont know what's the best way to do it. Currently I'm playing with some kind of DbCommandInterceptor that detects DB update process (applying migrations), parse them and create records accordingly but I'm not sure if that's correct approach. One of the reason I dont like it it's because it has to parse every command to DB and calculate if it is or if it's not the migration but maybe there is smarter way to do that. Any experience with it ? Thanks
8 replies
CC#
Created by Cryy on 7/25/2024 in #help
Performance question
Hey guys, I have an web app and I tried to optimize the performance of one report. One stuff that seems unefficient to me is this one:
var searchedResults = query.Count();
if (index.HasValue && pageSize.HasValue)
{
query = query.Skip(index.Value).Take(pageSize.Value);
}

var result = query.ToList();

var total = new SimsSiteInventoryReportData
{
QuantityOnSite = query.Sum(q => q.QuantityOnSite),
QuantityDispatchedToSite = query.Sum(q => q.QuantityDispatchedToSite),
QuarantinedOnSite = query.Sum(q => q.QuarantinedOnSite)
};
var searchedResults = query.Count();
if (index.HasValue && pageSize.HasValue)
{
query = query.Skip(index.Value).Take(pageSize.Value);
}

var result = query.ToList();

var total = new SimsSiteInventoryReportData
{
QuantityOnSite = query.Sum(q => q.QuantityOnSite),
QuantityDispatchedToSite = query.Sum(q => q.QuantityDispatchedToSite),
QuarantinedOnSite = query.Sum(q => q.QuarantinedOnSite)
};
If I understand it correctly, it'll execute the whole query 4 (5 if I count also counting but guess it's necessary) times so I tried to do optimize it to do everything in one step like this:
var result = new List<SimsSiteInventoryReportData>();
var total = new SimsSiteInventoryReportData();

foreach (var item in query)
{
total.QuantityOnSite += item.QuantityOnSite;
total.QuantityDispatchedToSite += item.QuantityDispatchedToSite;
total.QuarantinedOnSite += item.QuarantinedOnSite;
result.Add(item);
};
var result = new List<SimsSiteInventoryReportData>();
var total = new SimsSiteInventoryReportData();

foreach (var item in query)
{
total.QuantityOnSite += item.QuantityOnSite;
total.QuantityDispatchedToSite += item.QuantityDispatchedToSite;
total.QuarantinedOnSite += item.QuarantinedOnSite;
result.Add(item);
};
but according the Network in Chrome this optimalization is even a bit slower and I cant understand why. Any explanation ? Thanks
32 replies
CC#
Created by Cryy on 3/18/2024 in #help
Moving to GIT
No description
48 replies
CC#
Created by Cryy on 2/23/2024 in #help
.NET tool/library for communication/network logging
Hye guys, I was asked to do some "research" to find some fancy/cool way to logging communication between our application and webservices, external services, stuff like that. Original idea was just log requests/responses, some data to database but It turned out not to be ideal. Is there some standard way how to achieve that, maybe some third party library u use in your projects ? I'd really appreciate some input/advice. Thank you 🙂
15 replies
CC#
Created by Cryy on 1/1/2024 in #help
Windows 11 Dev Drive - file types
Hey guys, anyone using Dev Drive on Windows ? From what I heard it supposed to used only for source codes but is there any real catch if I use it also for storing unity assets, DB backups, bunch of different files ?
6 replies
CC#
Created by Cryy on 10/11/2023 in #help
❔ Docx behave differently after being rezipped
Hi, I'm trying to convert docx file -> PDF by using some library but there is an weird issue. Final PDF is generated without one picture in footer that is present in original docx file. But, it works correctly of I do one of these things: 1) open docx and than save it (save as) again, so new file works 2) unzip and than zip docx file to the new file by using System.IO.Compression.ZipFile. In first case I can see some differences in docx unzipped xmls but in second case everything seems to be the same so I really dont understand why that make a difference. Any idea why is that happening ? Thanks
2 replies
CC#
Created by Cryy on 9/9/2023 in #help
❔ Tabs in GemBox.Document
Hey guys, I'm "migrating" from Syncfusion to Gembox.Document and everything works fine but I have some issue with creating footer. Our previous code for syncfusion created footer using Tabs like this:
var footerPar = section.HeadersFooters.Footer.AddParagraph();
footerPar.ParagraphFormat.Tabs.AddTab(410f, TabJustification.Centered, TabLeader.NoLeader);
footerPar.ParagraphFormat.Tabs.AddTab(820f, TabJustification.Right, TabLeader.NoLeader);

var left = footerPar.AppendText("Some text\t");
ApplyNormalStyle(left.CharacterFormat, false);

var center = footerPar.AppendText($"Some date \t");
ApplyNormalStyle(center.CharacterFormat, false);

var right = footerPar.AppendText(" Page ");
ApplyNormalStyle(right.CharacterFormat, false);
var footerPar = section.HeadersFooters.Footer.AddParagraph();
footerPar.ParagraphFormat.Tabs.AddTab(410f, TabJustification.Centered, TabLeader.NoLeader);
footerPar.ParagraphFormat.Tabs.AddTab(820f, TabJustification.Right, TabLeader.NoLeader);

var left = footerPar.AppendText("Some text\t");
ApplyNormalStyle(left.CharacterFormat, false);

var center = footerPar.AppendText($"Some date \t");
ApplyNormalStyle(center.CharacterFormat, false);

var right = footerPar.AppendText(" Page ");
ApplyNormalStyle(right.CharacterFormat, false);
but i dont know how to recreate this using GemBox. I'm trying it using Run instances but I dont how how to add SpecialCharacter to it so it does not looks like it should. Any experience with that ? Thank you
2 replies
CC#
Created by Cryy on 9/9/2023 in #help
❔ Log4Net stopped works
Hey guys, any experience with log4net ? It stopped logging to DB from version 2.0.14. I tried previous version and it works fine but after update to 2.0.14 it does log nothing to DB (works for file though). After rollback everything is ok. Any idea why ?
3 replies
CC#
Created by Cryy on 8/6/2023 in #help
❔ Free .NET library to convert Word/Excel files to PDF
Hey guys, is there any free library to convert Word/Excel files to PDF available ? Currently we are using Synfusion libraries but we are looking for something inexpensive or even free. Any ideas ? 🙂 Thanks
15 replies
CC#
Created by Cryy on 12/28/2022 in #help
❔ Visual Studio extension
3 replies
CC#
Created by Cryy on 11/15/2022 in #help
❔ Sort dictionary according to the list of string (sorted keys)
Hey guys, I have a list od strings in specific order and dictionary with keys that each of keys is one of the string from the list. Is there any effective way to sort the dictionary according to the list of strings ? Thank
5 replies
CC#
Created by Cryy on 11/2/2022 in #help
Creating DataTable columns dynamically - C
Hey guys, currently I have an implementation of jQuery DataTables that wont allow to add columns dynamically. Now I need that fhe option to generate columns based of result of Linq query (database query result) but I'm not sure how to implement repositoy method for that. It's a good idea to put these dynamic columns into Expando object ? Thanks
1 replies
CC#
Created by Cryy on 10/25/2022 in #help
Parsing string to TimeSpan
Hey guys, I'm trying to parse some string to TimeSpan . Input string is "01:02:01" and I'm trying it to parse to TimeSpan structure like this TimeSpan.ParseExact(inputString, "dd:hh:mm", CultureInfo.InvariantCulture) (expecting days:hours:minutes) but I got "Input string was not in a correct format." exception. What I'm doing wrong ? Thanks
18 replies
CC#
Created by Cryy on 8/18/2022 in #help
Automapper 9.x.x - 10.x.x
1 replies
CC#
Created by Cryy on 8/17/2022 in #help
Autofac 5.x.x - 6.x.x
1 replies