0505.Net基础班第十六天(多线程和Socket网络编程)

时间:2022-12-15 10:46:34

01复习

0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.IO;
 11 using System.Media;
 12 namespace _01复习
 13 {
 14     public partial class Form1 : Form
 15     {
 16         public Form1()
 17         {
 18             InitializeComponent();
 19         }
 20         //用来存储音乐文件的全路径
 21         List<string> listSongs = new List<string>();
 22         private void button1_Click(object sender, EventArgs e)
 23         {
 24             OpenFileDialog ofd = new OpenFileDialog();
 25             ofd.Title = "请选择音乐文件";
 26             ofd.InitialDirectory = @"F:\CloudMusic";
 27             ofd.Multiselect = true;
 28             ofd.Filter = "音乐文件|*.wav|所有文件|*.*";
 29             ofd.ShowDialog();
 30             //获得我们在文件夹中选择所有文件的全路径
 31             string[] path = ofd.FileNames;
 32             for (int i = 0; i < path.Length; i++)
 33             {
 34                 //将音乐文件的文件名加载到ListBox中
 35                 listBox1.Items.Add(Path.GetFileName(path[i]));
 36                 //将音乐文件的全路径存储到泛型集合中
 37                 listSongs.Add(path[i]);
 38             }
 39         }
 40 
 41 
 42         /// <summary>
 43         /// 实现双击播放
 44         /// </summary>
 45         /// <param name="sender"></param>
 46         /// <param name="e"></param>
 47         /// 
 48         SoundPlayer sp = new SoundPlayer();
 49         private void listBox1_DoubleClick(object sender, EventArgs e)
 50         {
 51            
 52             sp.SoundLocation=listSongs[listBox1.SelectedIndex];
 53             sp.Play();
 54         }
 55         /// <summary>
 56         /// 点击下一曲
 57         /// </summary>
 58         /// <param name="sender"></param>
 59         /// <param name="e"></param>
 60         private void button3_Click(object sender, EventArgs e)
 61         {
 62             //获得当前选中歌曲的索引
 63             int index = listBox1.SelectedIndex;
 64             index++;
 65             if (index == listBox1.Items.Count)
 66             {
 67                 index = 0;
 68             }
 69             //将改变后的索引重新的赋值给我当前选中项的索引
 70             listBox1.SelectedIndex = index;
 71             sp.SoundLocation = listSongs[index];
 72             sp.Play();
 73         }
 74 
 75 
 76         /// <summary>
 77         /// 点击上一曲
 78         /// </summary>
 79         /// <param name="sender"></param>
 80         /// <param name="e"></param>
 81         private void button2_Click(object sender, EventArgs e)
 82         {
 83             int index = listBox1.SelectedIndex;
 84             index--;
 85             if (index < 0)
 86             {
 87                 index = listBox1.Items.Count-1;
 88             }
 89             //将重新改变后的索引重新的赋值给当前选中项
 90             listBox1.SelectedIndex = index;
 91             sp.SoundLocation = listSongs[index];
 92             sp.Play();
 93         }
 94 
 95         private void Form1_Load(object sender, EventArgs e)
 96         {
 97 
 98         }
 99     }
100 }
View Code
0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
  1 namespace _01复习
  2 {
  3     partial class Form1
  4     {
  5         /// <summary>
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region Windows 窗体设计器生成的代码
 24 
 25         /// <summary>
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.button1 = new System.Windows.Forms.Button();
 32             this.listBox1 = new System.Windows.Forms.ListBox();
 33             this.button2 = new System.Windows.Forms.Button();
 34             this.button3 = new System.Windows.Forms.Button();
 35             this.SuspendLayout();
 36             // 
 37             // button1
 38             // 
 39             this.button1.Location = new System.Drawing.Point(22, 29);
 40             this.button1.Name = "button1";
 41             this.button1.Size = new System.Drawing.Size(75, 23);
 42             this.button1.TabIndex = 0;
 43             this.button1.Text = "打开";
 44             this.button1.UseVisualStyleBackColor = true;
 45             this.button1.Click += new System.EventHandler(this.button1_Click);
 46             // 
 47             // listBox1
 48             // 
 49             this.listBox1.FormattingEnabled = true;
 50             this.listBox1.ItemHeight = 12;
 51             this.listBox1.Location = new System.Drawing.Point(22, 79);
 52             this.listBox1.Name = "listBox1";
 53             this.listBox1.Size = new System.Drawing.Size(216, 328);
 54             this.listBox1.TabIndex = 1;
 55             this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
 56             // 
 57             // button2
 58             // 
 59             this.button2.Location = new System.Drawing.Point(276, 79);
 60             this.button2.Name = "button2";
 61             this.button2.Size = new System.Drawing.Size(75, 23);
 62             this.button2.TabIndex = 2;
 63             this.button2.Text = "上一曲";
 64             this.button2.UseVisualStyleBackColor = true;
 65             this.button2.Click += new System.EventHandler(this.button2_Click);
 66             // 
 67             // button3
 68             // 
 69             this.button3.Location = new System.Drawing.Point(276, 139);
 70             this.button3.Name = "button3";
 71             this.button3.Size = new System.Drawing.Size(75, 23);
 72             this.button3.TabIndex = 3;
 73             this.button3.Text = "下一曲";
 74             this.button3.UseVisualStyleBackColor = true;
 75             this.button3.Click += new System.EventHandler(this.button3_Click);
 76             // 
 77             // Form1
 78             // 
 79             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
 80             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 81             this.ClientSize = new System.Drawing.Size(670, 417);
 82             this.Controls.Add(this.button3);
 83             this.Controls.Add(this.button2);
 84             this.Controls.Add(this.listBox1);
 85             this.Controls.Add(this.button1);
 86             this.Name = "Form1";
 87             this.Text = "Form1";
 88             this.Load += new System.EventHandler(this.Form1_Load);
 89             this.ResumeLayout(false);
 90 
 91         }
 92 
 93         #endregion
 94 
 95         private System.Windows.Forms.Button button1;
 96         private System.Windows.Forms.ListBox listBox1;
 97         private System.Windows.Forms.Button button2;
 98         private System.Windows.Forms.Button button3;
 99     }
