Skip to content

Commit 76ce097

Browse files
author
Srinivas Hariharan
committed
Add project files.
1 parent a1cbe59 commit 76ce097

5 files changed

+130
-0
lines changed

CSharpThreads.sln.DotSettings

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">False</s:Boolean>
3+
<s:Boolean x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=2702F7F8DE47304EBD35F13B201370CD/@KeyIndexDefined">True</s:Boolean>
4+
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=2702F7F8DE47304EBD35F13B201370CD/AbsolutePath/@EntryValue">D:\Personal\Srinivas\CSharpThreads\CSharpThreads.sln.DotSettings</s:String>
5+
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=2702F7F8DE47304EBD35F13B201370CD/RelativePath/@EntryValue"></s:String>
6+
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File2702F7F8DE47304EBD35F13B201370CD/@KeyIndexDefined">True</s:Boolean>
7+
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File2702F7F8DE47304EBD35F13B201370CD/RelativePriority/@EntryValue">1</s:Double>
8+
9+
10+
11+
12+
</wpf:ResourceDictionary>

CSharpThreads/Monitor.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

CSharpThreads/Program.cs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Threading;
3+
4+
namespace CSharpThreads {
5+
class Program
6+
{
7+
private static readonly object Monitor = new object();
8+
9+
private static int _numLimit;
10+
static void Main() {
11+
Console.WriteLine("Enter a number till which odd and even numbers would be printed using Monitor: ");
12+
_numLimit = int.Parse(Console.ReadLine() ?? string.Empty);
13+
PrintNumbers();
14+
Console.ReadKey();
15+
}
16+
17+
private static void PrintNumbers()
18+
{
19+
var evenThread = new Thread(PrintEven);
20+
var oddThread = new Thread(PrintOdd);
21+
22+
evenThread.Start();
23+
Thread.Sleep(100);
24+
oddThread.Start();
25+
}
26+
27+
private static void PrintEven()
28+
{
29+
try
30+
{
31+
System.Threading.Monitor.Enter(Monitor);
32+
33+
for (var i = 0; i <= _numLimit; i += 2)
34+
{
35+
Console.WriteLine(i);
36+
System.Threading.Monitor.Pulse(Monitor);
37+
Console.WriteLine("Comes after pulse");
38+
var isLastItem = i >= (_numLimit);
39+
if (!isLastItem)
40+
{
41+
System.Threading.Monitor.Wait(Monitor);
42+
}
43+
}
44+
45+
}
46+
catch (Exception ex)
47+
{
48+
Console.WriteLine(ex.Message);
49+
}
50+
finally
51+
{
52+
System.Threading.Monitor.Exit(Monitor);
53+
}
54+
}
55+
56+
private static void PrintOdd()
57+
{
58+
59+
try {
60+
61+
System.Threading.Monitor.Enter(Monitor);
62+
for (int i = 1; i <= _numLimit; i += 2) {
63+
Console.WriteLine(i);
64+
System.Threading.Monitor.Pulse(Monitor);
65+
66+
var isLastItem = i >= (_numLimit);
67+
if (!isLastItem) {
68+
System.Threading.Monitor.Wait(Monitor);
69+
}
70+
}
71+
} catch (Exception) {
72+
73+
} finally {
74+
System.Threading.Monitor.Exit(Monitor);
75+
}
76+
}
77+
}
78+
}

ThreadSynchronization.sln

+25
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 16
4+
VisualStudioVersion = 16.0.31025.194
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Monitor", "CSharpThreads\Monitor.csproj", "{92C340C8-769A-409A-A596-A104C92BAEBB}"
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+
{92C340C8-769A-409A-A596-A104C92BAEBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{92C340C8-769A-409A-A596-A104C92BAEBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{92C340C8-769A-409A-A596-A104C92BAEBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{92C340C8-769A-409A-A596-A104C92BAEBB}.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 = {E83AEFDD-11AB-4704-AD07-7FE35E8E5C86}
24+
EndGlobalSection
25+
EndGlobal

ThreadSynchronization.sln.DotSettings

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">False</s:Boolean>
3+
<s:Boolean x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=2702F7F8DE47304EBD35F13B201370CD/@KeyIndexDefined">True</s:Boolean>
4+
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=2702F7F8DE47304EBD35F13B201370CD/AbsolutePath/@EntryValue">D:\Personal\Srinivas\CSharpThreads\CSharpThreads.sln.DotSettings</s:String>
5+
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=2702F7F8DE47304EBD35F13B201370CD/RelativePath/@EntryValue"></s:String>
6+
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File2702F7F8DE47304EBD35F13B201370CD/@KeyIndexDefined">True</s:Boolean>
7+
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File2702F7F8DE47304EBD35F13B201370CD/RelativePriority/@EntryValue">1</s:Double></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)