5

我有一个 Stream 对象,我正在使用 BeginRead 开始(显然)读入缓冲区;读取完成后调用 AsyncCallback 函数。在这个函数中,我可以检查用户是否想要获得下一个“块”并再次启动 BeginRead 过程。

我遇到的问题是用户可能会在流仍在读取时选择取消(所以在调用 AsyncCallback 函数之前),那么如何取消流的读取?

只是为了进一步解释这个问题 - 如果我将 BackgroundWorker 与 Streams Read 方法或异步 BeginRead 方法一起使用,我似乎会得到相同的结果。在我可以检查流是否应该停止读取之前,用户可能会等待任何时间长度的 Read/BeginRead 方法完成。

编辑:下面的代码应该可以完成这项工作,我距离 C# 中任何体面的东西还有一百万英里,所以它很可能有几个错误,因为我怀疑它是否完美,尽管它确实展示了解决方案。

简而言之,CWorkManager 管理一定数量的线程(保存在 CWorkerDetail 类中)。每个 CWorkerDetail 都有一个状态,可以是 EWaiting 表示可以启动工作程序,EReading 表示工作程序正在从源读取,在此期间可以立即停止工作程序,EWriting 将读取的数据保存到磁盘 -这不能立即停止,并且必须在线程停止之前完成此过程。最后是 EAborting,如果工人应该尽快被中止,则由经理设置;现在,只有当工作人员处于无法中断的事情(例如写入磁盘)中间时才设置。

现在,实际上并没有进行任何读取或写入,因为这只会使主要解决方案复杂化(这基本上只是 StopWorker 函数检查 CWorker 的标志以查看它是否可以立即中止);因此,我们只是让线程休眠。

GUI 端相当简单,只有一个列表框(显示每个工作人员的状态)和一个停止和启动按钮。所有代码都在下面,希望这对某人有所帮助,但正如我所说,我对 C# 并不精通,所以请注意错误等...

CWorkManager.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;


namespace ThreadApplication {


    //A worker that spawns a number of threads (managed internally) that does nothing useful at all.
    public class CWorkManager {


        //The status of the worker.
        public enum EWorkerStatus {
            EWaiting,
            EReading,
            EWriting,
            EAborting,
        }


        //Holds all data relevant to the worker.
        private class CWorkerDetails {

            //Simple variables.
            private readonly Object _Lock=new Object();
            private Thread gThread;
            private EWorkerStatus gStatus;
            private CWorkManager gParentInstance;
            private int gIndex;

            //Simple constructor.
            public CWorkerDetails(int aIndex, CWorkManager aParentInstance, Thread aThread, EWorkerStatus aStatus) {
                gIndex=aIndex;
                gParentInstance=aParentInstance;
                gThread=aThread;
                gStatus=aStatus;
            }

            //Simple get set methods.
            public Thread GetThread() { lock(_Lock) { return gThread; } }
            public EWorkerStatus GetStatus() { lock(_Lock) { return gStatus; } }

            //Sets the status and automatically updates the GUI.
            public void SetStatus(EWorkerStatus aStatus) {
                lock(_Lock) {
                    gStatus=aStatus;
                    Form1.gInstance.Invoke(new UpdateGUIDelegate(gParentInstance.UpdateGUI), new object[] { gIndex, GetStatus() });
                }
            }

        }


        //Worker variable.
        private List<CWorkerDetails> gWorkers;


        //Simple constructor.
        public CWorkManager(int aWorkerCount){
            gWorkers=new List<CWorkerDetails>();
            for(int tIndex=0; tIndex<aWorkerCount; tIndex++)
                gWorkers.Add(null);
        }


        //Creates and starts the worker.
        public void StartWorker(int aWorkerIndex) {

            //Create a new worker if there is none or if it is waiting to start.
            if(gWorkers.ElementAt(aWorkerIndex)==null||gWorkers.ElementAt(aWorkerIndex).GetStatus()==EWorkerStatus.EWaiting)
                gWorkers[aWorkerIndex]=new CWorkerDetails(aWorkerIndex, this, new Thread(new ParameterizedThreadStart(WorkerMethod)), EWorkerStatus.EWaiting);

            //If the worker is waiting to start, then start.
            if(gWorkers.ElementAt(aWorkerIndex).GetStatus()==EWorkerStatus.EWaiting)
                gWorkers.ElementAt(aWorkerIndex).GetThread().Start(gWorkers.ElementAt(aWorkerIndex));
        }


