title: Multi-threading in Azure
body: <p>I wanted to shift a windows service to azure by the means of running it through an Azure function.</p>
  <p>The windows service runs multi-threading using <code>Parallel.ForEach</code> loop and just to test whether the multithreading works in Azure perfectly or not , I wrote the below code:</p>
  <p>Azure Function :</p>
  <pre class="lang-cs prettyprint-override"><code>[FunctionName(&quot;Function1&quot;)]
  public static void Run([TimerTrigger(&quot;0 */5 * * * *&quot;)] TimerInfo myTimer, TraceWriter log)
  {
      try
      {
          Process process = new Process();
  
          process.StartInfo.FileName = @&quot;D:\home\site\wwwroot\Dependency\MasterVB\MasterVB.exe&quot;;
          process.StartInfo.Arguments = &quot;&quot;;
          process.StartInfo.UseShellExecute = false;
          process.StartInfo.RedirectStandardOutput = true;
          process.StartInfo.RedirectStandardError = true;
  
          log.Info(&quot;Before Calling Start &quot;);
          process.Start();
          log.Info(&quot;After Calling Start &quot;);
  
          string output = process.StandardOutput.ReadToEnd();
          string err = process.StandardError.ReadToEnd();
          process.WaitForExit();
      }
      catch
      {
        //left out for brevity
      }
  }
  </code></pre>
  <p>MasterVB.exe:</p>
  <pre class="lang-vb prettyprint-override"><code>Imports System.Collections.Concurrent
  Imports Microsoft.WindowsAzure.Storage
  Imports Microsoft.WindowsAzure.Storage.Blob
      
  Module Module1
      
      Public connectionString As String = &quot;DefaultEndpointsProtocol=https;AccountName=r53eripcjroswtest;AccountKey=cktlWe/byeSOis94OOA0YFlHPRrAfmORuy9xRGScTrxwuliY4KmZfAif8aF4cgng2mnDLvJWIvxl0xEPMU6DKw==;&quot;
      Public storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(connectionString)
      Public serviceClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
      Public container As CloudBlobContainer = serviceClient.GetContainerReference(&quot;igb-test&quot;)
      Public blob As CloudAppendBlob = container.GetAppendBlobReference(&quot;DocumentMaster.txt&quot;)
      
      Sub Main()
      
          If Not blob.Exists Then
              blob.CreateOrReplace()
          End If
      
          Dim dealist As New List(Of String)
      
          dealist.Add(&quot;First&quot;)
          dealist.Add(&quot;Second&quot;)
          dealist.Add(&quot;Third&quot;)
          dealist.Add(&quot;Fourth&quot;)
          dealist.Add(&quot;Fifth&quot;)
          dealist.Add(&quot;Sixth&quot;)
          dealist.Add(&quot;Seventh&quot;)
          dealist.Add(&quot;Eighth&quot;)
          dealist.Add(&quot;Nine&quot;)
          dealist.Add(&quot;Tenth&quot;)
          Dim exceptions As New ConcurrentQueue(Of Exception)
          Dim opts As New ParallelOptions
          opts.MaxDegreeOfParallelism = -1
          Parallel.ForEach(dealist, opts, Sub(deal)
                                              If Len(deal) &gt; 0 Then
                                                  Try
                                                      Dim p As New Process()
                                                      p.StartInfo.FileName = &quot;D:\home\site\wwwroot\Dependency\MasterVB\ChildVB\ChildVB.exe&quot;
                                                      p.StartInfo.Arguments = deal
                                                      p.StartInfo.UseShellExecute = False
                                                      p.StartInfo.CreateNoWindow = True
                                                      p.Start()
                                                      p.WaitForExit()
                                                  Catch ex As Exception
                                                      exceptions.Enqueue(ex)
                                                  End Try
                                              End If
                                          End Sub)
      
          If exceptions.Count &gt; 0 Then
              For Each ex In exceptions
                  'Console.WriteLine(ex.Message)
                  blob.AppendText(ex.Message + Environment.NewLine)
              Next
          End If
      
      End Sub
      
  End Module
  </code></pre>
  <p>ChildVB.exe</p>
  <pre class="lang-vb prettyprint-override"><code>Imports Microsoft.WindowsAzure.Storage
  Imports Microsoft.WindowsAzure.Storage.Blob
  Module Module1
      Public keycode As String = &quot;&quot;
      Public dealPass As String = &quot;&quot;
      Public runType As String = &quot;&quot;
      
      Public connectionString As String = &quot;DefaultEndpointsProtocol=https;AccountName=r53eripcjroswtest;AccountKey=cktlWe/byeSOis94OOA0YFlHPRrAfmORuy9xRGScTrxwuliY4KmZfAif8aF4cgng2mnDLvJWIvxl0xEPMU6DKw==;&quot;
      Public storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(connectionString)
      Public serviceClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
      Public container As CloudBlobContainer = serviceClient.GetContainerReference(&quot;igb-test&quot;)
      Public blob As CloudAppendBlob = container.GetAppendBlobReference(&quot;DocumentChild.txt&quot;)
      
      Sub Main()
          Dim clArgs() As String = Environment.GetCommandLineArgs()
      
          If Not blob.Exists Then
              blob.CreateOrReplace()
          End If
              
          If clArgs.Count = 2 Then
      
              Dim args As String = clArgs(1)
              blob.AppendText(args + &quot;----&quot; + DateTime.Now.ToString(&quot;MM/dd/yyyy HH:mm:ss&quot;) + Environment.NewLine)                
      
          End If
      End Sub
      
  End Module
  </code></pre>
  <p>In the DocumentChild.txt , i am getting the following output:</p>
  <pre><code>First----08/12/2020 15:15:03
  Sixth----08/12/2020 15:15:04
  Third----08/12/2020 15:15:05
  Seventh----08/12/2020 15:15:06
  Fourth----08/12/2020 15:15:07
  Nine----08/12/2020 15:15:09
  Tenth----08/12/2020 15:15:10
  Fifth----08/12/2020 15:15:10
  
  First----08/12/2020 15:20:17
  Sixth----08/12/2020 15:20:17
  Second----08/12/2020 15:20:18
  Seventh----08/12/2020 15:20:19
  Fifth----08/12/2020 15:20:21
  Eighth----08/12/2020 15:20:21
  Nine----08/12/2020 15:20:23
  Tenth----08/12/2020 15:20:23
  Fourth----08/12/2020 15:20:24
  
  First----08/12/2020 15:25:02
  Sixth----08/12/2020 15:25:03
  Second----08/12/2020 15:25:04
  Seventh----08/12/2020 15:25:04
  Third----08/12/2020 15:25:05
  Fifth----08/12/2020 15:25:06
  Eighth----08/12/2020 15:25:07
  Fourth----08/12/2020 15:25:07
  Nine----08/12/2020 15:25:09
  Tenth----08/12/2020 15:25:09
  
  First----08/12/2020 15:30:01
  Sixth----08/12/2020 15:30:01
  Second----08/12/2020 15:30:03
  Third----08/12/2020 15:30:03
  Seventh----08/12/2020 15:30:04
  Fifth----08/12/2020 15:30:05
  Nine----08/12/2020 15:30:06
  Fourth----08/12/2020 15:30:06
  Eighth----08/12/2020 15:30:07
  Tenth----08/12/2020 15:30:08
  
  First----08/12/2020 15:35:02
  Second----08/12/2020 15:35:04
  Third----08/12/2020 15:35:04
  Fifth----08/12/2020 15:35:06
  Seventh----08/12/2020 15:35:06
  Fourth----08/12/2020 15:35:07
  Nine----08/12/2020 15:35:08
  Eighth----08/12/2020 15:35:08
  Tenth----08/12/2020 15:35:09
  </code></pre>
  <p>Above lines shows the output at every different azure function run after 5 mins for five different times.
  The output in the first and second instance, at 15:15 and 15:20 do not print all the ten strings but does so in the third and fourth instance but does not print all the strings in the fifth instance.
  In the actual code, the deallist mentioned in MasterVB will contain more than 4,000 strings to process in the same way,i.e., master calling child using mutlithreading.</p>
  <p>My Questions are:</p>
  <ol>
  <li>Why is the multithreading in this case not consistent and prints all ten strings every time?
  My concern is if its inconsistent with just 10 strings , will it be able to process 4000 strings with<br />
  more operations than just printing to a text file in the ChildVB module?</li>
  <li>Is there any good alternative to shift this windows service,which involves multi-threading, to azure.</li>
  <li>Is there a possibility this could be related to deployment plan.</li>
  </ol>
  <p>Note:</p>
  <p>1.I am using consumption plan to deploy but not facing any issues such as timeout which is there in this
  plan as a standard(10 mins)</p>
  <ol start="2">
  <li>I have ran this for several different times , but the inconsistency persists everytime the azure
  function is run.</li>
  <li>There is no complilation or runtime error.</li>
  </ol>