/// <summary> /// Close the connection with the client /// Call 'connectionClosed' of your application that extends the DKService when the connection is closed successfully /// </summary> public void Close() { // one thread per time, prevent two or more thread call Close() in same time and generate duplicate notifications lock (ThreadLocker.sync("Client::Close")) { if (!Connected) { return; } try { // sets connected false Connected = false; // remove the client from signal ClientSignal.Remove(Id); // close the socket _client.Close(); } catch (Exception ex) { Log.Write("Error when disconnect client " + ex.Message + " - " + ex.StackTrace, LogLevel.ERROR); } finally { // if the socket have a type and defined, notification the application is desconnected if (socketLayer != SocketLayer.undefined) { Application.send(ApplicationSend.connectionClosed, new object[] { this }); socketLayer = SocketLayer.undefined; } } } }
private void StartThreadedNameUpdate() { if (currentThreadCount > 0) { return; } int maxThreads = 1; Queue <UpdateJob> q = new Queue <UpdateJob>(); for (int i = 0; i < lstUsers.Items.Count; i++) { UserRecord r = lstUsers.Items[i] as UserRecord; if (r != null) { q.Enqueue(new UpdateJob(i, r.DirName)); } } int threads = maxThreads > q.Count ? maxThreads : q.Count; if (threads > 0) { currentThreadLock = new ThreadLocker(); SetUpdateInterfaceRunning(); for (int i = 0; i < threads; i++) { Thread t = new Thread(NameUpdateThread); currentThreadCount++; t.Start(new UpdateData(q, currentThreadLock)); } } }
private void SettingsFileChanged(object sender, FileSystemEventArgs e) { if (_saving) { return; } _settingsFileWatcher.EnableRaisingEvents = false; _jsonData = null; AppearanceSettings.Update(); using (ThreadLocker locker = ThreadLocker.Lock(Application.OpenForms)) { if (locker.IsLocked) { int i = 0; while (i < Application.OpenForms.Count) { if (Application.OpenForms[i] is BaseForm form) { MethodInvoker mi = new MethodInvoker(((BaseForm)form).UpdateAppearance); form.Invoke(mi); } i++; } } } _settingsFileWatcher.EnableRaisingEvents = true; }
/// <summary> /// Sends data to client, if exists peding data in sending status /// the actual data param enter in the queue /// </summary> /// <param name="packet">array of bytes to send</param> public void Send(byte[] packet) { if (packet.Length == 0 || packet == null) { return; } // one thread per time lock (ThreadLocker.sync("DataTransport::Send")) { // test the packet is a fix thread concorrent hack work if (packet.Length != 1 || packet[0] != 9) { // not packet thread concorrent // add the data in the queue queueData.Enqueue(packet); } // if asynSending is false and have data to send if (!asynSending && queueData.Count > 0) { // we set to true to say that we are working with sending asynSending = true; // start sending the packets BeginSend(queueData.Dequeue()); } } }
public static void PushAction(StatusAction action) { using (ThreadLocker.Lock(_actions)) { if (_actions.Count == 0) { _startTime = DateTime.Now; } _actions.Push(action); } }
private void tmrCheck_Tick(object sender, EventArgs e) { using (ThreadLocker locker = ThreadLocker.AttemptLock(_actions)) { if (locker.IsLocked && _actions.Count > 0) { ShowBusy(); } else { HideBusy(); } } }
public UpdateData(Queue <UpdateJob> q, ThreadLocker l) { jobs = q; tLock = l; }
public UpdateData( Queue<UpdateJob> q, ThreadLocker l ) { jobs = q; tLock = l; }
private void StartThreadedNameUpdate() { if( currentThreadCount > 0 ) return; int maxThreads = 1; Queue<UpdateJob> q = new Queue<UpdateJob>(); for( int i = 0; i < lstUsers.Items.Count; i++ ) { UserRecord r = lstUsers.Items[i] as UserRecord; if( r != null ) { q.Enqueue( new UpdateJob( i, r.DirName ) ); } } int threads = ( maxThreads > q.Count ) ? maxThreads : q.Count; if( threads > 0 ) { currentThreadLock = new ThreadLocker(); SetUpdateInterfaceRunning(); for( int i = 0; i < threads; i++ ) { Thread t = new Thread( this.NameUpdateThread ); currentThreadCount++; t.Start( new UpdateData( q, currentThreadLock ) ); } } }
public static void PopAction() { using (ThreadLocker.AttemptLock(_actions)) _actions.Pop(); }