public void OnTaskCompleted(string text) { if (InvokeRequired) { TaskCompletedDelegate taskCompletedDelegate = new TaskCompletedDelegate(OnTaskCompleted); BeginInvoke(taskCompletedDelegate, new object[] { text }); } else { MoonLogger.Instance.Info(text); Interlocked.Increment(ref taskCompletedCount); //多线程时会出错 //taskCompletedCount++; text = string.Format("当前已完成{0}个任务,还剩{1}个任务!", new object[] { taskCompletedCount, taskCount - taskCompletedCount }); MoonLogger.Instance.Info(text); //tsslStatus.Text = text; //如果完成任务总数等于任务总数 if (taskCount == taskCompletedCount) { text = string.Format("所有机器人发送任务已完成,总用时:{0:F}秒", new object[] { (DateTime.Now - beginTime).TotalSeconds }); MoonLogger.Instance.Info(text); // tsslStatus.Text = text; SetButtonState(4); } } }
public void CallbackTest() { TaskCompletedDelegate callbackDelegate = ProcessCallbackResult; AnotherClass anotherClass = new AnotherClass(); anotherClass.StartNewTask(callbackDelegate); }
public CancellableTask(DoWorkDelegate doWork, TaskCompletedDelegate completed) : this(new BackgroundWorker()) { this._doWork = doWork; this._completed = completed; this._worker.DoWork += new DoWorkEventHandler(this._doWork); this._worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this._completed); }
public SampleTaskRun1Minute(DataAccessMgr daMgr , string taskId , int taskQueueCode , string parameters , TaskCompletedDelegate taskCompletedHandler , string engineId) : base(daMgr, taskId, taskQueueCode, parameters, taskCompletedHandler, engineId) { }
internal void StartNewTask(TaskCompletedDelegate callbackDelegate) { // doing something long time to obtain System.Threading.Thread.Sleep(2000); string taskResult = "I am a long task rezult!"; if (callbackDelegate != null) { // ProcessCallbackResult(string result) - call() Console.WriteLine(callbackDelegate(taskResult)); } }
/// <summary> /// Constructor for a new task process that can queued and processed by Transaction Processing Engine (TPE) /// </summary> /// <param name="daMgr">A data access manager object</param> /// <param name="taskId">Unique Task Identifier</param> /// <param name="taskQueueCode">Unique code for the entry in the task processing queue</param> /// <param name="parameters">Optional parameters to pass to function</param> /// <param name="taskCompletedHandler">A delegate which will be called when task is completed or fails</param> /// <param name="threadPoolLabel">A string identifier to be used for the set of threads created under this instance</param> public TaskProcess(DataAccessMgr daMgr , string taskId , int taskQueueCode , string parameters , TaskCompletedDelegate taskCompletedHandler , string threadPoolLabel) { _daMgr = daMgr; _taskId = taskId; _taskQueueCode = taskQueueCode; _parameters = parameters; _taskCompletedHandler = taskCompletedHandler; _threadPoolLabel = threadPoolLabel; _processStatus = ProcessStatusEnum.Ready; }
//public ObservableCollection<GetDataFromWebservice> wDatas { get; } = new ObservableCollection<GetDataFromWebservice>(); //public List<Dictionary<string, string>> resultListMap { get; } = new List<Dictionary<string, string>>(); //public ObservableCollection<GetDataFromWebservice> wDatas { get; } = new ObservableCollection<GetDataFromWebservice>(); //public ObservableCollection<T> wDatas { get; } = new ObservableCollection<T>(); public async void GetWebDataTask(TaskCompletedDelegate delegateResult, String url, Dictionary <string, string> urlParam = null, Byte[] documentcontents = null) { try { url = url.Replace(" ", "%20"); String json; var client = new HttpClient(); if (urlParam != null) { var newDictionary = urlParam .Select(x => new KeyValuePair <string, string>(x.Key.Replace("@", ""), x.Value)) .ToDictionary(x => x.Key, x => x.Value); var content = new FormUrlEncodedContent(newDictionary); var response = await client.PostAsync(url, content); json = await response.Content.ReadAsStringAsync(); } else { json = await client.GetStringAsync(url); } var items = JsonConvert.DeserializeObject <List <T> >(json); //ResultJewelryMd ObservableCollection <T> wDatas = new ObservableCollection <T>(); foreach (var item in items) { wDatas.Add(item); } delegateResult(wDatas); } catch (Exception ex) { delegateResult(null); Debug.WriteLine(ex); } }
public void RunTask(object state, RunTaskDelegate task, TaskCompletedDelegate onTaskCompleted) { object result = task(state); onTaskCompleted(result); }
public void RunTask(object state, RunTaskDelegate task, TaskCompletedDelegate onTaskCompleted) { _task = task; _onTaskCompleted = onTaskCompleted; _testRunner.RunWorkerAsync(state); }