0% found this document useful (0 votes)
21 views5 pages

Unit 3

Unit 3 of .net

Uploaded by

ixiixi439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Unit 3

Unit 3 of .net

Uploaded by

ixiixi439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

C# Assemblies

What is an Assembly?

 Assemblies are the building blocks of .NET applications, containing compiled code in the
form of Intermediate Language (IL).

 They are portable, reusable, and versionable units of code.

 Types of Assemblies:

o Private Assembly: Used by a single application.

o Shared Assembly: Accessible by multiple applications, typically stored in the Global


Assembly Cache (GAC).

Key Components of an Assembly

1. Manifest: Metadata about the assembly (name, version, culture, etc.).

2. IL Code: Compiled .NET code executed by the CLR.

3. Type Metadata: Information about classes, interfaces, etc.

4. Resources: Embedded files (e.g., images, strings).

Creating and Using Assemblies

 Compile Code into an Assembly:

csc /target:library MyLibrary.cs

 Referencing an Assembly in a Project:

using MyLibraryNamespace;

2. Global Assembly Cache (GAC)

 Definition:

o Central repository for shared assemblies.

 Adding an Assembly to GAC:

o Use the gacutil command:

gacutil -i MyAssembly.dll

 Locating Assemblies in GAC:

o Assemblies must have a strong name (using sn.exe to generate).

3. Threads

Introduction to Threads

 A thread is the smallest unit of execution in a process.


 Multithreading allows concurrent execution of multiple threads, improving performance.

Creating Threads in C#:

 Using the Thread class:

using System.Threading;

class Program

static void PrintNumbers()

for (int i = 1; i <= 5; i++)

Console.WriteLine(i);

static void Main()

Thread t = new Thread(PrintNumbers);

t.Start();

4. AppDomains

Definition:

 Application Domains (AppDomains) are isolated environments for running .NET applications,
providing fault isolation and security boundaries.

Creating an AppDomain:

AppDomain domain = AppDomain.CreateDomain("NewAppDomain");

Console.WriteLine(domain.FriendlyName);

Unloading an AppDomain:

AppDomain.Unload(domain);

5. Processes Concepts
Definition:

 A process is an executing instance of an application. Each process has its own memory space.

Process Management in C#:

 Using the System.Diagnostics namespace.

using System.Diagnostics;

class Program

static void Main()

Process.Start("notepad.exe");

6. Concurrency and Synchronization

Concurrency involves multiple threads executing simultaneously, while synchronization ensures


proper coordination between threads.

Locks and Monitors

 Locks: Prevent simultaneous access to shared resources.

private static readonly object lockObject = new object();

lock (lockObject)

// Critical section

 Monitors: Provide more control than lock by allowing Wait and Pulse.

Monitor.Enter(lockObject);

try

// Critical section

}
finally

Monitor.Exit(lockObject);

ReaderWriterLock

 Allows multiple threads to read or a single thread to write.

ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();

rwLock.EnterReadLock();

// Perform read operation

rwLock.ExitReadLock();

Mutexes

 Used for synchronization across processes.

Mutex mutex = new Mutex(false, "GlobalMutex");

if (mutex.WaitOne())

try

// Critical section

finally

mutex.ReleaseMutex();

Thread Pooling

 Definition:
o A pool of worker threads managed by .NET, reducing the overhead of creating new
threads.

 Using ThreadPool:

ThreadPool.QueueUserWorkItem(state => {

Console.WriteLine("Work executed in thread pool");

});

You might also like