I have a csv file which contains about 50000 place details. I want to list all the places in a combo box. But it cant be loaded. App getting hang. how to fix it?
private async void LoadDataAsync()
{
try
{
string filePath = @"\assets\places.csv";
// Start a new task to load data from CSV file asynchronously
var placesTask = Task.Run(() => LoadDataFromCSVAsync(filePath));
// Wait for the task to complete and get the result
var places = await placesTask;
// Set the data to the ComboBox once loading is complete
cboPlace.ItemsSource = places;
cboPlace.DisplayMemberPath = "Name";
}
catch (Exception ex)
{
// Handle exception
MessageBox.Show("Error loading data: " + ex.Message);
}
}
private async Task<List<Place>> LoadDataFromCSVAsync(string filePath)
{
List<Place> places = new List<Place>();
using (StreamReader reader = new StreamReader(filePath))
{
// Skip header line
await reader.ReadLineAsync();
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
string[] parts = line.Split(',');
if (parts.Length == 5)
{
string name = parts[0].Trim();
double latitude, longitude;
if (double.TryParse(parts[1].Trim(), out latitude) && double.TryParse(parts[2].Trim(), out longitude))
{
string country = parts[3].Trim();
string state = parts[4].Trim();
places.Add(new Place { Name = name, Latitude = latitude, Longitude = longitude, Country = country, State = state });
}
}
}
}
return places;
}