Passing data from external dll using streams

I have a common comms library that I wrote to communicate with our hardware over TCP / IP

The shared library allows both the client application and our engineering tool to share common code. The shared library starts threads that are supposed to post data back to forms.

I have working code, but I believe there should be an easier way to do this.

This is a simplified version of what I have ...

namespace EngineerTool
{

    public delegate void DelegateSearchFinished();

    public partial class MainForm : Form
    {
        public DelegateSearchFinished d_SearchFinished;
        Discover discovery;

        public MainForm()
        {
            InitializeComponent();
            discovery = new Discover(this.FinishedSearchInvoke);
            d_SearchFinished = new DelegateSearchFinished(this.FinishedSearch);
            discovery.Start();
        }

        public void FinishedSearchInvoke()
        {
            this.Invoke(this.d_SearchFinished, new Object[] {});
        }

        public void FinishedSearch()
        {
            // Search has finished here!
        }
    }
}

namespace DiscoveryTool
{
    public delegate void DelegateSearchFinished();

    public class Discover
    {
        DelegateSearchFinished _callFinished;

        public Discover(DelegateSearchFinished callFinished)
        {
            _callFinished = callFinished;
        }

        public void Start()
        {
            // starts thread and stuff
        }

        public void ThreadWorker()
        {



            _callFinished();
        }

    }
}

      

0


a source to share


3 answers


How about creating an event in the Discover class and in the form of subscribing to the event, instead of passing delegates.



0


a source


Take a look at the BackgroundWorker class:



BackgroundWorker Components Overview

0


a source


Let's say you are doing a search that will lead to an object List<Customer>

. One fairly simple and straightforward approach is to raise an event when doing a search using a custom EventArgs class that carries the result:

public class CustomerSearchEventArgs : EventArgs
{
    public List<Customer> Customers { get; private set; }
    public CustomerSearchEventArgs(List<Customer> customers)
    {
        Customers = customers;
    }
}

      

... in the search class:

// the parameter is there to conform to the signature of the WaitDelegate
// used when invoking the method using the ThreadPool
public void Search(object state)
{    
    List<Customer> result = new List<Customer>();
    // perform the search, populate the result list
    // call a method to raise the event
    OnSearchFinished(new CustomerSearchEventArgs(result));
}

protected void OnSearchFinished(CustomerSearchEventArgs e)
{
    EventHandler<CustomerSearchEventArgs> searchFinished = this.SearchFinished;
    if (searchFinished != null)
    {
        searchFinished(this, e);
    }
}

      

... and in the form:

private delegate void CustomerListHandler(List<Customer> customers);

private void StartSearch()
{
    CustomerSearch search = new CustomerSearch();
    search.SearchFinished += new EventHandler<CustomerSearchEventArgs>(Search_SearchFinished);
    ThreadPool.QueueUserWorkItem(search.Search);
}
private void Search_SearchFinished(object sender, CustomerSearchEventArgs e)
{
    SearchFinished(e.Customers);
}

private void SearchFinished(List<Customer> list)
{
    if (this.InvokeRequired)
    {
        // the method is NOT executing on the UI thread; we
        // need to invoke it on the right thread
        this.Invoke(new CustomerListHandler(SearchFinished), list);
    }
    else
    {
        lstCustomers.Items.Clear();
        lstCustomers.DisplayMember = "Name";
        lstCustomers.Items.AddRange(list.ToArray());
    }
}

      

0


a source







All Articles