PDA

Click to See Complete Forum and Search --> : Multi-Threaded, Saving data in the background thread


steve_rm
Nov 7th, 2007, 09:29 AM
Hello,

VS 2005 Windows Mobile 6.0

I have an application that get data from a web service. This gets done in the main UI thread, as the application cannot move on until this is finished.

However, once the data has been loaded into the dataset, I want to save the contents of the tables in XML on the PDA device itself. It is this that I want to run in the background, so that this form will hide and open another form when this is being done in the background.

Many thanks for any suggestions,

Steve

{
//Load all data into the dataset from the web service
this.LoadDataIntoDataSet();
//Create new background thread
this.workerThreadSaveXML = new Thread(BackgroundProcessSaveXML);
this.workerThreadSaveXML.IsBackground = true;
this.workerThreadSaveXML.Start();

//THIS SHOULD OPEN THE NEW FORM WHILE THE DATA IS STILL BEING SAVED.

frmOrders objOrders = new frmOrders(DS);
objOrders.Show();
this.Hide();
}

//Load all data into the dataset - NOT RUN IN THE THREAD. THIS HAS TO BE DONE BEFORE THE //NEW FORM WILL OPEN.
private void LoadDataIntoDataSet()
{
try
{
DS = ws.GetStarters();
DS = ws.GetOrders(staffID);
DS = ws.GetBeverages();
DS = ws.GetMainCourses();
DS = ws.GetOrderDetails();
DS = ws.GetMenus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

This is will run in the background in saving the data to xml - THIS IS THE IMPORTANT PART AND SHOULD STILL WORK IF THE NEW FORM HAS BEEN OPENED
Code Block

//Save to xml files so that these can be used offline.
private void BackgroundProcessSaveXML()
{
try
{
this.LoadStartersData();
this.LoadBeverageData();
this.LoadOrdersData(staffID);
this.LoadMenus();
this.LoadMaincourse();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

//This is one example of loading the data into the xml file - there are 5 but I have just added on here to save space on the post..


//Load Starters data
private void LoadStartersData()
{
try
{
//DS = ws.GetStarters();
writeXML.WriteStartersXML(DS);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Shaggy Hiker
Nov 7th, 2007, 11:49 AM
Ummm, is there a question there? The steps you have laid out seem reasonable, though in mobile apps I don't like the idea of using more than one form, but use panels on a form instead.

You create your background thread, then start it.

Of course, the saving will take a certain amount of time, and it seems like you might want to do it again periodically, depending on what is happening with the dataset data, so there might be some advantage to wrapping all of the interactions with the XML store into a class that will exist on the main UI thread, but will spawn threads to read and write as needed, and will be able to track what the status of those threads are, but that's making lots of assumptions about what you intend to do with the data.

steve_rm
Nov 7th, 2007, 07:51 PM
Hello,

Sorry, I should had been more clear.

Currently when I run the code it doesn't open the new form. It continues to save the data, then it runs the code to open the new form.

I would like to open the new form, while the data is being saved.

Many thanks,

Steve

petevick
Nov 10th, 2007, 04:19 AM
Hi,
have you tried an 'application.doevents' in between your statements - this may give the UI time to kick in

Pete

steve_rm
Nov 10th, 2007, 12:13 PM
Hello,

Thanks for your reply.

I have added the Application.Doevent, not sure of where you wanted me to put it. But I did after the start method. But still the same problem. The new form will not open until the work is finished. Also the UI is not response when the working is being done.

Thank for any more suggestions,

See code below.

//Background worker thread for saving the data
Thread workerThreadSaveXML = new Thread(BackgroundProcessSaveXML);
workerThreadSaveXML.IsBackground = true;
workerThreadSaveXML.Start();
Application.DoEvents();

Cursor.Current = Cursors.Default;
frmOrders objOrders = new frmOrders();
objOrders.Show();
this.Hide();

Shaggy Hiker
Nov 10th, 2007, 03:44 PM
It really sounds like you are running in a non-pre-emptive environment. I don't know if that exists for some mobile platforms or not, but that is the behavior you are seeing.

steve_rm
Nov 11th, 2007, 12:34 AM
Hello,

Thanks for the reply.

I am working on a windows mobile 6, I am sure that support multi-tasking if that is what you mean by a non-pre-emptive environment.

Is it that there could be something wrong with my code?

Does all the processing have to be done in the single thread function, as mine calls other functions to do the work?

Many thanks for any more help,

Steve

petevick
Nov 11th, 2007, 02:37 AM
Hi,
no - try
Application.DoEvents();
this.LoadStartersData();
Application.DoEvents();
this.LoadBeverageData();
Application.DoEvents();
this.LoadOrdersData(staffID);
Application.DoEvents();
this.LoadMenus();
Application.DoEvents();
this.LoadMaincourse();
Application.DoEvents();