在类而不是Windows窗体中使用OpenNETCF.Net.Ftp [英] Using OpenNETCF.Net.Ftp inside a class instead of inside a Windows Form

查看:77
本文介绍了在类而不是Windows窗体中使用OpenNETCF.Net.Ftp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我正在Windows窗体内使用FTP对象.FTP对象在单独的线程中运行,因此,为确保我的应用程序不会冻结,我使用以下代码:

So far I am using an FTP object inside a Windows form. FTP object runs in a separate thread, so to ensure that my app doesn't freeze up, I use the following piece of code:

private void OnResponse(string response)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new StringDelegate(OnResponse), new object[] { response });
            return;
        }
    } //end of OnResponse

我不清楚什么是字符串委托,但是可以.

I am not completely clear on what a string delegate is, but this works.

但是,我现在正在重构,希望将ftp隐藏到一个类中.我的问题是如何确保主线程不会冻结?在线上有关引发类内部事件的所有参考都是有意义的,但是我还没有找到一个应用程序是多线程的示例.我最大的担心是InvokeRequired.

However, I am now refactoring and wish to hide the ftp into a class. My question is how do I make sure the main thread doesn't freeze? All the references online regarding raising events inside classes make sense, but I haven't found a single example where the application is multithreaded. My biggest concern would be InvokeRequired.

在上面的代码中,这是一个表单.如果我将ftp对象隐藏在如下所示的类中:

In the code above this is a form. If I hide the ftp object inside a class such as the following:

abstract class MyClass
{
    //data members
    private FTP _ftp;

    //other data members, methods, and properties etc
}

"This"成为MyClass的对象.我不确定InvokeRequired属性是否在类上实现(也许我应该使其实现具有该属性的特殊接口?).或者也许我缺少了某些东西,并且我不应该在类内部使用多线程对象?

"This" becomes an object of MyClass. I am not sure if InvokeRequired property is implemented on class (perhaps I should make it implement a special interface that has that property?). Or perhaps I am missing something and I am not supposed to use multithreaded objects inside classes?

推荐答案

您需要在UI线程上创建的Control或从Control派生的东西(不必是Form).您的MyClass可能不应该直接更新UI,因此在这里并不重要-MyClass可能引发事件或调用回调.

You need a Control or something derived from Control (doesn't have to be a Form) that was created on the UI thread. Your MyClass likely shouldn't be updating the UI directly, so it's not really relevant here - MyClass would probably raise an event or invoke a callback.

当您要基于来自FTP库的事件在Form上进行某些更改时,最重要的是在UI上.为此,您需要一个Control或从Control派生的任何东西(同样,也不必是一个Form),它是在UI线程上创建的.使用该控件检查InvokeRequired,如果为true,则调用Invoke.原始版本使用自定义委托(可能对我很熟悉,可能来自FTP示例),但是您可以使用所需的任何委托.

Where it gets important is up at the UI when you want to change something on a Form based on an event coming from the FTP library. For that you need a Control or anything derived from Control (again, doesn't have to be a Form) that was created on the UI thread. Use that control to check InvokeRequired, and if it's true, call Invoke. The original uses a custom delegate (probably comes from the FTP sample, as it looks really familiar to me), but you could use any delegate you want.

网络上有很多使用Control.Invoke的示例,因此您应该可以很容易地实现它.

There are plenty of examples on the web for using Control.Invoke, so you should be able to get it implemented fairly easily.

这篇关于在类而不是Windows窗体中使用OpenNETCF.Net.Ftp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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