views:

560

answers:

1

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();
+3  A: 

I wouldn't have a circular reference between the form and searchCore. why don't you make the search logic talk back to form through a callback? that way the search doesn't need to know about the form and is easier to test.

Ali Shafai
How do I make the search logic talk back through a callback? Isn't that what I'm doing with form.Invoke?
Unknown
I'd agree with Ali. I'd try and separate the functionality in your app so that the SearchCore class does not deal with the UI or at least not UI elements on form1. Instead of passing a Form parameter to the Execute method pass a delegate that can be called once the search is finished and allow the Form1 class to do any UI manipulations required.
sipwiz
Pass Delegates to those two functions into your SearchCore as parameters and using += register to them in your GUI. then you can call the delegates from your search.
Ali Shafai
@Ali, where should I put the delegate declarations? Also what do you mean by using += register.
Unknown
the cleaning up for the listbox is a gui concern, the search core should not be concerned. search core should get a parameter of type Action<string>: public static void Execute(string path, string searchterm, **Action<string> addResult** )and should call it: addResult(result);in the form you have a method AppendText(string searchResult)and pass it to the search as the parameter. ignore the +=, you don't need it.
Ali Shafai
@Ali, I am trying to turn them into delegates. However the code seems very long. Can you take a look at my edit and see if it can be made shorter? Thanks.
Unknown