Multithreading using c#
Program.cs==>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace multithreading
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("the process has started");
int[] arr = { 1, 2, 3, 4 };
counting obj = new counting();
counting obj1 = new counting();
Thread mythread =new Thread(new ThreadStart(obj.run));
Thread mythread1 = new Thread(new ThreadStart(obj1.run));
mythread.Start();
mythread1.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace multithreading
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("the process has started");
int[] arr = { 1, 2, 3, 4 };
counting obj = new counting();
counting obj1 = new counting();
Thread mythread =new Thread(new ThreadStart(obj.run));
Thread mythread1 = new Thread(new ThreadStart(obj1.run));
mythread.Start();
mythread1.Start();
}
}
}
//(create a new class called counting)
counting.cs==>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace multithreading
{
class counting
{
string vthreadname;
int a;
public void Computing(string name)
{
vthreadname=name;
}
public void run()
{
System.Console.WriteLine("thread named "+vthreadname+"started");
while(a<0)
{
System.Console.WriteLine("the value of a in thread:[ "+vthreadname+"]"+a.ToString());
a=a+1;
Thread.Sleep(1000);
}
System.Console.WriteLine("ended");
Thread.Sleep(100000);
}
}
}
0 comments:
Post a Comment