Featured post
c# - BackgroundWorker and instance variables -
one thing that's confused me how backgroundworker seems have thread-safe access instance variables of surrounding class.
given basic class:
public class backgroundprocessor { public list<int> items { get; private set; } public backgroundprocessor(ienumerable<int> items) { items = new list<int>(items); } public void dowork() { backgroundworker worker = new backgroundworker(); worker.runworkercompleted += new runworkercompletedeventhandler(worker_runworkercompleted); worker.dowork += new doworkeventhandler(worker_dowork); worker.runworkerasync(); } void worker_dowork(object sender, doworkeventargs e) { var processor = new processingclass(); processor.process(this.items); //accessing instance variable } void worker_runworkercompleted(object sender, runworkercompletedeventargs e) { //stuff goes here } }
am erroneous in assumption the call processor.process(this.points);
thread-safe call? how don't cross-thread access violation?
i'm sure it's obvious, has confused me.
it's not thread-safe, looks way. cross-thread exception gui controls. there hard requirement gui controls accessed thread created them. framework takes time check calls other threads. note orthogonal synchronization issues (at least our point of view, not point of view of user subsystem) still use locks prevent multiple threads accessing control @ same time , still cross-thread violation.
since member variable not gui control, there no check cross-thread calls, nor there check race conditions. have locking or other mechanism. no exception unless collection classes corrupted. don't believe thread-safe, i'm not sure ends meaning, nor have ever shared kinds of variables between threads running @ same time, haven't had problem. suffice say, it's better right thing hope collection classes magically work.
- Get link
- X
- Other Apps
Comments
Post a Comment