0

Here is the code whose responsibility is to connect to the server and get some details.

public class MigrationManager
{
       private string referenceServerId;

       public void MigrateAnalyzer(string serverid)
       {
            Gw _Gw = new Gw() // library reference
           _Gw.onConnectSucceeded += Handle_OnConnectSucceeded;
           _Gw.onConnectFailed += Handle_OnConnectFailed;

           ConnectToServer(prxHA, serverid);
       }

       private void ConnectToServer(string sid)
       {            
          workstationIDForReference = anayzer;
          string credentials = GWServiceCredentials("somesecret");
           referenceServerId = sid;
         _Gw.connectToServer("SSL", "device1-mydomain.net", credentials, referenceServerId);
       }

     }

This connection operation can end up in two scenarios.

  • Connection success
  • Connection failure

These events handled as shown below

private void Handle_OnConnectFailed(int hr)
{
      string msg = $"{referenceServerId}";
      Console.WriteLine("Connecting to server {0} is failed", msg); 
}

private void Handle_OnConnectSucceeded()
{
     Console.WriteLine("Connecting to server is success");
}

This works well. Now I am changing it to support multithreading. Because there are multiple requests to connect to the server.

List<string> requestCollection = new List<string>();
requestCollection.Add("I3-1");
requestCollection.Add("I3-2");
requestCollection.Add("I3-3");

var taskList = new List<Task>();

foreach (var serverid in requestCollection )
{
     MigrationManager manager = new MigrationManager();
     var task = Task.Factory.StartNew(() => manager.MigrateAnalyzer(serverid.ToString()));
}

Task.WaitAll(taskList.ToArray());

I see below Output

Console.WriteLine("Connecting to server I3-1 is failed");
Console.WriteLine("Connecting to server I3-2 is failed");
Console.WriteLine("Connecting to server I3-3 is failed");

Output looks good!

But, I think COM will use shared resources. so how do I use "lock"?

kudlatiger
  • 3,028
  • 8
  • 48
  • 98
  • Do I missunderstand something or why is the requestid (which is being printed in the event-handlers) static? That doesn't make sense to me. I also dont see where you would get the requestid that you print in the eventhandlers other than from this static variable. Neither do I see any assignment to this static field. I think you should examine that static field. – Joelius May 28 '19 at 11:04
  • I'm assuming the assignment is made in _Gw.connectToServer right? If so, could you add this method to your question? If not could you search for the assignment and show us where that is? – Joelius May 28 '19 at 11:05
  • You now edited the question to make the variable not static. I still think we need to see where and how this variable is assigned though. It's still one variable used by multiple events (static or non-static doesn't change that it's problematic). Can you show us the assignment? By the way, is there any reason why you used ArrayList? It's always better to use List, especially if you have only strings in it. – Joelius May 28 '19 at 12:57
  • @Joelius Yes, I was about to inform you. I was updating the question. – kudlatiger May 28 '19 at 12:58
  • @Joelius update complete. now please review – kudlatiger May 28 '19 at 13:03
  • Alright thanks. A few things I'd like to point out before going after the actual problem again: 1. Since you're not adding parameters to the Factory.StartNew method you can/should just use Task.Run (refer to [this answer](https://stackoverflow.com/a/38423507/10883465)), 2. using a List there's no need to call .ToString since it already is a string, 3. your ConnectToServer(requestid) signature is missing the parameter type and 4. I still don't see any requestid = ... calls. – Joelius May 28 '19 at 13:10
  • @Joelius Hope now it looks good. Now, how can I use "lock"? – kudlatiger May 28 '19 at 14:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194061/discussion-between-joelius-and-kudlatiger). – Joelius May 28 '19 at 15:23

0 Answers0