Reputation: 1299
I am a little confused.
I have a background worker which in its dowork method goes off and gets a pdf file (byte[]) for me to open. when its done, in the runworker completed method i create my pdf display object and display it.
However, i want to check if this byte[] is null in the dowork method, and if so go to a new database location (which requires a new set of method calls to get a different pdf)
I cant actually see how i can do this (other than spawning a new background worker within a background worker - if thats even possible :) )
Here is the code setup i have at the moment, and hopefully this should illustrate the problem i am running in to
byte[] pdf;
void method_DoWork(object sender, DoWorkEventArgs e)
{
pdf = myObject.getPdf();
}
void method_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (pdf!= null)
{
displayPdf(pdf);
}
else
{
goAndGetAnotherPDF();
}
}
obviously i can check for null in the dowork method instead. Is there anyway from there saying
v
oid method_DoWork(object sender, DoWorkEventArgs e)
if(pdf != null)
{
callRunWorkerCompleted()
}
else
{
doSomeOtherStuffAndStoreInLocalVariables();
}
Thanks
Upvotes: 1
Views: 363
Reputation: 2842
Can you just check if the PDF is null in the DoWork thread and, if so, switch databases and load the pdf again?
void method_DoWork(object sender, DoWorkEventArgs e)
{
pdf = myObject.getPdf();
if(pdf == null)
{
//Switch database or whatever and call again
pdf = myObject.getPdf();
}
}
Upvotes: 0
Reputation: 1399
Well, the DoWork method is already running in the background thread. Why don't you just continue on and do all of the work there? When the DoWork method completes, regardless of how, the callback will fire.
void method_DoWork(object sender, DoWorkEventArgs e)
{
pdf = myObject.getPdf();
if (pdf == null)
{
pdf = SomeOtherGetMethod();
}
}
I don't see any need to launch another thread just for the case where the first return is null. Just call the second fetch method if the first fails.
Upvotes: 3