public virtual void StartTransfer(Boxerp.Client.ResponsiveEnum trType) { _transferType = trType; try { if (_threadsPoolHash.Count != 0) { // busy, show an error message } List<MethodInfo> methods = this.GetResponsiveMethods(_transferType); if (methods.Count == 0) { throw new NullReferenceException("No private/protected responsive methods found"); } _asyncCallsCount = methods.Count; foreach (MethodInfo method in methods) { ThreadStart methodStart = new SimpleInvoker(method, this).Invoke; Thread methodThread = new Thread(methodStart); methodThread.Start(); _threadsPoolHash[methodThread.ManagedThreadId] = methodThread; //method.Invoke(this, null); // execute method } } catch (TargetInvocationException ex) { Console.WriteLine("One responsive method raises exception:" + ex.Message+ ex.StackTrace); throw ex; } catch(Exception ex) { throw ex; } // TODO : Write the code to stop threads }
/// <summary> /// Create a block of parallel threads and put it in a single slot in the queue /// </summary> /// <param name="trType"></param> /// <param name="controller"></param> public virtual List<Thread> StartAsyncCallList(Boxerp.Client.ResponsiveEnum trType, IController controller) { List<Thread> threads = new List<Thread>(); if (canGoAhead()) { List<MethodInfo> methods = this.GetResponsiveMethods(trType, controller); if (methods.Count == 0) { throw new NullReferenceException("No private/protected responsive methods found"); } Dictionary<int, Thread> threadsBlock = new Dictionary<int, Thread>(); lock(_innerLock) { foreach (MethodInfo method in methods) { ThreadStart methodStart = new SimpleInvoker(method, controller).Invoke; Thread methodThread = new Thread(methodStart); methodThread.Start(); int id = methodThread.ManagedThreadId; threads.Add(methodThread); threadsBlock[id] = methodThread; _operationSucess[id] = true; _exceptions[id] = null; _operationTypes[id] = trType; _cancelRequests[id] = false; } _threadDictionariesList.Add(threadsBlock); } } return threads; }
/// <summary> /// Create a new thread and put it in the queue /// (as long as there are no more running threads and the mode is not parallel) /// </summary> /// <param name="method">The meethod to run in the background</param> /// <returns>The Thread instance</returns> public virtual Thread StartAsyncCall(SimpleDelegate method) { Thread methodThread = null; if (canGoAhead()) { Dictionary<int, Thread> threadsBlock = new Dictionary<int, Thread>(); ThreadStart methodStart = new SimpleInvoker(method).Invoke; methodThread = new Thread(methodStart); lock(_threadDictionariesList) { methodThread.Start(); int id = methodThread.ManagedThreadId; threadsBlock[id] = methodThread; _operationSucess[id] = true; _exceptions[id] = null; _operationTypes[id] = ResponsiveEnum.Other; _cancelRequests[id] = false; _threadDictionariesList.Add(threadsBlock); Console.Out.WriteLine("*** *** *** thread is in queue now:" + id + "," + _threadDictionariesList.Count); } } return methodThread; }
private void ProcessAsyncCall(SimpleDelegate method) { try { _transferType = ResponsiveEnum.Other; _asyncCallsCount++; // = 1; ThreadStart methodStart = new SimpleInvoker(method).Invoke; Thread methodThread = new Thread(methodStart); methodThread.Start(); _threadsPoolHash[methodThread.ManagedThreadId] = methodThread; } catch (TargetInvocationException ex) { Console.WriteLine("responsive method raises exception:" + ex.Message+ ex.StackTrace); throw ex; } catch(Exception ex) { throw ex; } finally { } }