title: Is it possible to use another thread inside a lock to redirect output in a file?
body: <p>I have two functions using a lock statement, where after some operations finished, I redirect the output in a .txt file. I have realised that their executions take a lot of time, resulting in blocking their operations and degrading the app's performance in general. </p>
  
  <p>I was thinking that the high execution time could be due to the write operations into a file. What would be the most efficient way to reduce the execution time? Should I use another thread for the write operations, is it possible inside a lock without holding the lock?</p>
  
  <p>A simplified version of my code is illustrated below:</p>
  
  <pre><code>    StatsInformation statsInfo = new StatsInformation ();
      List&lt;int&gt; lInt = new List&lt;int&gt;();
  
      public void FunctionEnq(List&lt;byte&gt; lByte, int _int)
      {
          lock (OperationLock)
          {
             //Do some work here 
             lInt.Add(_int);
             string result = "New Int " + _int + " size " + lInt.Count + " time " + DateTime.Now.ToString("hh:mm:ss.fff");
             statsInfo.WriteStatsInFile(result); 
          }
      }
  
      public (List&lt;byte&gt; _outByte, int _time) FunctionDeq()
      {
          List&lt;byte&gt; _outByte = new List&lt;byte&gt; ();
          int _time = -1;
          lock (OperationLock)
          {
             //Do some work here 
             _outByte.Add(...);
             int _int =  lInt[0];
             //do operations
             _time = _int;
             lInt.RemoveAt(0);
             string result = "Get Int " + _int + " new size " + lInt.Count + " time " + DateTime.Now.ToString("hh:mm:ss.fff");
             statsInfo.WriteStatsInFile(result); 
          }
          return (_outByte, _time);
      }
  </code></pre>