Displaying result after callback method is invoked?

N

null

I have a web application that fires off an async task, but I have not been able
to determine if there is a way for my application to notify the user that the
task has finished.

Here are the relevant parts of the code:

public partial class default2 : System.Web.UI.Page
{
#region struct to pass data to AsyncCallback method
public struct EncoderData
{
public IFlix flxFlix;
public string strSavePath;
public DateTime dtStartTime;
public string strOutputVideo;
public int intWidth;
public int intHeight;

public EncoderData(IFlix flxIn, string strPathIn, DateTime dtStartIn,
string strOutVidIn, int intWidthIn, int intHeightIn)
{
flxFlix = flxIn;
strSavePath = strPathIn;
dtStartTime = dtStartIn;
strOutputVideo = strOutVidIn;
intWidth = intWidthIn;
intHeight = intHeightIn;
}
}
#endregion struct to pass data to AsyncCallback method

public delegate void AsyncCaller(EncoderData myEncoderData);
AsyncCaller acEncoderCaller = new AsyncCaller(RunEncoder);

[MTAThread]
protected void btnEncode_Click(object sender, EventArgs e)
{
DateTime startTime;
string savePath = @"C:\_testData\flixmedia\temp\";

/* create an instance of the main engine interface, IFlix */
IFlix flix = (IFlix)createInstance("On2.FlixEngine");
lblMessage.Text = String.Empty;

#region invoke encoder
startTime = DateTime.Now;
EncoderData myEncoderData = new EncoderData(flix, savePath, startTime,
outputVideo, intOutputWidth, intOutPutHeight);
IAsyncResult arResult = acEncoderCaller.BeginInvoke(myEncoderData, new
AsyncCallback(EncodingDone), myEncoderData);
Session["EncoderResult"] = arResult;
#endregion invoke encoder
}

private static void RunEncoder(EncoderData encoderData)
{
// start the encode
flxRunEncoder.encode();

// retrieve the encoding status interface, IEncodingStatus
IEncodingStatus encstatus = flxRunEncoder.encodingStatus();
bool ier = true;

#region loop until encoder completion (check every 1 sec)
int intTime = 0;

while (ier)
{
ier = flxRunEncoder.isEncoderRunning() == 1;
System.Threading.Thread.Sleep(1000);
}
#endregion loop until encoder completion (check every 1 sec)
}
catch (COMException ex)
{
printStackTrace(flxRunEncoder, ex);
}
catch (Exception up)
{
ExceptionHandler.LogToTextFile( up.ToString());
}
}

private static void EncodingDone(IAsyncResult arIn)
{
try
{
EncoderData encoderData = (EncoderData)arIn.AsyncState;

IFlix flxEndEncode = encoderData.flxFlix;
DateTime startTime = encoderData.dtStartTime;
string savePath = encoderData.strSavePath;

printEncoderStatus(flxEndEncode);

DateTime stopTime = DateTime.Now;
TimeSpan duration = stopTime - startTime;

#region end the encoder process invocation

AsyncResult ar = (AsyncResult)arIn;
AsyncCaller myAsyncDelegate = (AsyncCaller)ar.AsyncDelegate;
myAsyncDelegate.EndInvoke(ar);

#endregion end the encoder process invocation

#region delete temp file
try
{
File.Delete(savePath);
}
catch (Exception up)
{
ExceptionHandler.LogToTextFile( "Encoding complete, but cleanup of
temp file failed. Error = " + up.ToString());
}
#endregion delete temp file
}
catch (System.Exception up)
{
ExceptionHandler.LogToTextFile(up.ToString());
}
}
 
P

Pavel Minaev

I have a web application that fires off an async task, but I have not been able
to determine if there is a way for my application to notify the user thatthe
task has finished.
Here are the relevant parts of the code:

        public partial class default2 : System.Web.UI.Page
[snip]

Not directly, since by that time the response to request has already
been returned to the client. The client can only find out the current
state of the operation by polling the server (e.g. via AJAX). So the
server would have to set some flag in some shared state indicating
that operation is completed, which it would then retrieve on the
polling request.

Note also that using threads to do background work in a web
application is inherently dangerous, because your appdomain can be
recycled under certain circumstances (which will abort all threads in
it). If there is a long-running operation that _must_ be finished,
you'll have to run it in a separate process, and use some means to
communicate the results to your application in the IIS working process
(.NET remoting, named pipes, whatever). Look here for more info on the
problem and possible solutions:

http://stackoverflow.com/questions/536681/can-i-use-threads-to-carry-out-long-running-jobs-on-iis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top