// Metodo que faz a invocação assincrona, devolve false se não encontrar o servidor private Boolean WriteAux(String serverIP, int serverPort, String localFileName, byte[] data, long version) { IDataToClient dataServer = (IDataToClient)Activator.GetObject( typeof(IDataToClient), "tcp://" + serverIP + ":" + serverPort + "/PADIConnection"); AsyncWriteDel RemoteDel = new AsyncWriteDel(dataServer.Write); AsyncCallback RemoteCallback = new AsyncCallback(ClientEntry.CBDoWhenReturnFromWrite); try { IAsyncResult RemAr = RemoteDel.BeginInvoke(localFileName, data, version, RemoteCallback, null); } catch (SocketException) { System.Console.WriteLine("#WRITE Could not locate server"); return(false); } return(true); }
// This is the call that the AsyncCallBack delegate will reference. public static void CBDoWhenReturnFromWrite(IAsyncResult ar) { AsyncWriteDel del = (AsyncWriteDel)((AsyncResult)ar).AsyncDelegate; String id = del.EndInvoke(ar); System.Console.WriteLine("#WRITE *Data Server " + id + " Returned from Write"); returnedWriteIDs.Add(id); // Se somos o ultimo vamos informat a thread em espera no monitor if (returnedWriteIDs.Count.Equals(targetWriteQuorum)) { Thread.Yield(); Thread.Sleep(50); Thread.Yield(); System.Console.WriteLine("#WRITE *Last Data Server Realeasing Main Thread"); Monitor.Enter(monWriteLock); Monitor.Pulse(monWriteLock); Monitor.Exit(monWriteLock); } return; }