这是一个模拟多线程下载文件的程序代码,我默认最大支持开启3个线程,一个线程代表一个下载任务,同时启用三个任务这部分已经可以正常运行,就是在对其中的线程进行挂起和重启时一直没成功,.NET Framework是4.0的,所以Thread类里的Suspend和Resume被标识已过时,但是除了这个我不知道还有什么方式可以实现同样的效果,网上找的大多都是说用Monitor等同步类和Thread.sleep一起使用去实现,但是例子讲得不明不白,而且我是一个线程去控制一个下载任务,不存在多个线程访问同一个资源的情况,所以应该不用考虑同步的问题吧,以下是我模拟测试的代码,希望有朋友帮忙改一下,实现要求using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//-----
using System.Threading;
using System.Runtime.Remoting.Messaging;namespace TheadDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
            dtData.Columns.Add("id", typeof(Int32));
            dtData.Columns.Add("num", typeof(String));
        }        delegate void MyRun();
        delegate int MyReport();
        delegate void MyUpdate(int index, int num);
        DataTable dtData = new DataTable();
        int i = -1;
        Thread[] threads = new Thread[3];        private void btnRun_Click(object sender, EventArgs e)
        {
            i++;
            MyRun my = new MyRun(ThreadStartRun);
            threads[i] = new Thread(new ThreadStart(my));
            threads[i].Start();
        }        private void ThreadStartRun()
        {
            MyReport myReport = new MyReport(ReportInfo);
            myReport.BeginInvoke(Complete, myReport);        }        private int ReportInfo()
        {
            MyUpdate myUpdate = new MyUpdate(UpdateInfo);
            DataRow dr = dtData.NewRow();
            int count = dtData.Rows.Count;
            int index = count == 0 ? 0 : count;
            dr["id"] = index;
            dr["num"] = 0;
            dtData.Rows.Add(dr.ItemArray);
            for (int i = 0; i < 10000; )
            {
                IAsyncResult iar = this.BeginInvoke(myUpdate, index, i);
                if (!iar.IsCompleted)
                {
                    Thread.Sleep(100);
                }
                i += 10;
            }
            return index;
        }        private void UpdateInfo(int index,int num)
        {
            dtData.Select(string.Format("id={0}", index))[0]["num"] = num;
            this.gridControl1.DataSource = dtData;
        }        private void Complete(IAsyncResult iar)
        {
            MyReport my = iar.AsyncState as MyReport;
            int index = my.EndInvoke(iar);
            dtData.Select(string.Format("id={0}", index))[0]["num"] = "已完成";
            this.gridControl1.DataSource = dtData;
        }        private void btnSupend_Click(object sender, EventArgs e)
        {
            int index = Convert.ToInt32(this.gridView1.GetFocusedDataRow()["id"]);
            threads[index].Suspend();
        }        private void btnContinue_Click(object sender, EventArgs e)
        {
            int index = Convert.ToInt32(this.gridView1.GetFocusedDataRow()["id"]);
            threads[index].Resume();
        }    }
}