如何从库类开始 [英] how to start from the libraryclass

查看:96
本文介绍了如何从库类开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我有一个库可以从某些设备接收数据.我想启动winform以使用设备中的数据.但是Winform在接收到数据后消失了.

hello! i have a library to receive data from some device.and want to start winform to use the data from device. but the winform disappear after the data was received.

namespace Sample
{
    [Guid("66F485D2-B7D9-451d-960F-43CE8B843FC1")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("CS.Sample1")]
    public class NotifyClass : XXX.receive.IDataReceiver
    {
        void XXX.receive.IDataReceiver.Notify(string test, object data)
        {
            DeviceRequest devrequest = new DeviceRequest();
            devrequest.Initialize(data);
            //why the form2 always disappear????
            Form2 f2 = new Form2();    
            f2.Show();
            //I want to start form2 and use data received from the device
        }
       
        string XXX.receive.IDataReceiver.test
        {
            get
            {
                return "tutorial::example2_category";
            }
        }
    }
}


推荐答案

我猜想NotifyClass在收到数据后就被清理了.如果不是,则该方法仅结束,并且无论如何f2都将超出范围,也将被删除.尝试在另一个级别声明f2并将其传递给NotifyClass.然后它将被使用,而不是被拥有,并且不再消失.

将处理代码以某种形式放置并不是真正的最佳实践,因为它看起来很像魔术按钮模式.最好有一个表单(也许有一些额外的方法可以轻松地更新进度和一些消息),然后在处理方法中使用它.像这样的东西:

I guess the NotifyClass gets cleaned up after the data is received. If not, then the method simply ends and f2 would be out of scope anyway and also deleted. Try declare f2 at another level and pass it to the NotifyClass. It would then be used instead of owned and would not disappear anymore.

It isn''t really best practice to put the code for processing in a form because it looks a lot like the magic pushbutton pattern. It would be much better to have a form (maybe with some extra methods for easily updating progress and some message) and use that in the method for processing. something like this:

public void processData(string data)
{
  Form2 f2 = new Form2();
  try
  {
    f2.Message = "Step 1";
    processStep1(data);
    f2.Message = "Step 2";
    processStep2(data);
    f2.Message = "Step 3";
    processStep3(data);
  finally
  {
    f2.Close();
    MessageBox.Show("completed");
  }
}



上面的代码是一个实现的粗略示例,但希望能给您一个想法.

祝你好运!



Above code is a crude example of an implementation but hopefully gives you an idea.

Good luck!


这篇关于如何从库类开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