简体   繁体   English

在自定义类而不是Windows窗体中使用OpenNETCF.Net.Ftp

[英]Using OpenNETCF.Net.Ftp inside a custom class instead of inside a Windows Form

Exact duplicate of Using OpenNETCF.Net.Ftp inside a class instead of inside a Windows Form 在类而不是Windows窗体使用OpenNETCF.Net.Ftp的精确副本


So far I am using an FTP object inside a Windows form. 到目前为止,我正在Windows窗体内使用FTP对象。 FTP object runs in a separate thread, so to ensure that my app doesn't freeze up, I use the following piece of code: FTP对象在单独的线程中运行,因此为了确保我的应用程序不会冻结,我使用以下代码:

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. 我不清楚什么是字符串委托,但是可以。

However, I am now refactoring and wish to hide the ftp into a class. 但是,我现在正在重构,希望将ftp隐藏到一个类中。 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. 我最大的担心是InvokeRequired。

In the code above "This" is a form. 在上面的代码“ This”中是一种形式。 If I hide the ftp object inside a class such as the following: 如果我将ftp对象隐藏在如下类中:

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

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

"This" becomes an object of MyClass. “这”成为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?). 我不确定是否在类上实现了InvokeRequired属性(也许我应该使其实现具有该属性的特殊接口?)。 Or perhaps I am missing something and I am not supposed to use multithreaded objects inside classes? 或者,也许我缺少某些东西,并且我不应该在类内部使用多线程对象?

I am using c# and asp.net 2.0 with VS 2008. 我在VS 2008中使用c#和asp.net 2.0。

InvokeRequired and Invoke only need to be used at the UI level, to ensure that you don't interact with your controls on a non-UI thread. InvokeRequired和Invoke仅需要在UI级别使用,以确保您不与非UI线程上的控件进行交互。 If MyClass isn't a UI component, you don't have anything to worry about. 如果MyClass不是UI组件,则无需担心。

Where you MAY need to worry is when MyClass interacts with the UI. 您可能需要担心的是MyClass与UI交互的时间。 For example, if MyClass is going to raise an event as a result of OnResponse, and the UI is going to observe that event, then in the event handler in the UI, you will need to use InvokeRequired and Invoke. 例如,如果MyClass由于OnResponse而引发一个事件,而UI将要观察该事件,则在UI中的事件处理程序中,您将需要使用InvokeRequired和Invoke。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM