Is it safe for Task.Run() to access Method scope variables? - c#

Is the following guaranteed to print "Success," or is it possible garbage collection can eat the "dummyValue" object since TaskTest() ends long before the task it returns can finish?
public class DummyValueClass
{
public string Value { get; private set; }
public DummyValueClass(string value)
{
Value = value;
}
}
public class ScopeTest
{
public Task<string> TaskTest()
{
var dummyValue = new DummyValueClass("Success");
return Task.Run(() =>
{
Thread.Sleep(10000);
return dummyValue?.Value;
});
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Beginning Test");
var scopeTester = new ScopeTest();
var task = scopeTester.TaskTest();
Console.WriteLine(task.Result);
}
}

This is called a closure.
var dummyValue = new DummyValueClass("Success");
return Task.Run(() =>
{
Thread.Sleep(10000);
return dummyValue?.Value;
});
In short the compiler creates a compiler generate class and captures dummyValue as an instance member.
Example of sanitized emitted code. You can view all the gory details here
public class ScopeTest
{
[CompilerGenerated]
private sealed class GeneratedClass
{
public DummyValueClass dummyValue;
internal string InternalTaskTest()
{
Thread.Sleep(10000);
DummyValueClass dummyValueClass = dummyValue;
if (dummyValueClass == null)
{
return null;
}
return dummyValueClass.Value;
}
}
public Task<string> TaskTest()
{
GeneratedClass generatedClass = new GeneratedClass();
generatedClass.dummyValue = new DummyValueClass("Success");
return Task.Run(new Func<string>(generatedClass.InternalTaskTest));
}
}
The task now has a method pointer to the generated method, which in turn roots the GeneratedClass which has a reference to your DummyValueClass. The result being that the Garbage Collector knows your reference is still alive.

Yes, it is guaranteed to print "Success". Because when a lambda captures value outside from its scope, the compiler will generate an object which "captures" the variables acquired from outside its scope. So it will not be garbage collected

Related

given a Task instance, can I tell if ContinueWith has been called on it?

Given a Task instance, how can I tell if ContinueWith has been called on it? I want to know if I'm the last task executing in a chain.
Task task = Task.FromResult();
void SomeMethod(var x) {
task = task.ContinueWith(previous => {
if (task.ContinueWith is called) return;
// do something with x...
}
}
If you meant multiple continuations. A possible solution may be like this.
class Program
{
public class TaskState
{
public bool Ended { get; set; }
}
static void Main(string[] args)
{
var task = Task.FromResult("Stackoverflow");
var state = new TaskState();
task.ContinueWith((result, continuationState) =>
{
Console.WriteLine("in first");
}, state).ContinueWith((result, continuationState) =>
{
if (!state.Ended)
Console.WriteLine("in second");
state.Ended = true;
}, state).ContinueWith((result, continuationState) =>
{
if (!state.Ended)
Console.WriteLine("in third");
state.Ended = true;
}, state);
Console.ReadLine();
}
}
You can have a static variable (a dictionary object) declared on the parent and update it with unique keyvalues when your Tasks are triggered. You can monitor this static variable to see if all the other threads has completed the execution or not.

Threading With List Property

public static class People
{
List<string> names {get; set;}
}
public class Threading
{
public static async Task DoSomething()
{
var t1 = new Task1("bob");
var t2 = new Task1("erin");
await Task.WhenAll(t1,t2);
}
private static async Task Task1(string name)
{
await Task.Run(() =>
{
if(People.names == null) People.names = new List<string>();
Peoples.names.Add(name);
}
}
}
Is that dangerous to initialize a list within a thread? Is it possible that both threads could initialize the list and remove one of the names?
So I was thinking of three options:
Leave it like this since it is simple - only if it is safe though
Do same code but use a concurrentBag - I know thread safe but is initialize safe
Using [DataMember(EmitDefaultValue = new List())] and then just do .Add in Task1 and not worry about initializing. But the only con to this is sometimes the list wont need to be used at all and it seems like a waste to initialize it everytime.
Okay so what I figured worked best for my case was I used a lock statement.
public class Class1
{
private static Object thisLock = new Object();
private static async Task Task1(string name)
{
await Task.Run(() =>
{
AddToList(name);
}
}
private static AddToList(string name)
{
lock(thisLock)
{
if(People.names == null) People.names = new List<string>();
People.names.Add(name);
}
}
}
public static class People
{
public static List<string> names {get; set;}
}
for a simple case like this the easiest way to get thread-safety is using the lock statement:
public static class People
{
static List<string> _names = new List<string>();
public static void AddName(string name)
{
lock (_names)
{
_names.Add(name);
}
}
public static IEnumerable<string> GetNames()
{
lock(_names)
{
return _names.ToArray();
}
}
}
public class Threading
{
public static async Task DoSomething()
{
var t1 = new Task1("bob");
var t2 = new Task1("erin");
await Task.WhenAll(t1,t2);
}
private static async Task Task1(string name)
{
People.AddName(name);
}
}
of course it's not very usefull (why not just add without the threads) - but I hope you get the idea.
If you don't use some kind of lock and concurrently read and write to a List you will most likely get an InvalidOperationException saying the collection has changed during read.
Because you don't really know when a user will use the collection you might return the easiest way to get thread-saftey is copying the collection into an array and returning this.
If this is not practical (collection to large, ..) you have to use the classes in System.Collections.Concurrrent for example the BlockingCollection but those are a bit more involved.

Cannot return IObservable<T> from a method marked async

This contrived example is roughly how my code is structured:
public abstract class SuperHeroBase
{
protected SuperHeroBase() { }
public async Task<CrimeFightingResult> FightCrimeAsync()
{
var result = new CrimeFightingResult();
result.State = CrimeFightingStates.Fighting;
try
{
await FightCrimeOverride(results);
}
catch
{
SetError(results);
}
if (result.State == CrimeFightingStates.Fighting)
result.State = CrimeFightingStates.GoodGuyWon;
return result;
}
protected SetError(CrimeFightingResult results)
{
result.State = CrimeFightingStates.BadGuyWon;
}
protected abstract Task FightCrimeOverride(CrimeFightingResult results);
}
public enum CrimeFightingStates
{
NotStarted,
Fighting,
GoodGuyWon, // success state
BadGuyWon // error state
}
public class CrimeFightingResult
{
internal class CrimeFightingResult() { }
public CrimeFightingStates State { get; internal set; }
}
Now I'm trying to build a collection that would hold multiple SuperHero objects and offer a AllHerosFightCrime method. The hero's should not all fight at once (the next one starts when the first is finished).
public class SuperHeroCollection : ObservableCollection<SuperHeroBase>
{
public SuperHeroCollection() { }
// I mark the method async...
public async IObservable<CrimeFightingResult> AllHerosFightCrime()
{
var heros = new List<SuperHeroBase>(this);
var results = new ReplaySubject<CrimeFightingResult>();
foreach (var hero in heros)
{
// ... so I can await on FightCrimeAsync and push
// the result to the subject when done
var result = await hero.FightCrimeAsync();
results.OnNext(result);
}
results.OnCompleted();
// I can't return the IObservable here because the method is marked Async.
// It expects a return type of CrimeFightingResult
return results;
}
}
How can I return the IObservable<CrimeFightingResults> and still have the call to FightCrimeAsync awaited?
You could turn your task into an observable and combine them using Merge:
public IObservable<CrimeFightingResult> AllHerosFightCrime()
{
var heros = new List<SuperHeroBase>(this);
return heros.Select(h => h.FightCrimeAsync().ToObservable())
.Merge();
}
If you want to maintain the order your events are received you can use Concat instead of Merge.

Setting a class property on a long running thread

I am populating some class properties,
One of them involves serializing an Entity structure to a byte[] this takes some time so I wanted to do it in a thread.
The value never gets set as I assume the class and thread are now out of scope.
The code is below, any ideas would be appreciated
public class classA
{
public void DoSomething()
{
var classC = new ClassB().DoSomethingElse();
//SAVE CLASS c to database
var serialized = classC.GetSerializedDataTable(); // is always null unless i take out the task from class c
}
}
public class ClassB
{
public ClassC DoSomethingElse()
{
var classC = new ClassC();
classC.DataTableValue = new DataTable();
classC.SerializeToByteArray();
return classC;
}
}
public class ClassC
{
public DataTable DataTableValue { get; set; }
private byte[] serializedData;
public void SerializedDataTable()
{
new Task(() => this.serializedData = this.DataTableValue.SerializeToByteArray()).Start();
}
public byte[] GetSerializedDataTable()
{
return this.serializedData;
}
}
A Task is not just meant to be used to run code on another thread, it represents a logical unit of work that can return something once it is complete.
Your ClassC.GetSerializedDataTable() appears to be a perfect place to make use of a Task<byte[]> return type:
public class ClassC
{
public DataTable DataTableValue { get; set; }
private Task<byte[]> serializeDataTask;
public void SerializeDataTable()
{
serializeDataTask = Task.Factory.StartNew( () => this.DataTableValue.SerializeToByteArray() );
}
public Task<byte[]> GetSerializedDataTable()
{
// You can either throw or start it lazily if SerializeDataTable hasnt been called yet.
if ( serializeDataTask == null )
throw new InvalidOperationException();
return serializeDataTask;
}
}
And now your client code can utilize the Task return type in intelligent ways. If the result is already available, it can consume it immediately via Task.Result. Otherwise it can wait for it to complete, or perform some other work until it completes. The point is the calling code now has the context to take the most appropriate course of action.
Back to your example:
public void DoSomething()
{
var classC = new ClassB().DoSomethingElse();
//SAVE CLASS c to database
var serializeTask = classC.GetSerializedDataTable();
// will obtain result if available, will block current thread and wait for serialized data if task still running.
var serializedData = serializeTask.Result;
}

Ref in async Task

How I can to pass a reference as a parameter to Async method in Windows Store App ? I'm looking for something like this:
var a = DoThis(ref obj.value);
public async Task DoThis(ref int value)
{
value = 10;
}
But error:
Async methods cannot have ref or out parameters
Has any another way?
Note:I need to pass exactly obj.value. This method would be used by different types of objects, by same type of objects, by one object, but I will pass obj.val_1, obj.val_2 or obj.val_10. All values will be same type (for ex string)
If you don't care about a little overhead and possibly prolonged lifetime of your objects, you could emulate the ref behavior by passing a setter and a getter method to the function, like this:
public async Task DoStuff(Func<int> getter, Action<int> setter)
{
var value1 = getter();
await DoSomeOtherAsyncStuff();
setter(value1 * value1);
}
And call it like this:
await DoStuff(() => obj.Value, x => obj.Value = x);
You could directly pass the object itself and set the value of the corresponding property inside the method:
var a = DoThis(obj);
public async Task DoThis(SomeObject o)
{
o.value = 10;
}
And if you do not have such object simply write one and have the async method take that object as parameter:
public class SomeObject
{
public int Value { get; set; }
}
You can always use the Task<> class and return the desired value. Then Your code would look something like:
var a = DoThis(obj.value);
obj.value = a.Result;
public async Task<int> DoThis(int value)
{
int result = value + 10; //compute the resulting value
return result;
}
EDIT
Ok, the other way to go with this that I can think of is encapsulating the update of the given object's member in a method and then passing an action invoking this method as the task's argument, like so:
var a = DoThis(() => ChangeValue(ref obj.value));
public void ChangeValue(ref int val)
{
val = 10;
}
public async Task DoThis(Action act)
{
var t = new Task(act);
t.Start();
await t;
}
As far as I tested it the change was made in the child thread, but still it's effect was visible in the parent thread. Hope this helps You.
You can't do this as you have it (as you know). So, a few work arounds:
You can do this by passing the initial object since it will be a reference type
var a = DoThis(obj);
public async Task DoThis(object obj) //replace the object with the actual object type
{
obj.value = 10;
}
EDIT
Based upon your comments, create an interface and have your classes implement it (providing it's always the same type you want to pass). Then you can pass the interface which is shared (maybe over kill, depends on your needs, or even unrealistic amount of work).
Or, provide a base class with the property! (I don't like this suggestion but since you're asking for something which can't be done it may suffice although I don't recommend it).
An example of the interface is here (not using what you have, but close enough using a Colsone App)
using System;
namespace InterfacesReferenceTypes
{
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
DoThis(mc);
Console.WriteLine(mc.Number);
Console.ReadKey();
}
static void DoThis(IDemo id)
{
id.Number = 10;
}
}
class MyClass : IDemo
{
//other props and methods etc
public int Number { get; set; }
}
interface IDemo
{
int Number { get; set; }
}
}
EDIT2
After next comments, you will have to still use an interface, but re assign the value afterwards. I'm sure there is a better way to do this, but this works:
using System.Text;
namespace InterfacesRerefenceTypes
{
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
Console.WriteLine(mc.Number);
mc.val1 = 3;
mc.val2 = 5;
mc.Number = mc.val2;
DoThis(mc);
mc.val2 = mc.Number;
Console.WriteLine(mc.val2);
Console.ReadKey();
}
static void DoThis(IDemo id)
{
id.Number = 15;
}
}
class MyClass : IDemo
{
public int val1 { get; set; }
public int val2 { get; set; }
public int Number { get; set; }
}
interface IDemo
{
int Number { get; set; }
}
}

Categories

Resources