Finalizers and Dispose

后端 未结 6 1547
失恋的感觉
失恋的感觉 2021-01-01 06:04

I\'ve got a class named BackgroundWorker that has a thread constantly running. To turn this thread off, an instance variable named stop to needs to

6条回答
  •  醉梦人生
    2021-01-01 06:47

    Out of interest, any reason this couldn't use the regular BackgroundWorker, which has full support for cancellation?

    Re the lock - a volatile bool field might be less troublesome.

    However, in this case your finalizer isn't doing anything interesting, especially given the "if(disposing)" - i.e. it only runs the interesting code during Dispose(). Personally I'd be tempted to stick with just IDisposable, and not provide a finalizer: you should be cleaning it up with Dispose().

提交回复
热议问题