Using OpenNETCF.Net.Ftp inside a custom class and not inside a windows form

Exact duplicate Using OpenNETCF.Net.Ftp inside class instead of inside Windows form


So far I am using FTP object inside Windows Form. The FTP object runs on a separate thread, so to make sure my application doesn't freeze, 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 don't quite understand what a string delegate is, but it works.

However, now I am refactoring and want to hide ftp in the class. My question is, how can I make sure the main thread doesn't freeze? All the links on the internet regarding raising events within classes make sense, but I haven't found a single example where the application is multithreaded. My biggest concern is InvokeRequired.

In the above code, "This" is a form. If I hide the ftp object inside a class like:

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

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

      

"This" becomes a MyClass object. I'm not sure if the InvokeRequired property is implemented in the class (perhaps I should force it to implement a custom interface that has this property?). Or perhaps I'm missing something and I shouldn't be using multithreaded objects inside classes?

I am using C # and asp.net 2.0 with VS 2008.

0


a source to share


1 answer


InvokeRequired and Invoke should only be used at the UI level to ensure that you are not interacting with controls on a non-UI thread. If MyClass is not a UI component, then you have nothing to worry about.



Where you might need to worry when MyClass interacts with the UI. For example, if MyClass is going to raise an event as a result of OnResponse, and the UI will observe this event, then in an event handler in the UI, you will need to use InvokeRequired and Invoke.

+1


a source







All Articles