Backgroundworker and how to lock the shared object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mihai a
    New Member
    • Jul 2011
    • 1

    Backgroundworker and how to lock the shared object

    Hello C# Gurus,

    My question is about threading in C# (backgroundwork er and shared object)

    I have a communication class designed with singleton pattern (only one instance )

    private PRS485 serial = PRS485.RS485;

    Inside this class I’ve implemented a backgroundworke r for serial communication.
    With the help of PData class I’ve created a shared object between the main thread and backgroundworke r thread. This object in my case is called data and passed as argument to the worker. After some racing problems I realized that I have to lock the shared object.
    My problem: lock seems to have no effect. What I’m doing wrong here?
    I’ve spend hours on Google searching for an answer. How do I implement the lock procedure correctly inside the backgroungworke r?

    Please see below my code (simplified version)


    Thank you for your time.


    Mihai.

    Code:
    public sealed class PRS485
    {
    private static object locker = new object();
    private PData data = new PData();
    private SerialPort port;
    private BackgroundWorker bgw;
    private static readonly PRS485 rs485 = new PRS485();
    
    private PRS485()
    {
    bgw = new BackgroundWorker();
    bgw.WorkerReportsProgress = true;
    bgw.WorkerSupportsCancellation = true;
    bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
    bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
    bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
    
    }
    
    public static PRS485 RS485
    {
    get { return rs485; }
    }
    
    
    public void ReadTelegrams(List<PTelegram> TelegramsToRead)
    {
    data.TelegramsToProcess = TelegramsToRead;
    data.Step = 0;
    data.ProcentProgress = 0;
    data.Mode = false;
    if (PortIsOpen && !bgw.IsBusy & data.TelegramsToProcess.Count>0)
    {
    bgw.RunWorkerAsync(data);
    }
    }
    
    
    private void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
    //backgroundworker thread !!
    //here is my problem
    //lock is not working
    //tried also with lock(this) or lock(wData)
    PData wData = e.Argument as PData;
    lock(locker)
    {
    //modify wData object
    }
    e.Result = wData;
    }
    
    private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    PData rData = e.Result as PData;
    //modify rData object
    }
    }
    
    //now the Pdata class
    
    public class PData
    {
    public List<byte> BBuffer = new List<byte>();
    public List<PTelegram> TelegramsToProcess = new     List<PTelegram>();
    public string Message=String.Empty;
    public bool Mode = false;
    public int CommunicationDelay = 100;
    public int CommunicationTimeout = 1000;
    public string MenuFunction = String.Empty;
    public int ProcentProgress;
    public int Step;
    public byte Address = 0x00;
    }
Working...