Hi all, I've got the following code:
void ReferenceManager_DoWork(object sender, DoWorkEventArgs e)
{
try
{
// Get the raw data
byte[] data = this.GetData(IvdSession.Instance.Company.Description, IvdSession.Instance.Company.Password);
// Deserialize the list
List<T> deseriaizedList = null;
using (MemoryStream ms = new MemoryStream(data))
{
deseriaizedList = Serializer.Deserialize<List<T>>(ms);
}
// Convert the list to the Reference Interface
List<IReference> referenceList = new List<IReference>();
foreach (T entity in deseriaizedList)
{
IReference reference = (IReference)entity;
referenceList.Add(reference);
}
e.Result = referenceList;
}
catch (WebException)
{
e.Result = null;
}
}
The code is basically calling a delegate to a WebService method. Unfortunately, the main reason I used a background worker was to stop the UI freezing when loading the data. I've got a form that pops up saying please wait, click here to cancel.
On the click of cancel I call CancelAsync on the background worker. Now, as I'm not looping I cannot see a nice way of checking for a cancellation. The only option I can see is to have...
byte[] m_CurrentData;
...outside of the method and launch a new thread on the start of DoWork(..) that calls the webservice to populate m_CurrentData. I'd then need to perform a loop checking if cancelled or checking if m_CurrentData is no longer null.
Is there any better way of achieving a cancellation?