2- // الصيغة العامة النشاء الثريدات
// تضمين فضاء االسماء (المكتبة) الخاصة بالثريدات:اوال
using [Link];
// لدينا عدة طرق النشاء الثريد وتشغيلة
// الطريقة االولى
static void Main (string [] args)
{
// Create thread
Thread Thread_Name = new Thread(function_name);
// start thread
Thread_Name.Start();
}
// لدينا عدة طرق النشاء الثريد وتشغيلة
// الطريقة الثانية
static void Main (string [] args)
{
// Create thread
Thread Thread_Name = new Thread (new ThreadStart(function_name));
// start newly created thread
Thread_Name.Start();
}
-------------------------------------------------------------------------------
//هل يمكن تشيل الثريد اكثر من مرة
using System;
using [Link];
namespace Threading
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread(function1);
[Link] = "mythread";
[Link]();
[Link]();// Error
[Link]();
}
static void function1()
{
[Link]("A");
[Link](1000);
}
}
}
---------------------------------------------------------------------------------
--------
// معرفة عدد الثريدات الشغالة في الجهاز
using System;
using [Link];
namespace Threading
{
class Program
{
static void Main(string[] args)
{
[Link]([Link]);
[Link]();
}
}
}
---------------------------------------------------------------------------------
--------
1 - // Main_Thread ………
//Single Threaded Model Example
using System;
using [Link];
namespace Main_Thread
{
class Program
{
// Main method
static void Main (string [] args)
{
PrintInfo1();
PrintInfo2();
[Link]();
}
static void PrintInfo1()
{
for (int i = 1; i <= 4; i++)
{
[Link]("i value: {0}", i);
[Link](1000);
}
[Link]("First method completed");
}
static void PrintInfo2()
{
for (int i = 1; i <= 4; i++)
{
[Link]("i value: {0}", i);
}
[Link]("Second method completed");
}
}
}
//Multithreading Example
using System;
// تضمين فضاء االسماء (المكتبة) الخاصة بالثريدات:اوال
using [Link];
namespace Multithreading
{
class Program
{
// Main method
static void Main (string [] args)
{
// Creating and initializing child threads
Thread t1 = new Thread (PrintInfo1);
Thread t2 = new Thread (PrintInfo2);
[Link]();
[Link]();
[Link]();
}
static void PrintInfo1()
{
for (int i = 1; i <= 4; i++)
{
[Link]("i value: {0}", i);
[Link](1000);
}
[Link]("First method completed");
}
static void PrintInfo2()
{
for (int i = 1; i <= 4; i++)
{
[Link]("i value: {0}", i);
}
[Link]("Second method completed");
}
}
}
//كيفية انشاء مصفوفة ثريدات
using System;
using [Link];
namespace Threading
{
class Program
{
static void Main(string[] args)
{
Thread[] Object1 = new Thread[3];
Object1[0] = new Thread(function);
Object1[1] = new Thread(function);
Object1[2] = new Thread(function);
Object1[0].Name = "thread1";
Object1[1].Name = "thread2";
Object1[2].Name = "thread3";
Object1[0].Start();
Object1[1].Start();
Object1[2].Start();
[Link]();
}
static void function ()
{
[Link]([Link]);
}
}
}
//انشاء عدة ثريدات باستخدام الدوارات
using System;
using [Link];
namespace Threading
{
class Program
{
static void print()
{
[Link] = [Link];
[Link]([Link] + "Is Start");
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}
//انشاء عدة ثريدات باستخدام الدوارات
using System;
using [Link];
namespace Threading
{
class Program
{
static void print()
{
[Link] = [Link];
[Link]([Link] + "Is Start");
//[Link]([Link](1));
[Link](1000);
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}
---------------------------------------------------------------------------------
----------------------------
//انشاء عدة ثريدات باستخدام الدوارات
using System;
using [Link];
namespace Threading
{
class Program
{
static void print()
{
[Link] = [Link];
[Link]([Link] + "Is Start");
//[Link]([Link](1));
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link] = true;
[Link]();
}
}
}
}
//انشاء عدة ثريدات باستخدام الدوارات
using System;
using [Link];
namespace Threading
{
class Program
{
static void print()
{
[Link] = [Link];
[Link]([Link] + "Is Start");
[Link](1000);
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link] = true;
[Link]();
}
}
}
}
// sleep in Main_Thread
using System;
using [Link];
namespace sleep
{
class Program
{
// Main Thread
static void Main (string [] args)
{
for (int i = 1; i <= 5; i++)
{
[Link]("Thread paused for {0} second ", 1);
//paused Thread for 1 second
[Link](1000);
[Link]("i value: " + i);
}
[Link]("Main Thread Exists");
[Link]();
}
}
}
// sleep (Time Span) in Main_Thread
using System;
using [Link];
namespace sleep
{
class Program
{
// Main Thread
static void Main (string [] args)
{
for (int i = 1; i <= 5; i++)
{
[Link]("Thread paused for {0} second ", 1);
//paused Thread for 1 second
[Link]([Link](1));
[Link]("i value: " + i);
}
[Link]("Main Thread Exists");
[Link]();
}
}
}
// Name
using System;
using [Link];
namespace Name_Thread
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread(function1);
[Link] = "mythread";
[Link]();
[Link]([Link] );
[Link]();
}
static void function1()
{
[Link]([Link]);
}
}
}
// sleep in Sub Thread
using System;
using [Link];
namespace sleep
{
class Program
{
// Main Thread
static void Main (string [] args)
{
Thread th1 = new Thread (PrintInfo);
[Link]();
[Link]();
}
static void PrintInfo ()
{
for (int i = 1; i <= 5; i++)
{
[Link]("i value: " + i);
//paused Thread for 1 second
[Link](1000);
}
[Link]("Sub Thread Exists");
}
}
}
// sleep (Time Span) in Sub Thread
using System;
using [Link];
namespace sleep
{
class Program
{
// Main Thread
static void Main (string [] args)
{
Thread th1 = new Thread (PrintInfo);
[Link]();
[Link]();
}
static void PrintInfo ()
{
for (int i = 1; i <= 5; i++)
{
[Link]("i value: " + i);
//paused Thread for 1 second
[Link]([Link](1));
}
[Link]("Sub Thread Exists");
}
}
}
//Foreground thread
using System;
using [Link];
namespace Foreground_thread
{
class Program
{
// Main method
static void Main (string [] args)
{
// Creating and initializing thread
Thread th1 = new Thread(mythread);
[Link]();
[Link]("Main Thread Ends !!");
[Link]();
}
// static method
static void mythread ()
{
for (int i = 1; i <= 5; i++)
{
[Link]("mythread is in progress !!");
//paused Thread for 1 second
[Link](1000);
}
[Link]("mythread ends!! ");
}
}
}
//Background thread
using System;
using [Link];
namespace Background_thread
{
class Program
{
// Main method
static void Main (string [] args)
{
// Creating and initializing thread
Thread th1 = new Thread(mythread);
// Name of the thread is Mythread
[Link] = "Mythread";
// IsBackground is the property of Thread
// which allows thread to run in the background
[Link] = true;
// start thread
[Link]();
[Link]("Main Thread Ends !!");
[Link]();
}
// static method
static void mythread ()
{
// Display the name of the
// current working thread
[Link]("In progress thread is: {0}",
[Link]);
[Link](2000);
[Link]("Completed thread is: {0}",
[Link]);
}
}
}
//Join()
using System;
using [Link];
namespace Join_method
{
class Program
{
// Main method
static void Main (string [] args)
{
// Creating and initializing child threads
Thread t1 = new Thread(function1);
Thread t2 = new Thread(function2);
[Link]();
[Link]();
[Link]();
for (char i = 'A'; i < 'Z'; i++)
{
[Link]("Main Thread:" + i);
//[Link](1000);
}
[Link]();
}
static void function1()
{
for (int i = 0; i < 10; i++)
{
[Link]("function1: {0}", i);
[Link](1000);
}
[Link]("First method completed");
}
static void function2()
{
for (int i = 10; i < 20; i++)
{
[Link]("function2:" + i);
[Link](1000);
}
[Link]("Second method completed");
}
}
}
//Join ()
using System;
using [Link];
namespace Join_method
{
class Program
{
// Main method
static void Main (string [] args)
{
// Creating and initializing child threads
Thread t1 = new Thread(function1);
Thread t2 = new Thread(function2);
[Link]();
[Link]();
[Link]();
[Link]();
for (char i = 'A'; i < 'Z'; i++)
{
[Link]("Main Thread:" + i);
//[Link](1000);
}
[Link]();
}
static void function1()
{
for (int i = 0; i < 10; i++)
{
[Link]("function1: {0}", i);
[Link](1000);
}
[Link]("First method completed");
}
static void function2()
{
for (int i = 10; i < 20; i++)
{
[Link]("function2:" + i);
[Link](1000);
}
[Link]("Second method completed");
}
}
}
// Abort ()
using System;
using [Link];
namespace Abort_method
class Program
{
// Main method
static void Main (string [] args)
{
// Creating and initializing child threads
Thread th1 = new Thread(function1);
Thread th2 = new Thread(function2);
[Link]();
[Link]();
[Link]("child Thread th1 is Abort");
[Link]();
[Link]();
}
static void function1()
{
for (int i = 0; i < 10; i++)
{
[Link]("function1: {0}", i);
[Link](1000);
}
[Link]("First method completed");
}
static void function2()
{
for (int i = 10; i < 20; i++)
{
[Link]("function2:" + i);
[Link](1000);
}
[Link]("Second method completed");
}
}
}
// IsBackground
// التحقق من اللثريد هل شغال في الخلفية ام ال
using System;
using [Link];
namespace IsBackground
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread(function1);
[Link] = "mythread";
[Link]();
[Link]([Link] );
[Link]();
}
static void function1()
{
[Link](1000);
}
}
}
---------------------------------------------------------------------------------
--
// IsBackground
// التحقق من اللثريد هل شغال في الخلفية ام ال
using System;
using [Link];
namespace IsBackground
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread(function1);
[Link] = "mythread";
[Link] = true;
[Link]();
[Link]([Link] );
[Link]();
}
static void function1()
{
[Link](1000);
}
}
}
---------------------------------------------------------------------------------
---
// IsBackground
using System;
using [Link];
namespace IsBackground
{
class Program
{
static void Main (string [] args)
{
// Creating and initializing child threads
Thread thr1 = new Thread(function1);
[Link] = true;
[Link]();
//[Link]();
}
static void function1()
{
for (int i = 0; i < 10; i++)
{
[Link]("function1: {0}", i);
[Link](1000);
}
[Link]("child Thread Ends !!");
}
}
}
// IsBackground
using System;
using [Link];
namespace IsBackground
{
class Program
{
static void Main (string [] args)
{
// Creating and initializing child threads
Thread thr1 = new Thread(function1);
[Link] = true;
[Link]();
for (int i = 0; i < 10; i++)
{
[Link]("Main Thread:" + i);
}
[Link]("Main Thread Ends !!");
// [Link]();
}
static void function1()
{
for (int i = 0; i < 50; i++)
{
[Link]("function1: {0}", i);
// [Link](1000);
}
[Link]("child Thread Ends !!");
}
}
}
// IsBackground
using System;
using [Link];
namespace IsBackground
{
class Program
{
static void Main (string [] args)
{
// Creating and initializing child threads
Thread thr1 = new Thread(function1);
[Link] = true;
[Link]();
for (int i = 0; i < 10; i++)
{
[Link]("Main Thread:" + i);
// [Link](1000);
}
[Link]("Main Thread Ends !!");
//[Link]();
}
static void function1()
{
for (int i = 0; i < 10; i++)
{
[Link]("function1: {0}", i);
[Link](1000);
}
[Link]("child Thread Ends !!");
}
}
}
// IsBackground
using System;
using [Link];
namespace IsBackground
{
class Program
{
static void Main (string [] args)
{
// Creating and initializing child threads
Thread thr1 = new Thread(function1);
[Link] = true;
[Link]();
for (int i = 0; i < 10; i++)
{
[Link]("Main Thread:" + i);
[Link](1000);
}
[Link]("Main Thread Ends !!");
//[Link]();
}
static void function1()
{
for (int i = 0; i < 10; i++)
{
[Link]("function1: {0}", i);
[Link](1000);
}
[Link]("child Thread Ends !!");
}
}
}
// IsAlive
using System;
using [Link];
namespace IsAlive
{
class Program
{
static void Main (string [] args)
{
Thread thr1 = new Thread(function1);
[Link] = "mythread";
[Link]([Link] + " IsAlive : " + [Link]);
[Link]();
[Link]([Link]+ " IsAlive : " + [Link]);
[Link]();
}
static void function1()
{
[Link]([Link] + " IsAlive : " +
[Link]);
[Link](1000);
}
}
}
// IsAlive
using System;
using [Link];
namespace IsAlive
{
class Program
{
static void Main (string [] args)
{
Thread thr1 = new Thread(function1);
Thread thr2 = new Thread(function2);
[Link] = "mythread1";
[Link] = "mythread2";
[Link]([Link] + " IsAlive : " + [Link]);
[Link]([Link] + " IsAlive : " + [Link]);
[Link]();
[Link]();
[Link]([Link] + " IsAlive : " + [Link]);
[Link]([Link] + " IsAlive : " + [Link]);
[Link]();
[Link]([Link] + " IsAlive : " + [Link]);
[Link]([Link] + " IsAlive : " + [Link]);
[Link]();
[Link]([Link] + " IsAlive : " + [Link]);
[Link]([Link] + " IsAlive : " + [Link]);
[Link]();
[Link]([Link] + " IsAlive : " + [Link]);
[Link]([Link] + " IsAlive : " + [Link]);
[Link]();
[Link]([Link] + " IsAlive : " + [Link]);
[Link]([Link] + " IsAlive : " + [Link]);
static void function1()
{
[Link](2000);
}
static void function2()
{
[Link](2000);
}
}
}
// ThreadState
using System;
using [Link];
namespace ThreadState
{
class Program
{
static void Main (string [] args)
{
Thread thr1 = new Thread(function1);
[Link] = "mythread";
[Link]([Link] + "State is: " + [Link]);
[Link]();
[Link]([Link] + "State is: " + [Link]);
[Link]();
[Link]([Link] + "State is: " + [Link]);
[Link]();
[Link]([Link] + "State is: " + [Link]);
[Link]();
[Link]([Link] + "State is: " + [Link]);
[Link]();
[Link]([Link] + "State is: " + [Link]);
[Link]();
static void function1()
{
[Link](2000);
}
}
}
//طرق استدعاء الدالة بوسائط بواسطة الثريدات
//الطريقة االولى
//( start ) بواسطة الدالة
using System;
using [Link];
namespace Threading
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread(function1);
[Link] = "mythread";
[Link](100);
[Link]();
}
static void function1(object x )
{
for (int i = 0; i <(int) x; i++)
{
[Link](i);
[Link](500);
}
}
}
}
//طرق استدعاء الدالة بوسائط بواسطة الثريدات
//الطريقة الثانية
//lamda صيغة ال
using System;
using [Link];
namespace lamda
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread(()=>function1(10));
[Link] = "mythread";
[Link]();
[Link]();
}
static void function1(int x )
{
for (int i = 0; i < x; i++)
{
[Link](i);
[Link](500);
}
}
}
}
//طرق استدعاء الدالة بوسائط بواسطة الثريدات
//الطريقة الثانية
//lamda صيغة ال
using System;
using [Link];
namespace lamda
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread (() =>function1(1, 10));
[Link] = "mythread";
[Link]();
[Link]();
}
static void function1(int x, int y)
{
for (int i = x; i < y; i++)
{
[Link](i);
[Link](500);
}
}
}
}
//طرق استدعاء الدالة بوسائط بواسطة الثريدات
//الطريقة الثالثة
//delegate
using System;
using [Link];
namespace Delegate
{
class Program
{
static void Main(string[] args)
{
Thread thr1 = new Thread(delegate () { function1(1, 20); });
[Link] = "mythread";
[Link]();
[Link]();
}
static void function1(int x, int y )
{
for (int i = x; i < y; i++)
{
[Link](i);
[Link](500);
}
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
//Deadlock مشكلة االقفال المميت
using System;
using [Link];
namespace Deadlock
{
class calc
{
public int n1, n2;
Random rd = new Random();
public void Div()
{
for (long i = 0; i < 100000; i++)
{
n1 = [Link](5, 10);
n2 = [Link](5, 10);
int result = n1 / n2;
n1 = 0;
n2 = 0;
}
}
}
class Program
{
static void Main(string[] args)
{
calc c = new calc();
Thread t1 = new Thread([Link]);
[Link]();
//Thread t2 = new Thread([Link]);
//[Link]();
[Link]();
// [Link]();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
//Deadlock حلول مشكلة االقفال المميت
// lock الطريقة االولى
using System;
using [Link];
namespace Lock
{
class calc
{
public int n1, n2;
Random rd = new Random ();
public void Div()
{
lock (this)
{
for (long i = 0; i < 100000; i++)
{
n1 = [Link](5, 10);
n2 = [Link](5, 10);
int result = n1 / n2;
n1 = 0;
n2 = 0;
}
}
}
}
class Program
{
static void Main(string[] args)
{
calc c = new calc();
Thread t1 = new Thread([Link]);
[Link]();
[Link]();
[Link]();
}
}
}
//Deadlock حلول مشكلة االقفال المميت
// Mutex الطريقة الثانية
using System;
using [Link];
namespace mutex
{
class Program
{
static Mutex mutex= new Mutex();
static void print()
{
try
{
[Link]();
[Link] = [Link];
[Link]([Link] + "Is Start");
[Link](1000);
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
finally
{
[Link]();
}
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}
//Deadlock حلول مشكلة االقفال المميت
// Mutex الطريقة الثانية
using System;
using [Link];
namespace mutex
{
class Program
{
static Mutex mutex= new Mutex();
static void print()
{
try
{
[Link]();
[Link] = [Link];
[Link]([Link] + "Is Start");
// [Link](1000);
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
finally
{
[Link]();
}
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}
//Deadlock حلول مشكلة االقفال المميت
// Mutex الطريقة الثانية
//Mutex مشكلة عدم تحرير االقفال للفصيل
using System;
using [Link];
namespace mutex
{
class Program
{
static Mutex mutex= new Mutex();
static void print()
{
[Link]();
[Link] = [Link];
[Link]([Link] + "Is Start");
// [Link](1000);
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}
//Deadlock حلول مشكلة االقفال المميت
// Semaphore الطريقة الثالثة
using System;
using [Link];
namespace semaphore
{
class Program
{
static Semaphore sem = new Semaphore(2, 5);
static void print()
{
try
{
[Link]();
[Link] = [Link];
[Link]([Link] + "Is Start");
[Link](2000);
[Link] = [Link];
[Link]([Link] + "Is Exit");
}
finally
{
[Link]();
}
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}
//Deadlock حلول مشكلة االقفال المميت
// Monitor الطريقة الرابعة
using System;
using [Link];
namespace monitor
{
class Program
{
static object ob1 = new object();
static void print()
{
[Link](ob1);
[Link] = [Link];
[Link]([Link] + "Is Start");
[Link] = [Link];
[Link]([Link] + "Is Exit");
[Link](2000);
[Link](ob1);
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}
//Deadlock حلول مشكلة االقفال المميت
// Monitor الطريقة الرابعة
//Monitor مشكلة عدم تحرير االقفال للفصيل
using System;
using [Link];
namespace monitor
{
class Program
{
static object ob1 = new object();
static void print()
{
[Link](ob1);
[Link] = [Link];
[Link]([Link] + "Is Start");
[Link] = [Link];
[Link]([Link] + "Is Exit");
[Link](2000);
// [Link](ob1);
}
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
Thread ob = new Thread(print);
[Link] = "Thread " + i + ":";
[Link]();
}
}
}
}