1.进程类
Process[ ] pro = Process. GetProcesses ( ) ;
Process. Start ( "calc" ) ;
ProcessStartInfo pro = new ProcessStartInfo ( Path)
Process p = new Process ( ) ;
p. StartInfo = pro;
2.线程类
Thread th = new Thread ( Test) ;
th. IsBackground = true ;
th. Start ( ) ;
Control. CheckForIllegalCrossThreadCalls = false ;
3.音乐播放器
using System;
using System. Collections. Generic;
using System. ComponentModel;
using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Data;
using System. Drawing;
using System. Linq;
using System. Media;
using System. Runtime. InteropServices;
using System. Text;
using System. Threading. Tasks;
using System. Windows. Forms;
namespace _16天
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
List< string > pathlist = new List < string > ( ) ;
string path;
private void button1_Click ( object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog ( ) ;
openFile. Multiselect = true ;
openFile. Title = "音乐播放器" ;
openFile. Filter = "音乐| *.mp3|所有文件|*.wav;*.mp4" ;
openFile. ShowDialog ( ) ;
string [ ] path = openFile. FileNames;
for ( int i = 0 ; i < path. Length; i++ )
{
listBox1. Items. Add ( System. IO. Path. GetFileName ( path[ i] ) ) ;
pathlist. Add ( path[ i] ) ;
}
}
private void button3_Click ( object sender, EventArgs e)
{
int Index = listBox1. SelectedIndex;
Index++ ;
if ( Index == listBox1. Items. Count)
{
Index = 0 ;
}
sp. SoundLocation = pathlist[ Index ] ;
sp. Play ( ) ;
listBox1. SelectedIndex = Index;
}
SoundPlayer sp = new SoundPlayer ( ) ;
[ DllImport ( "winmm.dll" , EntryPoint = "mciSendString" , CharSet = CharSet. Auto) ]
public static extern int mciSendString ( string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback) ;
private void listBox1_DoubleClick ( object sender, EventArgs e)
{
sp. SoundLocation = pathlist[ listBox1. SelectedIndex] ;
sp. Play ( ) ;
}
private void button2_Click ( object sender, EventArgs e)
{
int Index = listBox1. SelectedIndex;
Index-- ;
if ( Index == - 1 )
{
Index = listBox1. Items. Count- 1 ;
}
sp. SoundLocation = pathlist[ Index ] ;
sp. Play ( ) ;
listBox1. SelectedIndex = Index;
}
private void Form1_Load ( object sender, EventArgs e)
{
Control. CheckForIllegalCrossThreadCalls = false ;
;
}
}
}
4.摇奖号
using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Data;
using System. Drawing;
using System. Linq;
using System. Text;
using System. Threading;
using System. Threading. Tasks;
using System. Windows. Forms;
namespace 摇奖机
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
bool flag = false ;
private void button1_Click ( object sender, EventArgs e)
{
if ( flag == false )
{
button1. Text = "暂停" ;
flag = true ;
Thread th = new Thread ( PlayGame) ;
th. Start ( ) ;
}
else
{
flag = false ;
button1. Text = "开始" ;
}
}
Random r = new Random ( ) ;
private void PlayGame ( )
{
while ( flag == true )
{
label1. Text = r. Next ( 0 , 10 ) . ToString ( ) ;
label2. Text = r. Next ( 0 , 10 ) . ToString ( ) ;
label3. Text = r. Next ( 0 , 10 ) . ToString ( ) ;
}
}
private void Form1_Load ( object sender, EventArgs e)
{
Control. CheckForIllegalCrossThreadCalls = false ;
}
}
}
5.带参数的线程方法
using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Data;
using System. Drawing;
using System. Linq;
using System. Text;
using System. Threading;
using System. Threading. Tasks;
using System. Windows. Forms;
namespace 线程执行带参数的方法
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
private void button1_Click ( object sender, EventArgs e)
{
Thread th = new Thread ( Test) ;
th. Start ( "123" ) ;
}
public void Test ( Object s)
{
for ( int i = 0 ; i < 10000 ; i++ )
{
Console. WriteLine ( i. ToString ( ) ) ;
}
}
}
}