Multithreading - GhostLabsAB/Examples GitHub Wiki

A Source-adapter can be coded to support multiple threads. There is a special base property called “SupportsMultiThreading” which has to be set to true in the adapter constructor.

  public Startup()
  { 
    SupportsMultiThreading = true;
  }

Example of parallel code:

  IEnumerable<string> matches = Directory.EnumerateFiles(m_NodeParams.Path, m_NodeParams.Filter);
  int matchesCount = matches.Count();
  if (matchesCount > 0)
  {
    if (base.MultiThreadingEnabled && base.MaxThreads > 1)
    {
      var result = Parallel.ForEach(matches,
          new ParallelOptions
          {
            MaxDegreeOfParallelism = base.MaxThreads, 
            CancellationToken = CancellationToken
          },
          (fi, state, index) =>
          {
            if (!IsRunning || IsSuspended)
              state.Stop();

             HandleMessage(fi);
           });
    }
    else
    {
      foreach (FileInfo fi in matches)
      {
        // Break if we're stopping or suspending
        if (!IsRunning || IsSuspended)
          break;

          HandleMessage(fi);
      }
    }
  }
⚠️ **GitHub.com Fallback** ⚠️