[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
<Window x:Class="CSharp5Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Button Click="Button_Click"/> </Window>
using System; using System.Windows; namespace CSharp5Test { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { heavyLogic(); Title = "now : " + DateTime.Now; } private void heavyLogic() { System.Threading.Thread.Sleep(3000); } } }
using System; using System.Threading.Tasks; using System.Windows; namespace CSharp5Test { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void Button_Click(object sender, RoutedEventArgs e) { await heavyLogicTaskAsync(); Title = "now : " + DateTime.Now; } private Task heavyLogicTaskAsync() { return Task.Run(() => heavyLogic()); } private void heavyLogic() { System.Threading.Thread.Sleep(3000); } } }
using System; using System.Threading.Tasks; using System.Windows; namespace CSharp5Test { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void Button_Click(object sender, RoutedEventArgs e) { writeThreadId(); await heavyLogicTaskAsync(); Title = "now : " + DateTime.Now; writeThreadId(); } private Task heavyLogicTaskAsync() { return Task.Run(() => heavyLogic()); } private void heavyLogic() { writeThreadId(); System.Threading.Thread.Sleep(3000); } private static void writeThreadId() { Console.WriteLine( System.Threading.Thread.CurrentThread.ManagedThreadId ); } } }
using System; using System.Threading.Tasks; using System.Windows; namespace CSharp5Test { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void Button_Click(object sender, RoutedEventArgs e) { DateTime now = await heavyLogicTaskAsync(); Title = "now : " + now; } private Task<DateTime> heavyLogicTaskAsync() { return Task.Run(() => heavyLogic()); } private DateTime heavyLogic() { System.Threading.Thread.Sleep(3000); return DateTime.Now; } } }このプログラムは重い処理をして現在時刻を返すメソッドを呼びます。
using System; using System.Threading; using System.Threading.Tasks; namespace AwaitConsole { class Program { static void Main(string[] args) { execute(); Console.WriteLine("startSleeping: "); Thread.Sleep(3000); } private static async void execute() { await Task.Run(() => heavyLogic()); Console.WriteLine("heavy logic completed:"); } private static void heavyLogic() { Console.WriteLine("heavy logic thread:"); Thread.Sleep(300); } } }
using System; using System.Threading; using System.Threading.Tasks; namespace AwaitConsole { class Program { static void Main(string[] args) { execute(); Console.WriteLine("startSleeping: " + getThreadId()); Thread.Sleep(3000); } private static async void execute() { await Task.Run(() => heavyLogic()); Console.WriteLine("heavy logic completed:" + getThreadId()); } private static void heavyLogic() { Console.WriteLine("heavy logic thread:" + getThreadId()); Thread.Sleep(300); } private static int getThreadId() { return Thread.CurrentThread.ManagedThreadId; } } }