rliq
January 20th, 2010, 09:40 PM
I have a Cache class, one of it's methods is:
public Byte[] GetFile(String fileName)
{
...
}
Behind the scenes it gets the file from the Cache, else the Web. It works fine. Obviously, when it has to get it from the Web, it takes longer. Therefore I want my Cache class to also provide the following method.
public void GetFileAsync(String fileName, /* some callback function that allows me to return the Byte[] */)
In this method I create a BackgroundWorker and start it.
In the DoWork(), I call my normal GetFile(...). I understand I will have to lock{} stuff etc...
In the RunWorkerCompleted I assume I need to call the user of my Cache class's callback passing the file data.
I just can't get the syntax right. How/where do I define the callback, so the user of my Cache class knows it's signature and how to get the Byte[] data?
Here's my test code snippit, with some bits commented:
private class InputParameters
{
public /* ???? */ CallBack;
public String FileName;
}
private class OutputParameters
{
public /* ???? */ CallBack;
public Byte[] FileData;
}
public void GetFileAsync(/* ???? */ callBack, String fileName)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
InputParameters parameters = new InputParameters();
parameters.CallBack = callBack;
parameters.FileName = fileName;
worker.RunWorkerAsync(parameters);
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
InputParameters inputParameters = e.Argument as InputParameters;
OutputParameters outputParameters = new OutputParameters();
outputParameters.CallBack = inputParameters.CallBack;
outputParameters.FileData = GetFile(inputParameters.FileName);
e.Result = outputParameters;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
OutputParameters parameters = e.Result as OutputParameters;
parameters.CallBack(parameters.FileData); // ????
}
Or am I barking up the wrong tree alltogether?
public Byte[] GetFile(String fileName)
{
...
}
Behind the scenes it gets the file from the Cache, else the Web. It works fine. Obviously, when it has to get it from the Web, it takes longer. Therefore I want my Cache class to also provide the following method.
public void GetFileAsync(String fileName, /* some callback function that allows me to return the Byte[] */)
In this method I create a BackgroundWorker and start it.
In the DoWork(), I call my normal GetFile(...). I understand I will have to lock{} stuff etc...
In the RunWorkerCompleted I assume I need to call the user of my Cache class's callback passing the file data.
I just can't get the syntax right. How/where do I define the callback, so the user of my Cache class knows it's signature and how to get the Byte[] data?
Here's my test code snippit, with some bits commented:
private class InputParameters
{
public /* ???? */ CallBack;
public String FileName;
}
private class OutputParameters
{
public /* ???? */ CallBack;
public Byte[] FileData;
}
public void GetFileAsync(/* ???? */ callBack, String fileName)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
InputParameters parameters = new InputParameters();
parameters.CallBack = callBack;
parameters.FileName = fileName;
worker.RunWorkerAsync(parameters);
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
InputParameters inputParameters = e.Argument as InputParameters;
OutputParameters outputParameters = new OutputParameters();
outputParameters.CallBack = inputParameters.CallBack;
outputParameters.FileData = GetFile(inputParameters.FileName);
e.Result = outputParameters;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
OutputParameters parameters = e.Result as OutputParameters;
parameters.CallBack(parameters.FileData); // ????
}
Or am I barking up the wrong tree alltogether?