Skip to content

941854: UG Documentation for multi threading and thread safety in PdfToImageConverter #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Multithreading_using_parallel_process</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.PdfToImageConverter.Net" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35818.85 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading-using-parallel-process", "Multithreading-using-parallel-process.csproj", "{1989562F-B2D5-4030-B4B5-7CE03A727A4F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3C50F3BF-01C8-4496-AB89-EE451C32138B}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Syncfusion.PdfToImageConverter;

namespace Multithreading_using_parallel_process
{
class MultiThreading
{
static void Main(string[] args)
{
//Indicates the number of threads to be create.
int limit = 5;
Console.WriteLine("Parallel For Loop");
Parallel.For(0, limit, count =>
{
Console.WriteLine("Task {0} started", count);
//Create multiple PDF document, one document on each thread.
ConvertPdfToImage(count);
Console.WriteLine("Task {0} is done", count);
});
}
//Open and save a PDF document using multi-threading.
static void ConvertPdfToImage(int count)
{
using (FileStream inputStream = new FileStream(@"../../../Data/Input.pdf", FileMode.Open, FileAccess.Read))
{
//Load an existing PDF document.
using (PdfToImageConverter imageConverter = new PdfToImageConverter(inputStream))
{
Stream outputStream = imageConverter.Convert(0, false, false);

//Rewind the stream position to the beginning before copying.
outputStream.Position = 0;

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output" + count + ".jpeg", FileMode.Create, FileAccess.Write))
{
//Save the image to file stream.
outputStream.CopyTo(outputFileStream);
}
}
}
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Multithreading_using_tasks</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.PdfToImageConverter.Net" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35818.85 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading-using-tasks", "Multithreading-using-tasks.csproj", "{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4359DB27-1E8B-4B21-AE6B-7A5E3AE58022}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4229D954-1DCB-4D09-99C5-07520C20B09E}
EndGlobalSection
EndGlobal
Empty file.
44 changes: 44 additions & 0 deletions PDF-to-image/Multithreading-using-tasks-in-.NET/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Syncfusion.PdfToImageConverter;

namespace Multithreading_using_tasks
{
class MultiThreading
{
//Indicates the number of threads to be create.
private const int TaskCount = 1000;
public static async Task Main()
{
//Create an array of tasks based on the TaskCount.
Task[] tasks = new Task[TaskCount];
for (int i = 0; i < TaskCount; i++)
{
tasks[i] = Task.Run(() => ConvertPdfToImage());
}
//Ensure all tasks complete by waiting on each task.
await Task.WhenAll(tasks);
}

//Open a PDF document and save image using multi-threading.
static void ConvertPdfToImage()
{
using (FileStream inputStream = new FileStream(@"../../../Data/Input.pdf", FileMode.Open, FileAccess.Read))
{
//Load an existing PDF document.
using (PdfToImageConverter imageConverter = new PdfToImageConverter(inputStream))
{
Stream outputStream = imageConverter.Convert(0, false, false);

//Rewind the stream position to the beginning before copying.
outputStream.Position = 0;

//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Output" + Guid.NewGuid().ToString() + ".jpeg"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the image to file stream.
outputStream.CopyTo(outputFileStream);
}
}
}
}
}
}