简体   繁体   中英

understanding lock in c#

Please help me to understand the flow of lock in c#. A class with methods is continuously calling by two different threads periodically.

class DataRetriever
{
   BaseUrl="...";
   public static void _Fetch(<list<dataInformation> quote)
   {
      XDocument doc=XDocument.Load(BaseUrl);
      parse(quote,doc);
   }

   private static void Parse(List<dataInformation> quotes, XDocument doc)
   {
       // some statement
   }
}

// dataInformation is a class here

Now Questions are... 1. Is there any need of lock statement here or not if _Fetch() method is being called by two or more threads periodically. If yes please tell me why and how? I want to understand lock flow. 2. Why we use object to be locked?

I've tried here

class DataRetriever
{
   BaseUrl="...";

   object obj=new object()
   public static void _Fetch(<list<dataInformation> quote)
   {
      lock(obj) // giving error obj is not accepting here
      {
         XDocument doc=XDocument.Load(BaseUrl);
         parse(quote,doc);
      }
   }

   private static void Parse(List<dataInformation> quotes, XDocument doc)
   {
       // some statement
   }
}

Also tried to solve

class DataRetriever
{
   BaseUrl="...";

   public static void _Fetch(<list<dataInformation> quote)
   {
      lock(this) // giving error this is not accepting
      {
         XDocument doc=XDocument.Load(BaseUrl);
         parse(quote,doc);
      }
   }

   private static void Parse(List<dataInformation> quotes, XDocument doc)
   {
       // some statement
   }
}

Do the needful. please provide me some useful link if possible. regards

This is correct but you should really pay attention to errors that prevent compiling.

your object obj=new object(); was missing the ;

class DataRetriever
{
   BaseUrl="...";

   object obj=new object();
   public void _Fetch(<list<dataInformation> quote)
   {
      lock(obj) // giving error obj is not accepting here
      {
         XDocument doc=XDocument.Load(BaseUrl);
         parse(quote,doc);
      }
   }

   private void Parse(List<dataInformation> quotes, XDocument doc)
   {
       // some statement
   }
}

As mentioned below you also use an instanced field with static methods. I simply changed the methods to non static.

You are receiving an error because you're trying to use an this which reflects the current instance of your type inside a static method , which belongs to your type, not your instance.

A static method on a non-static class will result in only the static fields to be initialized via a static constructor (if one exists) or via field initialization. That is why your use of this isn't allowed inside your method.

The alternative could be to create a static field for a lock. Note this will mean that this lock object will be shared among all instances of your class.

public static object _lock = new object();

And then:

lock(_lock)
{
    XDocument doc=XDocument.Load(BaseUrl);
    parse(quote,doc);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM