form1.cs

来自「c#的小例子:用到的知识点: * c#编程基础 * IO流 * 读取HTT」· CS 代码 · 共 76 行

CS
76
字号
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 + =
减小字号Ctrl + -
显示快捷键?