I know there are lots of topics on this issues. I looked at them, as well as at Microsoft's website, but I still don't know how to apply possible solutions to my case. So, I have a Windows Forms Desktop app that is supposed to read some text files and then print results to the app interface. I've got this function call after I choose an item from a drop down list in a Combobox:
private async void infosFolderCbx_SelectionChangeCommitted(object sender, EventArgs e)
{
progressBar1.Visible = true;
progressBar1.Style = ProgressBarStyle.Marquee;
await Task.Run(() => UpdateQuestOrInfoFilesList(infosFolderCbx.SelectedItem.ToString(), infoFoldersHistoryList));infoFoldersHistoryList);
progressBar1.Visible = false;
}
And it calls this function:
private void UpdateQuestOrInfoFilesList(string folderWithFiles, List<string> list)
{
if (Directory.Exists(folderWithFiles) && (list == questFoldersHistoryList || list == infoFoldersHistoryList))
{
int maxHistoryItems = 5;
ListView listview; ComboBox cbx; string fileExtension;
if (list == questFoldersHistoryList) { listview = questFilesList; cbx = questFolderCbx; fileExtension = questFileExtension; }
else /* if (list == infoFoldersHistoryList) */ { listview = infoFilesList; cbx = infosFolderCbx; fileExtension = infoFileExtension; }
//This part updates history lists
if (list.Contains(folderWithFiles)) { list.Remove(folderWithFiles); }
list.Insert(0, folderWithFiles);
if (list.Count > maxHistoryItems) { list.RemoveRange(maxHistoryItems, list.Count - maxHistoryItems); }
cbx.Items.Clear();
cbx.Items.AddRange(list.ToArray());
if (list == questFoldersHistoryList) { cbx.Text = questFilesFolder = folderWithFiles; }
else /* if (list == infoFoldersHistoryList) */ { cbx.Text = infoFilesFolder = folderWithFiles; }
//This part updates files shown in the corresponding window
string[] files = Directory.GetFiles(folderWithFiles);
listview.Items.Clear();
foreach (string file in files)
{
//string textToAdd = file; //This add to the list the whole file path
//string textToAdd = Path.GetFileName(file); //This adds only the file name
string textToAdd = Path.GetFileName(file).Replace(fileExtension, ""); //This adds only the base file name without: _info_G3_World_01.info
listview.Items.Add(textToAdd);
if (list == questFoldersHistoryList) { LoadQuestToQuestDict(textToAdd); }
else /* if (list == infoFoldersHistoryList) */ { LoadInfoToInfosDict(textToAdd); }
}
//LookForDialogs();
infoDataTextBox.Text = string.Join(Environment.NewLine, dictInfoFiles.Keys.ToArray());
}
else { MessageBox.Show("Invalid List<string> list in UpdateQuestOrInfoFilesList method"); }
}
Now, it seems I'm getting this error
Cross-thread operation not valid. Control "" accessed from a thread other than the thread it was created on.
on
infosFolderCbx.SelectedItem.
I have tried some solutions, most notably the one from the Microsoft's webpage, but apparently I'm not able to apply to my case correctly. Any ideas on how to solve this issue? Or is there a better way to actually make a working await function, because my overall goal is just to allow the program to get the data from the text file, show the progress bar while it's doing that and then hide the bar again after it has finished. So maybe there is another way of doing, as the solutions that I read seem rather complicated for such a trivial task.