        //Stops the worker.
        public void StopWorker(int aWorkerIndex) {

            //Do nothing if the worker is null.
            if(gWorkers.ElementAt(aWorkerIndex)==null)
                return;

            //Do nothing if the worker is waiting.
            if(gWorkers.ElementAt(aWorkerIndex).GetStatus()==EWorkerStatus.EWaiting)
                return;

            //If the worker is reading we can abort instantly.
            if(gWorkers[aWorkerIndex].GetStatus()==EWorkerStatus.EReading) {
                gWorkers[aWorkerIndex].GetThread().Abort();
                gWorkers[aWorkerIndex].SetStatus(EWorkerStatus.EWaiting);
                return;
            }

            //Since the worker is not reading or waiting, we have to request the 
            //worker to abort by itself.
            gWorkers[aWorkerIndex].SetStatus(EWorkerStatus.EAborting);

        }


        //Updates the GUI.
        private delegate void UpdateGUIDelegate(int aIndex, EWorkerStatus aStatus);
        private void UpdateGUI(int aIndex, EWorkerStatus aStatus) {
            Form1.gInstance.SetThreadStatus(aIndex, aStatus);
        }


        //This method is where all the real work happens.
        private void WorkerMethod(Object aWorker) {

            //Fetch worker.
            CWorkerDetails mWorker=(CWorkerDetails)aWorker;

            //Loop forever, the thread will exit itself when required.
            while(true) {

                //Is the worker status aborting - if so we stop here.
                if(mWorker.GetStatus()==EWorkerStatus.EAborting) {
                    mWorker.SetStatus(EWorkerStatus.EWaiting);
                    return;
                }

                //This would normally be reading from a stream which would cause the thread
                //to block, simulate this by just sleeping the thread.
                mWorker.SetStatus(EWorkerStatus.EReading);
                Thread.Sleep(3000);

                //Is the worker status aborting - if so we stop here.
                if(mWorker.GetStatus()==EWorkerStatus.EAborting) {
                    mWorker.SetStatus(EWorkerStatus.EWaiting);
                    return;
                }

                //All data has been read, set status to writing and again simulate by
                //sleeping the thread.
                mWorker.SetStatus(EWorkerStatus.EWriting);
                Thread.Sleep(3000);

            }

        }

    }


}

Form1.cs:


包含:

  • 一个列表框 (ListBox_WorkerStatus)
  • 一个按钮(Button_Start)
  • 一个按钮 (Button_Stop)

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;


namespace ThreadApplication {


    public partial class Form1:Form {


        public static Form1 gInstance;
        private CWorkManager gManager;


        public Form1() {
            InitializeComponent();
            Button_Start.Click+=new EventHandler(Button_Start_Click);
            Button_Stop.Click+=new EventHandler(Button_Stop_Click);
            gInstance=this;
            for(int tIndex=0; tIndex<5; tIndex++)
                ListBox_WorkerStatus.Items.Add("Created");
            gManager=new CWorkManager(ListBox_WorkerStatus.Items.Count);
        }


        public void SetThreadStatus(int aIndex, CWorkManager.EWorkerStatus aStatus) {
            ListBox_WorkerStatus.Items[aIndex]=aStatus.ToString();
        }


        private void Button_Start_Click(object sender, EventArgs e) {
            if(ListBox_WorkerStatus.SelectedIndex>=0) {
                gManager.StartWorker(ListBox_WorkerStatus.SelectedIndex);
            }
        }


        private void Button_Stop_Click(object sender, EventArgs e) {
            if(ListBox_WorkerStatus.SelectedIndex>=0) {
                gManager.StopWorker(ListBox_WorkerStatus.SelectedIndex);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
            for(int tIndex=0; tIndex<ListBox_WorkerStatus.Items.Count; tIndex++) {
                gManager.StopWorker(tIndex);
            }
        }


    }


}

4

2 回答 2

1

请看取消开始阅读

于 2011-06-12T13:31:33.050 回答
0

使用 BackgroundWorker

BackgroundWorker backgroundWorker1= new backgroundWorker()

private void InitializeBackgroundWorker()
{
        backgroundWorker1.DoWork += 
            new DoWorkEventHandler(backgroundWorker1_DoWork);

        backgroundWorker1.WorkerSupportsCancellation = true;
}



private void backgroundWorker1_DoWork(object sender, 
        DoWorkEventArgs e)
{   
        BackgroundWorker worker = sender as BackgroundWorker;

        e.Result = YourWorkToDo();
}

public void Start()
{
    backgroundWorker1.RunWorkerAsync()
}

public voic Cancel()
{
    backgroundWorker1.CancelAsync();
{

如果您需要更多帮助,请发表评论

于 2011-06-12T12:11:54.623 回答