Is there any way with a try-catch that if the try fails that I retry it
Basically I have an app that downloads text files, and then processes them. However, at my location we lose internet connectivity on a fairly common basis, and I don't want the app to try to process half downloaded files.
The downloading sequence is in a class by itself, so I just basically want to do the following:
try
{
download files
}
catch
{
try again
}
I haven't found any way to do it so far, so any help would be appreciated.
Thanks,
Russ

try-catch
Dave Foderick
I ended up going with Sven's model, and it worked great.
Thanks a lot guys for all your help,
Russ
Shril Pyrrho
Yes, that makes perfect sense. I have one more question if you don't mind.
If this is my method:
public
void wp_download_process(object sender, DoWorkEventArgs e){
}
How do I call it
I am trying to use:
this
.wp_download_process("I don't know what goes here", e.Argument);And keep getting errors.
Thanks for all your help,
Russ
gamer6000
the example shows you how :-P
If you could have one method such as "DoTrySomething(bool keepTrying)" then in here have your try catch block, it will keep calling itself until you set the keepTrying bool parameter to false. Example:
//global
private int maxNumberOfTries = 3;
private int numberOfTries = 0;
...
...
this.DoTrySomething(true);
..
private void DoTrySomething(bool keepTrying)
{
if (!keepTrying || this.maxNumberOfTries < numberOfTries)
{
try { .... }
catch(Exception e)
{
this.numberOfTries++;
this.DoTrySomething(true)
}
}
}
so here, this method will keep calling itself when there has been an error unless either the parameter passed in (keepTrying) is false OR the numberOfTries equals the maxNumberOfTries and increments the numberOfTries by one on every exception that it catches
does this make more sense
the idea is:
I guess you could do away with the bool value and just keep an eye on the maxNumberOfTries. Example:
//global
private int maxNumberOfTries = 3;
private int numberOfTries = 0;
...
...
this.DoTrySomething(true);
..
private void DoTrySomething(int triesHad)
{
if (triesHad < this.maxNumberOfTries)
{
try { .... }
catch(Exception e)
{
this.numberOfTries++;
this.DoTrySomething(this.numberOfTries)
}
}
}
does this make sense
stang4lyfe
Hi,
you could also use a straight forward loop. That would also avoid stack overflows because of deep recursions:
void foo()
{
bool tryAgain = true;
while(tryAgain)
{
tryAgain = false;
try
{
// do some work which may throw SomeException
}
catch(SomeException ex)
{
// clean up if you can
tryAgain = true;
}
}
}
--
SvenC
Rattlerr
I'm not very clear on how to use the recursive method from this post or the one that you gave the link to. Could you possibly show me how it would be used
Thanks,
Russ
flash.tato
im not an expert on the backgroundworker thread to be honest so I guess a good place to start would be here:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
as there is tons of docs and examples. Sorry I couldnt be of much help on this one
Marlon Smith
good point Sven, I knew there had to be a simpler way! however even using the previous one wouldnt have the stack overflow exception since you get a max of 3 tries and thats it.
In regards to the other question, im not sure. Is this a backgroundworker thread
DevboyX
Yes, this is a backgroundworker thread.
Thanks,
Russ
AlexBB
there isn't a way but you can try to recall itself, the method rather, which has the try catch block to do whatever it is you are doing. (Recursive method). You could also then set some "numberOfRetries" variable globally so everytime you catch an exception it would add 1 to it but if it gets to 3, then don't recall the method.
take a look at this too:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=808935&SiteID=1