.


:




:

































 

 

 

 


() 4

́ ( . thread) , . , , .

: . , . , .

 

(UML)

 

.Net .

System.Threading - . .

Thread, .

Thread myThread = new Thread(func);

Start() -

Thread.Sleep(n) -

.

using System;

using System.Threading;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Thread myThread = new Thread(func);

myThread.Start(); //

for (int i = 0; i < 10; i++)

{

Console.WriteLine(" 1 " + i);

Thread.Sleep(1);

}

Console.Read(); //

}

 

static void func()

{

for (int i = 0; i < 10; i++)

{

Console.WriteLine(" 2 " + i.ToString());

Thread.Sleep(1);

}

:
1 0
2 0
1 1
1 2
1 3
2 1
<> ..

: .

. Main() .

IsAlive, true . Join(), .

. IsBackground . , . myThread.IsBackground = false;

, . - , , .

using System;

using System.Threading;

 

namespace ConsoleApplication1

{

class Program

{

class myThread

{

Thread thread;

public myThread(string name, int num)

//

{

thread = new Thread(this.func);

thread.Name = name;

thread.Start(num);//

}

 

void func(object num)// ,

{

for (int i = 0;i < (int)num;i++)

{

Console.WriteLine(Thread.CurrentThread.Name + " " + i);

Thread.Sleep(1);

}

Console.WriteLine(Thread.CurrentThread.Name + " ");

}

 

 

}

static void Main(string[] args)

{

myThread t1 = new myThread("Thread 1", 6);

myThread t2 = new myThread("Thread 2", 3);

myThread t3 = new myThread("Thread 3", 2);

Console.Read();

}

}

}

Join()

static int[] array;static void Main(string[] args){ Thread filler = new Thread(FillArray); filler.Start(); // , . for (int i = 1; i < 4; i++) { Console.WriteLine(" {0}", i); Thread.Sleep(1000); } // , , , . // , . filler.Join(); Console.WriteLine(" : {0}", string.Join(" ", array)); Console.ReadKey();}// static void FillArray(){ Random rand = new Random(); array = new int[20]; for (int i = 0; i < array.Length; i++) { array[i] = rand.Next(100); Thread.Sleep(200); }}

 

Invoke - . , , .

- , . Invoke , (-, , ).

, , . Invoke, , .

public Form1(){ InitializeComponent(); // , // var th = new Thread(DoSomething); th.Start();} void DoSomething(){ for (int i = 0; i <= 100; i++) { if (this.InvokeRequired) // DoSomething , // {// UpdateProgress //, this.Invoke(new Action(UpdateProgress)); } else // DoSomething , // UpdateProgress(); Thread.Sleep(50); }} void UpdateProgress(){ // 1 progressBar1.PerformStep();}

 



<== | ==>
 |
:


: 2016-11-20; !; : 238 |


:

:

, .
==> ...

1701 - | 1523 -


© 2015-2024 lektsii.org - -

: 0.01 .