C # winforms: is my GUI and logic separation doing right?
Edit
Based on the suggestions below for sending logical code GUI delegates, I came up with a code like this:
Action ClearFunction = new Action(()=>Invoke(new Action(delegate() { ResultsBox.Clear(); } ) ));
Can you shorten this?
Here is a portion of my Windows Forms program.
When I started converting the code to use a different thread, it started to feel very brutal when I spawned threads and created public methods wrapped in delegates so that the boolean code could actually use the GUI.
Please provide suggestions for better idioms or architecture improvements. Thanks.
// 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();
+1
a source to share