Skip to content

Instantly share code, notes, and snippets.

@DevStarSJ
Created June 17, 2016 06:15
Show Gist options
  • Save DevStarSJ/bc53fc7c5d9da76405a2ea1e06e4cbb9 to your computer and use it in GitHub Desktop.
Save DevStarSJ/bc53fc7c5d9da76405a2ea1e06e4cbb9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FormTestUsingAsync
{
public partial class Form1 : Form
{
public DataManager DataManager = new DataManager();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show($"{DataManager.DataList[2]}");
}
}
public class DataManager
{
private List<int> _list = null;
public List<int> DataList
{
get
{
if (_list == null)
{
Init();
//InitAsync().Wait(); // Deadlock
}
return _list;
}
}
private void Init()
{
_list = Task.Run(async () =>
{
return await MakeList();
}).Result;
}
private async Task InitAsync()
{
_list = await MakeList();
}
private async Task<List<int>> MakeList()
{
await Task.Delay(1000);
return new List<int>() { 1, 2, 3, 4, 5 };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment