Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/28/16 17:03:36 (8 years ago)
Author:
thasling
Message:

#2615:
implemented lock for timers, because if elapsed-method takes longer than the timer to elapse again, the method is called by another thread conccurently

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs

    r13937 r13943  
    1818    private Timer timer; //sends cached messages to network in background
    1919
     20    private Object timerLock = new Object();
     21
    2022    public void Init(PeerInfo source) {
    2123      myself = source;
    2224      bufferedMessages = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[][]>>();
    23       bufferedMessages.Limit = 1000;
     25      bufferedMessages.Limit = 100000;
    2426      timer = new Timer(1000 * 60); //each 5 minutes
    2527      timer.Elapsed += GenerateSendingTasks;
     
    5254    /// <param name="data">data to send</param>
    5355    private void SendDataFromQueue(PeerInfo destination, byte[][] data) {
    54       try {
    55         Console.WriteLine(string.Format("Sending data to {0}:{1} in the background...", destination.IpAddress, destination.Port));
    56         var serviceUrl = "DistributedGA.svc";
    57         var baseUri = new Uri(string.Concat("net.tcp://", destination.IpAddress, ":", destination.Port, "/DistributedGA"));
    58         var serviceUri = new Uri(baseUri, serviceUrl);
     56      lock (timerLock) {
     57        try {
     58          Console.WriteLine(string.Format("Sending data to {0}:{1} in the background...", destination.IpAddress, destination.Port));
     59          var serviceUrl = "DistributedGA.svc";
     60          var baseUri = new Uri(string.Concat("net.tcp://", destination.IpAddress, ":", destination.Port, "/DistributedGA"));
     61          var serviceUri = new Uri(baseUri, serviceUrl);
    5962
    60         var binding = new NetTcpBinding();
    61         var endpoint = new EndpointAddress(serviceUri);
    62         using (var myChannelFactory = new ChannelFactory<IMessageContract>(binding, endpoint)) {
    63           using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) {
    64             ((IMessageContract)client).SendData(myself, data); //maybe timout exception...
     63          var binding = new NetTcpBinding();
     64          var endpoint = new EndpointAddress(serviceUri);
     65          using (var myChannelFactory = new ChannelFactory<IMessageContract>(binding, endpoint)) {
     66            using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) {
     67              ((IMessageContract)client).SendData(myself, data); //maybe timout exception...
     68            }
    6569          }
    6670        }
     71        catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
    6772      }
    68       catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
    6973    }
    7074  }
Note: See TracChangeset for help on using the changeset viewer.