【问题标题】:Passing data back from external dll with threads使用线程从外部 dll 传回数据
【发布时间】:2009-05-13 14:09:10
【问题描述】:

我有一个通用的通信库,我已经编写了它来通过 TCP/IP 与我们的硬件进行通信

公共库允许客户端应用程序和我们的工程师工具共享公共代码。 公共库运行需要将数据返回到表单的线程。

我有一些工作代码,但我觉得必须有更简单的方法来做到这一点。

这是我所拥有的简化版本...

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();
        }

    }
}

【问题讨论】:

    标签: c# multithreading dll delegates


    【解决方案1】:

    如果在 Discover 类上创建一个事件,Form 订阅该事件,而不是传递委托。

    【讨论】:

    • 是的...您根据实例化对象进行订阅。如果您需要知道“谁”处理了工作,可以在事件 args 中发回线程标识符。
    【解决方案2】:

    看看BackgroundWorker类:​​

    BackgroundWorker Component Overview

    【讨论】:

    • 我研究了 BackgroundWorker,但它不够灵活,所以我写了自己的线程...
    【解决方案3】:

    假设您正在执行将产生List<Customer> 对象的搜索。然后,一种相当直接且简单的方法是在搜索完成时引发一个事件,使用携带结果的特殊 EventArgs 类:

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

    ...在搜索类中:

    // 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);
        }
    }
    

    ...形式为:

    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());
        }
    }
    

    【讨论】:

    • 我设法做到了,而不必拥有 EventArgs 类。我有:公共代表无效NegotiateStatus(字符串状态);公共事件 NegotiateStatus OnNegotiateStatus;然后我可以调用 OnNegotiateStatus("string") 然后在我的表单中我只有 conNeg.OnNegotiateStatus += new NegotiateConnection.NegotiateStatus(this.NegotiateStatus);
    猜你喜欢
    • 2017-02-16
    • 2011-05-12
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多