I initiate a search, then display the results of that search in the form.
If I use .Show()
the form freezes. If I use .ShowDialog()
the form shows up, but the search does not complete as control is not returned to the thread until the form is closed.
The .ShowDialog()
way works on the 2nd call once the form is initialized because the initialize method calls the else activate code. But I'd like this to work on the first call.
Code is as follows.
public static void Search(string searchstring)
{
IntializeSearchResultsForm()
List<searchitem> templist = searchmethod(searchstring);
SearchForm.Invoke((MethodInvoker) (() => SearchForm.SetSearchResultsData(tempList)));
}
public static void IntializeSearchResultsForm()
{
if (SearchForm == null)
{
SearchForm = new SearchForm();
SearchForm.Show(); OR SearchForm.ShowDialog();
}
else
{
SearchForm.Invoke(new MethodInvoker(SearchForm.Activate));
}
}
UPDATE with more code details:
public static List<PricerSearchResultEntry> searchmethod(string dealID)
{
List<PricerSearchResultEntry> tempResultsList = new List<PricerSearchResultEntry>();
foreach (String dir in pricerFolderArray)
{
if (Directory.Exists(dir))
{
string[] filesList = Directory.GetFiles(dir, "*" + dealID + "*");
foreach (String file in filesList)
{
if (AppContext.SearchPricersForm.PricersCheckBox)
{
if (file.Contains("pricer") && !file.Contains("Failed") && !file.Contains("Incomplete"))
{
tempResultsList.Add(ParseFileString(file));
}
}
}
}
}
tempResultsList.Sort((x, y) => y.ValuationDate.CompareTo(x.ValuationDate));
return tempResultsList;
}
public SearchForm()
{
InitializeComponent();
searchResultsListBox.DisplayMember = "Title";
searchResultsListBox.ValueMember = "DealID";
searchResultsListBox.DataSource = searchResultsList;
}