Skip to content

Commit 28fd8ea

Browse files
Multithreading using tasks
1 parent 1277428 commit 28fd8ea

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Multithreading_using_tasks</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.PdfToImageConverter.Net" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35818.85 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading-using-tasks", "Multithreading-using-tasks.csproj", "{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {4229D954-1DCB-4D09-99C5-07520C20B09E}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Syncfusion.PdfToImageConverter;
2+
3+
namespace NET
4+
{
5+
internal class MultiThreading
6+
{
7+
//Indicates the number of threads to be create.
8+
private const int TaskCount = 1000;
9+
public static async Task Main()
10+
{
11+
//Create an array of tasks based on the TaskCount.
12+
Task[] tasks = new Task[TaskCount];
13+
for (int i = 0; i < TaskCount; i++)
14+
{
15+
tasks[i] = Task.Run(() => OpenPDFAndSaveImage());
16+
}
17+
//Ensure all tasks complete by waiting on each task.
18+
await Task.WhenAll(tasks);
19+
}
20+
21+
//Open a PDF document and save image using multi-threading.
22+
static void OpenPDFAndSaveImage()
23+
{
24+
using (FileStream inputStream = new FileStream(@"Data/Input.pdf", FileMode.Open, FileAccess.Read))
25+
{
26+
//Load an existing PDF document.
27+
using (PdfToImageConverter imageConverter = new PdfToImageConverter(inputStream))
28+
{
29+
Stream outputStream = imageConverter.Convert(0, false, false);
30+
31+
//Rewind the stream position to the beginning before copying.
32+
outputStream.Position = 0;
33+
34+
//Create file stream.
35+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output" + Guid.NewGuid().ToString() + ".jpeg"), FileMode.Create, FileAccess.ReadWrite))
36+
{
37+
//Save the image to file stream.
38+
outputStream.CopyTo(outputFileStream);
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}

PDF-to-image/Multithreading-using-tasks-in-.NET/Multithreading-using-tasks/bin/Debug/net8.0/Output/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)