100 }
View Code
0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using System.Windows.Forms;
 6 
 7 namespace _01复习
 8 {
 9     static class Program
10     {
11         /// <summary>
12         /// 应用程序的主入口点。
13         /// </summary>
14         [STAThread]
15         static void Main()
16         {
17             Application.EnableVisualStyles();
18             Application.SetCompatibleTextRenderingDefault(false);
19             Application.Run(new Form1());
20         }
21     }
22 }
View Code

02线程和进程的复习

0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading;
 7 using System.Threading.Tasks;
 8 
 9 namespace _02线程和进程的复习
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15 
16             //通过进程去打开应用程序
17           //  Process.getprocesses
18             //Process.Start("notepad");
19             //Process.Start("iexplore", "http://www.baidu.com");
20 
21 
22 
23             //通过进程去打开指定的文件
24             //ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\SpringRain\Desktop\1、播放音乐下一曲.wmv");
25             //Process p = new Process();
26             //p.StartInfo = psi;
27             //p.Start();
28             //Console.ReadKey();
29 
30 
31             //进程和线程的关系? 一个进程包含多个线程
32 
33             //前台  后台
34         }
35     }
36 }
View Code

03、线程执行带参数的方法

0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _03_线程执行带参数的方法
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14         }
15 
16         
17     }
18 }
View Code

04、线程执行带参数的方法

0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace _04_线程执行带参数的方法
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button1_Click(object sender, EventArgs e)
22         {
23             Thread th = new Thread(Test);
24             th.IsBackground = true;
25             th.Start("123");
26             //Test();
27         }
28 
29 
30         private void Test(object s)
31         {
32             string ss = (string)s;
33             for (int i = 0; i < 10000; i++)
34             {
35                 Console.WriteLine(i);
36             }
37         }
38     }
39 }
View Code
0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 namespace _04_线程执行带参数的方法
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// 必需的设计器变量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// 清理所有正在使用的资源。
12         /// </summary>
13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows 窗体设计器生成的代码
24 
25         /// <summary>
26         /// 设计器支持所需的方法 - 不要
27         /// 使用代码编辑器修改此方法的内容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.button1 = new System.Windows.Forms.Button();
32             this.SuspendLayout();
33             // 
34             // button1
35             // 
36             this.button1.Location = new System.Drawing.Point(226, 28);
37             this.button1.Name = "button1";
38             this.button1.Size = new System.Drawing.Size(75, 23);
39             this.button1.TabIndex = 0;
40             this.button1.Text = "button1";
41             this.button1.UseVisualStyleBackColor = true;
42             this.button1.Click += new System.EventHandler(this.button1_Click);
43             // 
44             // Form1
45             // 
46             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
47             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
48             this.ClientSize = new System.Drawing.Size(599, 417);
49             this.Controls.Add(this.button1);
50             this.Name = "Form1";
51             this.Text = "Form1";
52             this.ResumeLayout(false);
53 
54         }
55 
56         #endregion
57 
58         private System.Windows.Forms.Button button1;
59     }
60 }
View Code
0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using System.Windows.Forms;
 6 
 7 namespace _04_线程执行带参数的方法
 8 {
 9     static class Program
10     {
11         /// <summary>
12         /// 应用程序的主入口点。
13         /// </summary>
14         [STAThread]
15         static void Main()
16         {
17             Application.EnableVisualStyles();
18             Application.SetCompatibleTextRenderingDefault(false);
19             Application.Run(new Form1());
20         }
21     }
22 }
View Code

