Skip to content

Commit fa71fb2

Browse files
authored
Merge pull request #49 from SyncfusionExamples/multithreading
941854: UG Documentation for multi threading and thread safety in PdfToImageConverter
2 parents 1277428 + 06b818b commit fa71fb2

File tree

9 files changed

+167
-0
lines changed

9 files changed

+167
-0
lines changed
Binary file not shown.
Lines changed: 15 additions & 0 deletions
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_parallel_process</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>
Lines changed: 25 additions & 0 deletions
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-parallel-process", "Multithreading-using-parallel-process.csproj", "{1989562F-B2D5-4030-B4B5-7CE03A727A4F}"
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+
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1989562F-B2D5-4030-B4B5-7CE03A727A4F}.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 = {3C50F3BF-01C8-4496-AB89-EE451C32138B}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Syncfusion.PdfToImageConverter;
2+
3+
namespace Multithreading_using_parallel_process
4+
{
5+
class MultiThreading
6+
{
7+
static void Main(string[] args)
8+
{
9+
//Indicates the number of threads to be create.
10+
int limit = 5;
11+
Console.WriteLine("Parallel For Loop");
12+
Parallel.For(0, limit, count =>
13+
{
14+
Console.WriteLine("Task {0} started", count);
15+
//Create multiple PDF document, one document on each thread.
16+
ConvertPdfToImage(count);
17+
Console.WriteLine("Task {0} is done", count);
18+
});
19+
}
20+
//Open and save a PDF document using multi-threading.
21+
static void ConvertPdfToImage(int count)
22+
{
23+
using (FileStream inputStream = new FileStream(@"../../../Data/Input.pdf", FileMode.Open, FileAccess.Read))
24+
{
25+
//Load an existing PDF document.
26+
using (PdfToImageConverter imageConverter = new PdfToImageConverter(inputStream))
27+
{
28+
Stream outputStream = imageConverter.Convert(0, false, false);
29+
30+
//Rewind the stream position to the beginning before copying.
31+
outputStream.Position = 0;
32+
33+
//Create file stream.
34+
using (FileStream outputFileStream = new FileStream("Output" + count + ".jpeg", FileMode.Create, FileAccess.Write))
35+
{
36+
//Save the image to file stream.
37+
outputStream.CopyTo(outputFileStream);
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
Binary file not shown.
Lines changed: 15 additions & 0 deletions
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>
Lines changed: 25 additions & 0 deletions
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

PDF-to-image/Multithreading-using-tasks-in-.NET/Output/.gitkeep

Whitespace-only changes.
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 Multithreading_using_tasks
4+
{
5+
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(() => ConvertPdfToImage());
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 ConvertPdfToImage()
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+
}

0 commit comments

Comments
 (0)