Thread LOCK & Function Variable

patrick

Well-known member
Joined
Dec 5, 2021
Messages
269
Programming Experience
1-3
Hello

Thread LOCK
Is it correct to place the Thread Lock here?
If you place it here, Thread Lock will not work.


C#:
private static object syncresetdb = new Object();
public string GetList(ref DataFormat mts, bool setItem = false) //<====== variable(mts) Do variables affect thread LOCK?
        {
            lock (syncresetdb) //<======== Thread LOCK
            {
                string ret = null;

                if (mts.AttrList.Count > 0)
                    mts.ViewLayer = -1;
             
                mts.ViewLayer++;

                DBAttrListDataFormat dbAttrViewList = DBAttrList.Instance(DBName).DBAttrViewList;

                IEnumerable<string[]> DBAttrListResult;                  

                try
                {
                    foreach (string[] row in Result)
                    {                  
                        IEnumerable<string[]> valdt = GetAttrSetValueList(key, mts);
                        mts.SetAttrValueList(key, valdt, DBAttrList.Instance(DBName).TransDBEng, viewname, viewnameshown, depth, isIdentify, isEssential, ismulti, layer);
                        mts.ViewLayer = layer;


                        if (delList.ContainsKey(key))
                        {
                            bool notContains = false;
                            if (delList[key] != null)
                            {
                                string[] delvaluelist = delList[key].Split(',');

                                foreach (string devalue in delvaluelist)
                                {
                                    if (!mts.AttrList[key].ValueList.Contains(devalue))
                                    {
                                        if (!setFirstItem)
                                        {
                                            mts.AttrList[key].ValueList.Add(devalue);
                                        }
                                    }
                                }
                            }
                        }              
                    }
                }
                catch (Exception e)
                {
                    return ret;
                }
                return ret;
            }
        }
K
 
Last edited:
You need to explain what you're actually trying to achieve if you want us to tell us whether the code you have will achieve it or not. Something is only right or wrong if it achieves the stated aim or not. If you don't state an aim, we have no idea whether it's right or not.

What you have will prevent the body of that method being executed on more than one thread at a time. If that's your aim, that code will is correct. If it's not, it's not.

It's also worth noting that a lock will prevent that section being executed twice on the same thread too. That means that, in a situation like yours, you must never make a recursive call to that method inside the critical section. You aren't here so that's not a problem, but it's something to keep in mind.
 
The code here looks very similar to the code in your other thread where you were asking for help about an exception being thrown from MoveNextRare(). Are you just trying to put a band aid on the problem by using a thread lock? Did you conclusively determine that it was this code that was being called from another thread that is causing the collection to be changed, or this is just a shot in the dark hoping it would fix the problem?
 
Back
Top Bottom