task

Passing parameter into a Task.Factory.StartNew

随声附和 提交于 2019-12-14 00:18:12
问题 Given the following code: string injectedString = "Read string out of HttpContext"; Task.Factory.StartNew(() => { MyClass myClass = new MyClass(); myClass.Method(injectedString); } Is this the best way to pass the string into the Task/Thread? My concerns with this method are: Would the garbage collector know when the string became out of context and clean it up correctly? Is there a better way to inject dependencies into a Task breaking the link to the object in the main thread? This is in a

Processing tasks sequentially

我与影子孤独终老i 提交于 2019-12-13 23:29:32
问题 i have a base class JobBase that has a single method Execute() which returns a Task. There are several classes that inherits from JobBase and that do some asynchronous operations. I need to put instances of the inherited classes into a list and execute them sequentially, so having job1, job2, job3, etc. job2 shall start when job 1 is finished, job 3 shall start when job2 is finished, etc. So the queue may contain an arbitrary number of jobs. For example: job1 downloads some content, job2

Powershell Task Scheduler Stuck Running

a 夏天 提交于 2019-12-13 21:46:12
问题 I was trying to test a simple powershell script with task scheduler, the status showed running but the powershell console never showed up. My ps1 script just contains two simple commands: dir pause Here is my setup: General Run whether user is logged on or not (check) Run with highest privileges (check) Actions Action: Start a program Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Add arguments (optional): -file E:\iQ_Schedule\Untitled1.ps1 This setup works on other

Run Service in background Thread

二次信任 提交于 2019-12-13 20:15:54
问题 Can I run Service in background thread? I want to load data from database and during loading data i want progress bar to be indicating progress. First I have created task, run it in background thread and then update progress bar according to this task. However, I found out that this Task is not reusable, so next time i hit the button, it didn't work. Later on, I fount out that Service can run Tasks multiple times, So i have encapsulated those tasks inside of Service. Now it works great -

Cancel Specific Task using CancellationToken

≯℡__Kan透↙ 提交于 2019-12-13 19:28:42
问题 I have the following Loop which may or may not create a series of tasks dynamically: While(CreateNewTask == true) { if(isWorkerFree() && isValidJob() && isExecutable()) { CancellationTokenSource cs = new CancellationTokenSource(); var myTask = Task.Run(() => Execute(cs.token); } } Now since these tasks are created dynamically, how can I track them and cancel a specific task or send a cancellation token to a specific task? There may be 6-7 tasks running at any time, I need the functionality to

C# How to catch exception in a Task without block UI using wait

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 18:39:41
问题 I have this code in a button click private async void btnStart_Click(object sender, EventArgs e) { Msg.Clear(); stopWatch.Reset(); timer.Start(); stopWatch.Start(); lblTime.Text = stopWatch.Elapsed.TotalSeconds.ToString("#"); progressBar.MarqueeAnimationSpeed = 30; try { await Task.Factory.StartNew(() => { try { Reprocess(); } catch (Exception ex) { Msg.Add(new clsMSG(ex.Message, "Error", DateTime.Now)); timer.Stop(); stopWatch.Stop(); throw; } }); } catch { throw; } } and this on the

Keep UI responsive using Tasks, Handle AggregateException

烂漫一生 提交于 2019-12-13 16:46:25
问题 I have a problem with handling the AggregateException if my WinForms application starts a task to keep responsive while the task is performing. The simplified case is as follows. Suppose my Form has a fairly slow method, for instance: private double SlowDivision(double a, double b) { System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5)); if (b==0) throw new ArgumentException("b"); return a / b; } After pressing a button I want my form to show the result of SlowDivision(3,4). The following

Run 2 tasks in parallel and cancel the 2nd task if the first completes .NET

青春壹個敷衍的年華 提交于 2019-12-13 12:42:26
问题 If have 2 methods I want to run in parallel on different threads, let's call them Task1 and Task2. If Task2 completes before Task1 then I want the results of both combined into a list. If Task1 completes and Task2 is still running, it should cancel Task2 to avoid further processing. WhenAll doesn't work because it will wait for both tasks. WhenAny doesn't work because it will return if either finish first. 回答1: This is the solution I came up with. static void Main(string[] args) { var

Catch exception in async Task

佐手、 提交于 2019-12-13 12:23:09
问题 I'm working in C# (console application). In my program, I have to contact a httpClient. First I check if the client is responding using GetAsync . So my request method is async, making my Task async. When client doesn't respond (or something else) it throw an exception but i'm unable to catch it. I added a ContinueWith but it doesn't work. With a breackpoint I saw that the piece of code is reached at the start of my Task so the exception is always null. How can I solve this problem ? There is

How two different task can goto one task in SSIS

时间秒杀一切 提交于 2019-12-13 12:01:19
问题 please see the figure to get idea of the scenario. Now when my Package will run after executing the s! task, either A or B path will be followed. So If A is followed then Task S2 and F2 is executed. But if path B is followed, task F1 and F3 is executed. But also after completing task F3, the flow should go to task S2 via path E. But this doesn't happen and as the task F3 is completed, the package ends with success. I need help on this on how to move to path E and execute task S2 and F2 after