private BackgroundAction StartBufferThread(Guid id, StreamReader reader) { var bufferSize = 8192; var buffer = new char[bufferSize]; var action = new BackgroundAction(() => { var bytesRead = reader.Read(buffer, 0, bufferSize); lock (_buffers) { StringBuilder sb; if (_buffers.TryGetValue(id, out sb)) { sb.Append(new string(buffer, 0, bytesRead)); } } }) { Interval = 100 }; action.StartAsync(); return(action); }
private void Init() { View.ModelTitleGetter = delegate(object rowObject) { return(((AbstractBackgroundAction)rowObject).toString()); }; View.ModelDescriptionGetter = delegate(object rowObject) { return (((BackgroundAction)rowObject).getActivity()); }; View.ModelIsRunningGetter = delegate(object rowObject) { return(((BackgroundAction)rowObject).isRunning()); }; View.StopActionEvent += View_StopActionEvent; BackgroundActionRegistry.global().addListener(new BackgroundActionListener(this)); // Add already running background actions ArrayList tasks = new ArrayList(); int size = BackgroundActionRegistry.global().size(); for (int i = 0; i < size; i++) { try { BackgroundAction action = (BackgroundAction)BackgroundActionRegistry.global().get(i); tasks.Add(action); } catch (Exception) { Log.debug("BackgroundActionRegistry modified while iterating"); // collection has been modified by another thread, continue } } View.SetModel(tasks); }
public ProgressForm(string title, BackgroundAction action) { InitializeComponent(); Style = (Style)FindResource(typeof(Window)); Title = title; detailTextBox.Visibility = Visibility.Collapsed; bg = new BackgroundWorker(); bg.WorkerReportsProgress = true; bg.WorkerSupportsCancellation = true; bg.DoWork += (bw, dwe) => { try { if (!action(this, (BackgroundWorker)bw)) { dwe.Cancel = true; } } catch (Exception e) { this.Dispatcher.Invoke(new Action(() => { ErrorForm.ShowDialog(this, e); status.Text = "Error."; })); } }; bg.ProgressChanged += UpdateProgress; bg.RunWorkerCompleted += Completed; bg.RunWorkerAsync(); }
public override void ApplyTemplateInfo(ActionTemplateInfo ati, BackgroundAction action) { // default implementation: do nothing ServiceDataObject sdo = GetServiceDataObjectByID(ati.ID); if (sdo == null) { Log.ErrorFormat("Unable to find object for ID {0}", ati.ID); } else { string expectedStartType = ati["StartTypeString"]; string actualStartType = sdo.StartTypeString; if (expectedStartType != actualStartType) { Log.InfoFormat("=> StartType for {0} needs to change from {1} to {2}", ati, actualStartType, expectedStartType); using (NativeSCManager scm = new NativeSCManager(MachineName)) { SC_START_TYPE startType = ServicesLocalisation.ReverseLocalizedStartType(expectedStartType); if (startType != SC_START_TYPE.SERVICE_NO_CHANGE) { sdo.ApplyStartupChanges(scm, startType); } } } else { Log.InfoFormat("=> StartType for {0} identical, no need to change", ati); } } }
public override void collectionItemRemoved(object item) { BackgroundAction action = item as BackgroundAction; _actionListener.Remove(action); _controller.Invoke(new RemoveTaskAction(_controller, action)); }
public override void collectionItemAdded(object item) { BackgroundAction action = item as BackgroundAction; TaskListener listener = new TaskListener(_controller, action); _actionListener.Add(action, listener); _controller.Invoke(new AddTaskAction(_controller, action)); }
public ObjectDatabaseClient(ObjectClient client, Func <TObject, TPrimaryKey> keySelector) { _client = client; _repository = new ObjectRepository <TObject, TPrimaryKey>("Client", keySelector); _thread = new BackgroundAction(SaveRepo) { Interval = 1000 }; _thread.StartAsync(); }
private void View_StopActionEvent() { BackgroundAction action = View.SelectedTask; if (null != action) { Log.debug("Cancel action:" + action); action.cancel(); } }
public IJob Create(string tag, BackgroundAction action) { if (tag == null) { throw new ArgumentNullException(nameof(tag)); } if (action == null) { throw new ArgumentNullException(nameof(action)); } var job = new GoodJob(tag, action); lock (_jobsSyncRoot) { _jobsByTag[tag] = job; _jobsByGuid.Add(job.Guid, job); } return(job); }
public void stop(BackgroundAction ba) { _controller.View.RefreshTask(_action); }
public void Update(Game game, float dt) { if (actions == null) { actions = new BackgroundAction[game.clientmodsCount]; for (int i = 0; i < game.clientmodsCount; i++) { actions[i] = new BackgroundAction(); } } if (game.platform.MultithreadingAvailable()) { for (int i = 0; i < game.clientmodsCount; i++) { game.clientmods[i].OnReadOnlyMainThread(game, dt); } bool allDone = true; for (int i = 0; i < game.clientmodsCount; i++) { if (actions[i] != null && actions[i].active && (!actions[i].finished)) { allDone = false; } } if (allDone) { for (int i = 0; i < game.clientmodsCount; i++) { game.clientmods[i].OnReadWriteMainThread(game, dt); } for (int i = 0; i < game.commitActions.count; i++) { game.commitActions.items[i].Run(); } game.commitActions.Clear(); for (int i = 0; i < game.clientmodsCount; i++) { BackgroundAction a = actions[i]; a.game = game; a.dt = dt; a.i = i; a.active = true; a.finished = false; game.platform.QueueUserWorkItem(a); } } } else { for (int i = 0; i < game.clientmodsCount; i++) { game.clientmods[i].OnReadOnlyMainThread(game, dt); } for (int i = 0; i < game.clientmodsCount; i++) { game.clientmods[i].OnReadOnlyBackgroundThread(game, dt); } for (int i = 0; i < game.clientmodsCount; i++) { game.clientmods[i].OnReadWriteMainThread(game, dt); } for (int i = 0; i < game.commitActions.count; i++) { game.commitActions.items[i].Run(); } game.commitActions.Clear(); } }
protected override void open(BackgroundAction download) { controller.background(new OpenBackgroundAction(controller, download)); }
protected override void save(BackgroundAction upload) { controller.background(new SaveBackgroundAction(controller, upload)); }
public void RefreshTask(BackgroundAction action) { activitiyListView.RefreshObject(action); }
public void Background(BackgroundAction backgroundAction) { // Move to background thread background(backgroundAction); }
public void AddTask(BackgroundAction action) { activitiyListView.AddObject(action); }
private GoodJob(string tag, BackgroundAction action) { Action = action; Tag = tag; }
public SaveBackgroundAction(BrowserController controller, BackgroundAction upload) : base(controller) { _upload = upload; }
public OpenBackgroundAction(BrowserController controller, BackgroundAction download) : base(controller) { _download = download; }
public override void stop(BackgroundAction action) { Invoke(delegate { View.StopActivityAnimation(); }); }
public void RemoveTask(BackgroundAction action) { activitiyListView.RemoveObject(action); }
public TaskListener(ActivityController controller, BackgroundAction action) { _action = action; _controller = controller; }
public RemoveTaskAction(ActivityController controller, BackgroundAction action) : base(controller) { _action = action; _controller = controller; }
public override void ApplyTemplateInfo(ActionTemplateInfo ati, BackgroundAction action) { // default implementation: do nothing ServiceDataObject sdo = GetServiceDataObjectByID(ati.ID); if (sdo == null) { Log.ErrorFormat("Unable to find object for ID {0}", ati.ID); } else { string expectedStartType = ati["StartTypeString"]; string actualStartType = sdo.StartTypeString; if (expectedStartType != actualStartType) { Log.InfoFormat("=> StartType for {0} needs to change from {1} to {2}", ati, actualStartType, expectedStartType); using (NativeSCManager scm = new NativeSCManager(MachineName)) { SC_START_TYPE startType = ServicesLocalisation.ReverseLocalizedStartType(expectedStartType); if( startType != SC_START_TYPE.SERVICE_NO_CHANGE ) { sdo.ApplyStartupChanges(scm, startType); } } } else { Log.InfoFormat("=> StartType for {0} identical, no need to change", ati); } } }