⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 c#的小例子:用到的知识点: * c#编程基础 * IO流 * 读取HTTP响应 * 多线程异步GUI的使用 * 使用“委派”实现“监听-观察者”模式
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace M3UDownloader
{
    public partial class MainForm : Form
    {
        String m3uFileName;
        m3uHandler m3uHandler1;
        Thread t;

        // The progress of the download in percentage
        private static int PercentProgress;

        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            m3uFileName = (openFileDialog1.ShowDialog() == DialogResult.OK) ? openFileDialog1.FileName : null;
            
            if (m3uFileName != null)
            {
                m3uHandler1 = new m3uHandler(m3uFileName);
                m3uHandler1.parse();

                m3uHandler1.CompleteEvent+= new m3uHandler.DownloadHandler(this.UpdateProgress);

                t = new Thread(m3uHandler1.download);
                t.Start();
            }
        }

        delegate void SetProcess(String name, int TotalSize, int Downloaded);
        private void UpdateProgress(String name, int TotalSize, int Downloaded)
        {
            // Calculate the download progress in percentages
            PercentProgress = Convert.ToInt32((Downloaded * 100) / TotalSize);
            // Make progress on the progress bar

            if (DownloadProcess.InvokeRequired)
            {
                Invoke(new SetProcess(UpdateProgress), new object[] { name, TotalSize, Downloaded });
            }
            else
            {
                DownloadProcess.Value = PercentProgress;
                
                //Display the current progress on the form
                PrsLabel.Text = name+" 已下载: " + Downloaded + " 总共: " + TotalSize + " (" + PercentProgress + "%)";
            }
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            if (t.IsAlive)
                t.Abort();
            this.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -