线程

创建线程

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbers);

t.Start();

 

PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


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

{


Console.WriteLine(i);

}

}

}

}

暂停线程

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();

 

PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


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

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


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

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i);

}

}

}

}

线程等待

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();

t.Join();

 

PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


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

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


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

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);

}

}

}

}

终止线程

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();


Thread.Sleep(TimeSpan.FromSeconds(6));

t.Abort();

 


//PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


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

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


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

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);

}

}

}

}

检测线程状态

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();


Thread.Sleep(TimeSpan.FromSeconds(6));


Console.WriteLine(t.ThreadState.ToString());

t.Abort();

 


//PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


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

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


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

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);


Console.WriteLine(Thread.CurrentThread.ThreadState);

}

}

}

}

线程优先级

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t1 = new
Thread(PrintNumbers1);


Thread t2 = new
Thread(PrintNumbers2);

t1.Priority = ThreadPriority.Highest;

t2.Priority = ThreadPriority.Lowest;

t2.Start();

t1.Start();

 

 


//PrintNumbers();

}

 


static
void PrintNumbers1()

{


Console.WriteLine(“Starting…1”);


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

{


Console.WriteLine(i.ToString()+“+1”);

}

}


static
void PrintNumbers2()

{


Console.WriteLine(“Starting…2”);


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

{


Console.WriteLine(i.ToString() + “+2”);

}

}


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


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

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);


Console.WriteLine(Thread.CurrentThread.ThreadState);

}

}

}

}

前后台程序

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t1 = new
Thread(PrintNumbers1);


Thread t2 = new
Thread(PrintNumbers2);

t2.IsBackground = true;

t2.Start();


Thread.Sleep(TimeSpan.FromSeconds(10));

t1.Start();

 

 


//PrintNumbers();

}

 


static
void PrintNumbers1()

{


Console.WriteLine(“Starting…1”);


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

{


Console.WriteLine(i.ToString()+“+1”);

}

}


static
void PrintNumbers2()

{


Console.WriteLine(“Starting…2”);


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

{


Console.WriteLine(i.ToString() + “+2”);

}

}


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


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

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);


Console.WriteLine(Thread.CurrentThread.ThreadState);

}

}

}

}

进程

关于进程的主要三个类

Process

ProcessModule

ProcessThread

直接启动程序

Process.Start(@”C:\Program Files\Modbus Tools\Modbus Slave\mbslave.exe”);

Process.Start(“Iexplore.exe”,“www.baidu.com”);

启动程序前,设置参数


ProcessStartInfo psi = new
ProcessStartInfo(“IExplore.exe”);

psi.WindowStyle = ProcessWindowStyle.Maximized;

psi.Arguments = “www.dianqirensheng.com”;


Process.Start(psi);

 

 

LabVIEW与S71200 以太网TCP通讯

首先设PLC的IP地址

启用系统和时钟存储器

如果数据量很大,需要修改一下通信负荷

建立发送数据存储区

建立接收数据存储区

编写程序

调用发送指令

指令的属性设置

其中连接数据选择新建,连接数据自动生成。

接收指令

接收指令属性设置

注意连接数据要与发送指令的连接数据保持一致。

下载程序。

LabVIEW程序编写

简单的事件模式

程序的退出

数据接收

因为接收到的是字节,需要把字节转换成数组

数据的发送

需要把发送的数据转换成字节

程序界面

运行结果

 

链式队列

新建节点node.cls

Option Explicit

 

Public data As Integer

Public nextdata As node

建队列类queue.cls

Option Explicit

 

 

Public head As node

Public length As Integer

 

‘初始化类

Private Sub Class_Initialize()

Set head = New node

Me.length = 0

End Sub

 

‘进队

Public Sub enQueue(e As Integer)

Dim s As node

Set s = New node

s.data = e

Set s.nextdata = Nothing

If Me.length = 0 Then

Set head.nextdata = s

Me.length = Me.length + 1

Exit Sub

End If

Dim i As Integer

Dim p As node

Set p = head

For i = 1 To Me.length

Set p = p.nextdata

Next i

Me.length = Me.length + 1

End Sub

 

 

‘出队

Public Function deQueue() As Integer

If Me.length = 0 Then

Exit Function

End If

Dim i As Integer

Dim p As node

Set p = head

For i = 1 To Me.length

Set p = p.nextdata

Next i

Me.length = Me.length – 1

deQueue = p.data

End Function

演示

Option Explicit

 

Dim a As New queue

 

Private Sub CommandButton1_Click()

a.enQueue (123)

End Sub

 

Private Sub CommandButton2_Click()

MsgBox a.deQueue

End Sub

2021年3月18日

 

 

 

 

顺序队列

新建模块

Option Explicit

 

Public Const MaxSize As Integer = 10

 

Public Type SeqQueue

data(0 To MaxSize – 1) As Integer

length As Integer

End Type

‘队列初始化

Public Sub initQueue(q As SeqQueue)

q.length = 0

End Sub

 

‘进队

Public Sub enQueue(q As SeqQueue, e As Integer)

q.data(q.length) = e

q.length = q.length + 1

End Sub

 

‘出队

Public Function deQueue(q As SeqQueue) As Integer

Dim temp As Integer

temp = q.data(0)

Dim i As Integer

For i = 0 To q.length – 1

q.data(i) = q.data(i + 1)

Next i

q.length = q.length – 1

deQueue = temp

End Function

演示程序

Option Explicit

 

Dim q As SeqQueue

 

Private Sub CommandButton1_Click()

enQueue q, 123

End Sub

 

Private Sub CommandButton2_Click()

MsgBox deQueue(q)

End Sub

 

Private Sub UserForm_Click()

initQueue q

End Sub

 

2021年3月18日