05、摇奖机应用程序

0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace _05_摇奖机应用程序
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20         bool b = false;
21         private void button1_Click(object sender, EventArgs e)
22         {
23             if (b == false)
24             {
25                 b = true;
26                 button1.Text = "停止";
27                 Thread th = new Thread(PlayGame);
28                 th.IsBackground = true;
29                 th.Name = "新线程";
30                // th.
31                 th.Start();
32             }
33             else//b==true
34             {
35                 b = false;
36                 button1.Text = "开始";
37             }
38             //PlayGame();
39         }
40         private void PlayGame()
41         {
42             Random r = new Random();
43             while (b)
44             {
45                 label1.Text = r.Next(0, 10).ToString();
46                 label2.Text = r.Next(0, 10).ToString();
47                 label3.Text = r.Next(0, 10).ToString();
48             }
49         }
50 
51         private void Form1_Load(object sender, EventArgs e)
52         {
53             Control.CheckForIllegalCrossThreadCalls = false;
54         }
55     }
56 }
View Code
0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 namespace _05_摇奖机应用程序
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// 必需的设计器变量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// 清理所有正在使用的资源。
12         /// </summary>
13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows 窗体设计器生成的代码
24 
25         /// <summary>
26         /// 设计器支持所需的方法 - 不要
27         /// 使用代码编辑器修改此方法的内容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.label1 = new System.Windows.Forms.Label();
32             this.label2 = new System.Windows.Forms.Label();
33             this.label3 = new System.Windows.Forms.Label();
34             this.button1 = new System.Windows.Forms.Button();
35             this.SuspendLayout();
36             // 
37             // label1
38             // 
39             this.label1.AutoSize = true;
40             this.label1.Location = new System.Drawing.Point(111, 137);
41             this.label1.Name = "label1";
42             this.label1.Size = new System.Drawing.Size(41, 12);
43             this.label1.TabIndex = 0;
44             this.label1.Text = "label1";
45             // 
46             // label2
47             // 
48             this.label2.AutoSize = true;
49             this.label2.Location = new System.Drawing.Point(246, 137);
50             this.label2.Name = "label2";
51             this.label2.Size = new System.Drawing.Size(41, 12);
52             this.label2.TabIndex = 1;
53             this.label2.Text = "label2";
54             // 
55             // label3
56             // 
57             this.label3.AutoSize = true;
58             this.label3.Location = new System.Drawing.Point(396, 136);
59             this.label3.Name = "label3";
60             this.label3.Size = new System.Drawing.Size(41, 12);
61             this.label3.TabIndex = 2;
62             this.label3.Text = "label3";
63             // 
64             // button1
65             // 
66             this.button1.Location = new System.Drawing.Point(398, 297);
67             this.button1.Name = "button1";
68             this.button1.Size = new System.Drawing.Size(75, 23);
69             this.button1.TabIndex = 3;
70             this.button1.Text = "开始";
71             this.button1.UseVisualStyleBackColor = true;
72             this.button1.Click += new System.EventHandler(this.button1_Click);
73             // 
74             // Form1
75             // 
76             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
77             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
78             this.ClientSize = new System.Drawing.Size(626, 456);
79             this.Controls.Add(this.button1);
80             this.Controls.Add(this.label3);
81             this.Controls.Add(this.label2);
82             this.Controls.Add(this.label1);
83             this.Name = "Form1";
84             this.Text = "Form1";
85             this.Load += new System.EventHandler(this.Form1_Load);
86             this.ResumeLayout(false);
87             this.PerformLayout();
88 
89         }
90 
91         #endregion
92 
93         private System.Windows.Forms.Label label1;
94         private System.Windows.Forms.Label label2;
95         private System.Windows.Forms.Label label3;
96         private System.Windows.Forms.Button button1;
97     }
98 }
View Code
0505.Net基础班第十六天(多线程和Socket网络编程)0505.Net基础班第十六天(多线程和Socket网络编程)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using System.Windows.Forms;
 6 
 7 namespace _05_摇奖机应用程序
 8 {
 9     static class Program
10     {
11         /// <summary>
12         /// 应用程序的主入口点。
13         /// </summary>
14         [STAThread]
15         static void Main()
16         {
17             Application.EnableVisualStyles();
18             Application.SetCompatibleTextRenderingDefault(false);
19             Application.Run(new Form1());
20         }
21     }
22 }
View Code