This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
/
MainScreen_iPhone.cs
93 lines (76 loc) · 3 KB
/
MainScreen_iPhone.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Threading.Tasks;
using MultiThreading.Controls;
namespace MultiThreading {
public partial class MainScreen_iPhone : UIViewController {
protected LoadingOverlay _loadPop = null;
public MainScreen_iPhone () : base ("MainScreen_iPhone", null)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// this example uses the Task.Factory class to spin up a new task (thread)
// Factory is nice because it allows you to use the ContinueWith method so
// you can specify code to execute after the first task is complete.
this.StartBackgroundTaskButton.TouchUpInside += (sender, e) => {
// show the loading overlay on the UI thread
this._loadPop = new LoadingOverlay (UIScreen.MainScreen.Bounds);
this.View.Add ( this._loadPop );
// spin up a new thread to do some long running work using StartNew
Task.Factory.StartNew (
// tasks allow you to use the lambda syntax to pass work
() => {
Console.WriteLine ( "Hello from taskA." );
LongRunningProcess(2);
}
// ContinueWith allows you to specify an action that runs after the previous thread
// completes
//
// By using TaskScheduler.FromCurrentSyncrhonizationContext, we can make sure that
// this task now runs on the original calling thread, in this case the UI thread
// so that any UI updates are safe. in this example, we want to hide our overlay,
// but we don't want to update the UI from a background thread.
).ContinueWith (
t => {
this._loadPop.Hide ();
Console.WriteLine ( "Finished, hiding our loading overlay from the UI thread." );
}, TaskScheduler.FromCurrentSynchronizationContext()
);
// Output a message from the original thread. note that this executes before
// the background thread has finished.
Console.WriteLine("Hello from the calling thread.");
};
// the simplest way to start background tasks is to use create a Task, assign
// some work to it, and call start.
this.StartBackgroundTaskNoUpdateButton.TouchUpInside += (sender, e) => {
var TaskA = new Task ( () => { LongRunningProcess (5); } );
var TaskB = new Task ( () => { LongRunningProcess (4); } );
TaskA.Start ();
TaskB.Start ();
};
}
/// <summary>
/// Simulation method to sit for a number of seconds.
/// </summary>
protected void LongRunningProcess (int seconds)
{
Console.WriteLine ( "Beginning Long Running Process {0}", seconds );
System.Threading.Thread.Sleep ( seconds * 1000 );
Console.WriteLine ( "Finished Long Running Process {0}", seconds );
}
public override void ViewDidUnload ()
{
base.ViewDidUnload ();
ReleaseDesignerOutlets ();
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
}
}
}