Last active
December 12, 2015 06:09
-
-
Save anujb/4727142 to your computer and use it in GitHub Desktop.
Task Threading and Memory Management
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyViewController : UIViewController { | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
for (int i = 0; i < Int32.MaxValue; i++) { | |
DoStuffAsync(); | |
} | |
} | |
public void DoStuffAsync() { | |
Task.Factory.StartNew<string> (() => { | |
using (var client = new WebClient()) { | |
var response = client.DownloadString (new Uri ("http://www.google.com")); | |
return response; | |
} | |
}).ContinueWith ((t) => { | |
Console.WriteLine(t); | |
this.DoSomethingElse(); | |
});; | |
} | |
public void DoSomethingElse() { | |
Console.WriteLine ("I'm doing something else now..."); | |
} | |
public void DoStuffAsyncWeak() { | |
var weakViewController = new WeakReference (this); | |
Task.Factory.StartNew<string> (() => { | |
using (var client = new WebClient()) { | |
var response = client.DownloadString (new Uri ("http://www.google.com")); | |
return response; | |
} | |
}).ContinueWith ((t) => { | |
Console.WriteLine(t); | |
var strongViewController = weakViewController.Target as MyViewController; | |
if (strongViewController == null) | |
return; | |
strongViewController.DoSomethingElse(); | |
});; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment