-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement-asynchronous-barcode-generation-with-taskrun-to-prevent-ui-thread-blocking-during-complex-mailmark-creation.cs
More file actions
124 lines (111 loc) · 4.99 KB
/
Copy pathimplement-asynchronous-barcode-generation-with-taskrun-to-prevent-ui-thread-blocking-during-complex-mailmark-creation.cs
File metadata and controls
124 lines (111 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Title: Asynchronous Mailmark Barcode Generation Example
// Description: Demonstrates generating Mailmark barcodes asynchronously to avoid UI thread blocking, saving each as a PNG file.
// Category-Description: This example belongs to the Aspose.BarCode complex barcode generation category, showcasing the use of ComplexBarcodeGenerator with MailmarkCodetext. It illustrates typical scenarios where developers need to create multiple Mailmark barcodes efficiently, leveraging asynchronous Task.Run to keep UI responsive. Key API classes include ComplexBarcodeGenerator, MailmarkCodetext, and BarCodeImageFormat, commonly used for high‑volume barcode creation in .NET applications.
// Prompt: Implement asynchronous barcode generation with Task.Run to prevent UI thread blocking during complex Mailmark creation.
// Tags: mailmark, barcode, asynchronous, task.run, complexbarcode, generation, png, aspnet, csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.ComplexBarcode;
using Aspose.Drawing;
/// <summary>
/// Demonstrates asynchronous generation of Mailmark barcodes using Aspose.BarCode.
/// </summary>
class Program
{
/// <summary>
/// Asynchronously generates a Mailmark barcode image and saves it to the specified path.
/// </summary>
/// <param name="mailmark">The Mailmark codetext containing barcode data.</param>
/// <param name="outputPath">Full file path where the PNG image will be saved.</param>
/// <returns>A task that resolves to the output path once the image is saved.</returns>
private static Task<string> GenerateMailmarkAsync(MailmarkCodetext mailmark, string outputPath)
{
// Run the generation on a background thread to avoid blocking the UI.
return Task.Run(() =>
{
// Ensure the output directory exists.
string directory = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
// Create the complex barcode generator with the Mailmark codetext.
using (var generator = new ComplexBarcodeGenerator(mailmark))
{
// Optional: set barcode colors.
generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Black;
generator.Parameters.BackColor = Aspose.Drawing.Color.White;
// Save the barcode image as PNG.
generator.Save(outputPath, BarCodeImageFormat.Png);
}
// Return the path of the generated file.
return outputPath;
});
}
/// <summary>
/// Entry point. Prepares sample Mailmark data, generates barcodes asynchronously, and outputs file locations.
/// </summary>
/// <param name="args">Command‑line arguments (not used).</param>
/// <returns>A task representing the asynchronous operation.</returns>
static async Task Main(string[] args)
{
// Prepare a list of sample Mailmark codetexts.
var mailmarks = new List<MailmarkCodetext>();
// Sample 1
var mailmark1 = new MailmarkCodetext
{
Format = 4, // Default/unspecified format
VersionID = 1,
Class = "0",
SupplychainID = 384224,
ItemID = 16563762,
DestinationPostCodePlusDPS = "EF61AH8T "
};
mailmarks.Add(mailmark1);
// Sample 2 (different ItemID)
var mailmark2 = new MailmarkCodetext
{
Format = 4,
VersionID = 1,
Class = "0",
SupplychainID = 384224,
ItemID = 16563763,
DestinationPostCodePlusDPS = "EF61AH8T "
};
mailmarks.Add(mailmark2);
// Sample 3 (different ItemID)
var mailmark3 = new MailmarkCodetext
{
Format = 4,
VersionID = 1,
Class = "0",
SupplychainID = 384224,
ItemID = 16563764,
DestinationPostCodePlusDPS = "EF61AH8T "
};
mailmarks.Add(mailmark3);
// Create tasks for asynchronous generation.
var tasks = new List<Task<string>>();
int index = 1;
foreach (var mailmark in mailmarks)
{
// Build a unique file name for each barcode.
string fileName = $"mailmark_{index}.png";
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
// Queue the generation task.
tasks.Add(GenerateMailmarkAsync(mailmark, outputPath));
index++;
}
// Await all generation tasks to complete.
string[] results = await Task.WhenAll(tasks);
// Output the locations of the generated barcode images.
foreach (var path in results)
{
Console.WriteLine($"Generated Mailmark barcode saved to: {path}");
}
}
}