Edit
Based on suggestions below to send the logic code GUI delegates, I came up with code like this:
Action ClearFunction = new Action(()=>Invoke(new Action(delegate() { ResultsBox.Clear(); } ) ));
Is it possible to shorten this?
Here is part of my C# windows forms program.
When I started converting the code to use another thread, it started to feel very crufty as I spawned threads and created public methods wrapped in delegates so the logic code could actually use the GUI.
Please give suggestions on better idioms or improvements to the architecture. Thank you.
// form1.cs
public void ClearResultsBox()
{
ResultsBox.Clear();
}
public void PrintResults(string s)
{
ResultsBox.AppendText(s);
}
private void SearchButton_Click(object sender, EventArgs e)
{
var t = new Thread(() => SearchCore.Execute(DirectoryBox.Text, SearchBox.Text, this));
t.Start();
}
// logic.cs
class SearchCore
{
delegate void ClearFunction();
delegate void AppendFunction(string a);
static ClearFunction clear;
static AppendFunction print;
public static void Execute(string path, string searchterm, MainForm form)
{
clear = new ClearFunction(() => form.Invoke(new ClearFunction(form.ClearResultsBox)));
print = new AppendFunction(s => form.Invoke(new AppendFunction(form.PrintResults), s));
clear();