Solved

Do I need to set again event handler in case I make object to null ?

Posted on 2009-05-10
8
178 Views
Last Modified: 2013-12-17
Hi experts!

  Im using C# 2008 , WinForm NET 2. I have object, which I create, than set dispose and then again create. Im interesting what happen with event holder, do I need to set again events holder, or not ? I ask because after once I commit goto I loose this handler,  software is working but I do not enter in this function (fileTransfer1_OnProgress)

public partial class frmFTPProgress : CommonSearchForm

    {

        

        public SocketTools.FileTransfer fileTransfer1;
 
 

public  Boolean SendFileViaFTP( )

        {
 

        ResumeFTP:

            fileTransfer1 = new SocketTools.FileTransfer();

            fileTransfer1.OnProgress += new SocketTools.FileTransfer.OnProgressEventHandler(fileTransfer1_OnProgress);
 
 

       // some code &

     If (err<>0) 

           {

          fileTransfer1.dispose();

// do I need againt to  fileTransfer1.OnProgress += new                          

//SocketTools.FileTransfer.OnProgressEventHandler(fileTransfer1_OnProgress)
 

                goto ResumeFTP; //                                 

}
 

        }

Open in new window

0
Comment
Question by:dvplayltd
  • 4
  • 4
8 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24348570
If you use an object that has a dispose() method, you should use "using" instead (and, in general, it is best to avoid "goto" with C#, it defies structured coding, but some argue that it's a matter of taste).

Is the above really your code? There are quite some things in that cannot compile: "If" instead of "if", "err" variable which isn't in declared, ".dispose()" which should be ".Dispose()" etc.

Below is an example of how to use the using-block in your case:



using(FileTransfer fileTransfer1 = new SocketTools.FileTransfer())

{

    // do your fileupload etc.

    fileTransfer1.OnProgress += new SocketTools.FileTransfer.OnProgressEventHandler(fileTransfer1_OnProgress);
 

}  // at this point, the compiler will make sure automatically that .Dispose and .Close are called

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24348574
Now, a small note on your question itself: "are the event handlers gone". The answer is: no, they are not. But after Dispose(), you should not use the object anymore. In fact, you will receive an error if you try to. So it doesn't really matter because the events will never be fired anyway.
0
 

Author Comment

by:dvplayltd
ID: 24348784
Yes, this is real code ..almost :-)

This is function which provide send to FTP host file with provide resume. Resume - this is same like start from same function.

 I publish all code (only this function. OK .. i see you profile and understand that you are professional .  
Look and offer change related with this question, I'm sure that you will easy navigate where is responsible code for this. Check function SendFileViaFTP()  and see will you somethink wrong ... i still after one call goto loose  fileTransfer1_OnProgress() ...


I need to use goto, other option is to make recall (function to call it self) which make more question than will solve & J
And  using is not practical  when you see full code will see why &

Note: There other reason why do not work as expected .. may be FTP server in resume mode not provide information back?

frmFTPProgress.txt
0
 

Author Comment

by:dvplayltd
ID: 24348803
This is code for form, I do not send you other file (frmFTPProgress.Designer.cs,   frmFTPProgress.resx) for WInForm because have third part librabry ..DevComponent, Catalist FTP ...

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:dvplayltd
ID: 24348856
Other, is there diffrence between

fileTransfer1.Dispose()
and
fileTransfer1 = null

Which I should ?
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24348964
On goto: you are entitled to you opinion of course, but the construction you are using can be achieved by using a while-loop. Recursion (calling oneself) is not a good idea in this case. For quite a while, actually, since the sixties, GOTO is considered "harmful". Here's the original paper: http://www.ecn.purdue.edu/ParaMount/papers/dijkstra68goto.pdf, but this is an easier read: Or, more to the point or modern OO: http://visualbasic.about.com/od/imhoinmyhumbleopinion/a/GraGoTo01.htm


> Other, is there diffrence between

yes, there is. Like I said in my original answer, you need to apply a using-statement. Setting it to null is not effective enough, and will cause the .Dispose not to work anymore. You need to Close() and then Dispose if you do it by hand and you must do so in finally-block of a try/catch/finally or a try/finally. You can do that by hand, but like I already said, there's an easier way to do so" the using-statement.

Here's how you can do it by hand, to see what is actually going on under the hood or the using-statement:



FileTransfer fileTransfer1;

try 

{

   fileTransfer1 = new SocketTools.FileTransfer();

   // ... do your things

}

finally 

{

    fileTransfer1.Close();

    fileTransfer1.Dispose();

}

Open in new window

0
 

Author Closing Comment

by:dvplayltd
ID: 31579919
Question is solved in other way .. however thank for you time :-)
P.S: About goto - it is not for common use, but in some situation is best and I use it. Yes, it can be solved via while, but I need to make two while which break each other .. then goto is best.
0
 
LVL 39

Expert Comment

by:abel
ID: 24372290
(from grading comment)
> Question is solved in other way ..


Glad you found your way. Pity you can't use the OO methods of programming, but I know, it takes quite some time to get used to it..

I'm curious about your "other way" though. Because there's little else you can do with objects that expose the IDispose interface, without risking loosing resources. This method is there for a reason. (also read: http://discuss.joelonsoftware.com/default.asp?dotnet.12.387957.17)

On a side note and off-topic: be careful about B- or C- grading, esp. if someone gives you a quick, thorough and to the point answer. You don't have to agree with an expert, but that doesn't mean the answer is less good. Remember: the expert you grade today is the one you may need tomorrow. See the 10-pts must principle of EE.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now