task

Nested task inside loop

拜拜、爱过 提交于 2019-12-06 13:17:02
问题 I'm trying to implement a nested task inside a loop - this is the pattern I have so far, however I'm far from sure as this is the first time I've used the parallel task library. The parent (tier) task should wait for the children (node) tasks to complete. public int NestedTask(IEnumerable<MatchTier> tierNodes) { var tier = Task<int>.Factory.StartNew(() => { Task<int> node = null; foreach(var n in tierNodes) { node = Task<int>.Factory.StartNew(() => { // Task logic goes here return 1; // temp

Why does Type.IsGenericType return TRUE for Task without return type obtained by reflection from method but typeof(Task).IsGenericTyp returns FALSE

China☆狼群 提交于 2019-12-06 12:57:57
Can someone explain this? Per documentation IsGenericType indicatesing whether the current Type represents a type parameter in the definition of a generic type or method. So this (LINQPad) code: bool bStraight = typeof(Task).IsGenericType; bStraight.Dump("typeof(Task).IsGenericType"); works as expected and yields the output: typeof(Task).IsGenericType False But when I retrieve it from a method by reflection: public class MyClass { public async Task Method() { await Task.Run(() => { Thread.Sleep(3000); }); } } public async Task TEST() { MyClass theObject = new MyClass(); Task task = (Task

Have multiple calls wait on the same internal async task

好久不见. 提交于 2019-12-06 12:17:05
(Note: this is an over-simplified scenario to demonstrate my coding issue.) I have the following class interface: public class CustomerService { Task<IEnumerable<Customer>> FindCustomersInArea(String areaName); Task<Customer> GetCustomerByName(String name); : } This is the client-side of a RESTful API which loads a list of Customer objects from the server then exposes methods that allows client code to consume and work against that list. Both of these methods work against the internal list of Customers retrieved from the server as follows: private Task<IEnumerable<Customer>> LoadCustomersAsync

Equivalent of Task.FromResult() from .NET 4.5 in .NET 4.0

心不动则不痛 提交于 2019-12-06 12:07:38
问题 I cant find information about retargeting my code from .NET 4.5 to 4.0. I have to install this application on Windows XP. my code in .NET 4.5 public async Task <IXLWorksheet> ImportFile(string fileToImport) { ... return await Task.FromResult<IXLWorksheet>(Sheet1) } In .NET 4.0 method FromResult does not exist. Someone knows how it should looks in .NET 4.0?? 回答1: You're returning the awaited result of a task, which is constructed on a result. The solution is rather simple - drop the await :

TaskCanceledException when not awaiting

爷,独闯天下 提交于 2019-12-06 11:53:30
I seem to be getting a TaskCanceledException whenever I return another Task synchronously instead of awaiting it, following the guidelines in When at last you await . TaskCanceledException code public static class Download { public static Task<byte[]> FromYouTubeAsync(string videoUri) { using (var client = new HttpClient()) { return FromYouTubeAsync( () => client .GetStringAsync(videoUri), uri => client .GetByteArrayAsync(uri)); } } public async static Task<byte[]> FromYouTubeAsync( Func<Task<string>> sourceFactory, Func<string, Task<byte[]>> downloadFactory) { string source = await //

How to kill an 3rd-app programmatically?

試著忘記壹切 提交于 2019-12-06 11:46:49
How to kill an app in android? killPackageProcesses(services.get(i).baseActivity.getPackageName()); this is the function killPackageProcesses public void killPackageProcesses(String packagename) { int pid = 0; ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> pids = am .getRunningAppProcesses(); for (int i = 0; i < pids.size(); i++) { ActivityManager.RunningAppProcessInfo info = pids.get(i); if (info.processName.equalsIgnoreCase(packagename)) { pid = info.pid; } } android.os.Process.killProcess(pid); } It doesn't work

How can I not include a build task when I include a project in my settings.gradle file?

↘锁芯ラ 提交于 2019-12-06 11:25:05
My settings.gradle file looks like: include "serverTest", "shared" And the serverTest build.gradle file looks like: group = 'gradle' version = '1.0' defaultTasks 'build' apply plugin: 'java' apply plugin: 'eclipse' sourceCompatibility = JavaVersion.VERSION_1_6 dependencies { compile project(':shared') } The directory structure is: The top level holds the settings.gradle file and it has folders shared and serverTest in it. Then in the serverTest directory there is the build.gradle file. When I run gradle at the top level it outputs: :shared:compileJava UP-TO-DATE :shared:processResources UP-TO

How to add an ItemList to a default ItemDefinitionGroup metadata in MSBuild?

自古美人都是妖i 提交于 2019-12-06 11:13:50
I'm trying to add all the files in a given directory to the ClCompile metadata's ForcedUsingFiles parameter. I'm using the following code: <ItemGroup> <ForcedUsingFilesList Include="c:\path\to\files\*" /> </ItemGroup> <ItemDefinitionGroup> <ClCompile> <ForcedUsingFiles>@(ForcedUsingFilesList)</ForcedUsingFiles> </ClCompile> </ItemDefinitionGroup> But I'm getting the error The value "@(ForcedUsingFilesList)" of metadata "ForcedUsingFiles" contains an item list expression. Item list expressions are not allowed on default metadata values. Any idea how I can work around this error? Thanks Ah,

Asynchronous method in a C# class that executes a process

倾然丶 夕夏残阳落幕 提交于 2019-12-06 10:41:16
I have a follow-up question to this post . In my version, I have the following I want to make asynchronous. Here is what I have: public virtual Task<bool> ExecuteAsync() { var tcs = new TaskCompletionSource<bool>(); string exe = Spec.GetExecutablePath(); string args = string.Format("--input1={0} --input2={1}", Input1, Input2); try { var process = new Process { EnableRaisingEvents = true, StartInfo = { UseShellExecute = false, FileName = exe, Arguments = args, RedirectStandardOutput = true, RedirectStandardError = true, WorkingDir = CaseDir } }; process.Exited += (sender, arguments) => { if

iPhone background task stops running at some point

折月煮酒 提交于 2019-12-06 09:53:33
问题 In my iPhone app I have a background task that starts running when the app enters the background state. The task runs fine and is structured in a loop like this: run. sleep for 5 min. run. sleep for 5 min. etc. For some reason, the task stops running after a certain amount of time... say half an hour to an hour. Can anyone help me understand why? Here is the background task code: - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"Application entered background state.