|
-
Nov 7th, 2007, 10:29 AM
#1
Thread Starter
Frenzied Member
Multi-Threaded, Saving data in the background thread
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
Code:
{
//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();
}
Code:
//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
Code:
//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..
Code:
//Load Starters data
private void LoadStartersData()
{
try
{
//DS = ws.GetStarters();
writeXML.WriteStartersXML(DS);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
-
Nov 7th, 2007, 12:49 PM
#2
Re: Multi-Threaded, Saving data in the background thread
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.
My usual boring signature: Nothing
 
-
Nov 7th, 2007, 08:51 PM
#3
Thread Starter
Frenzied Member
Re: Multi-Threaded, Saving data in the background thread
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
-
Nov 10th, 2007, 05:19 AM
#4
Frenzied Member
Re: Multi-Threaded, Saving data in the background thread
Hi,
have you tried an 'application.doevents' in between your statements - this may give the UI time to kick in
Pete
-
Nov 10th, 2007, 01:13 PM
#5
Thread Starter
Frenzied Member
Re: Multi-Threaded, Saving data in the background thread
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.
vb Code:
//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();
-
Nov 10th, 2007, 04:44 PM
#6
Re: Multi-Threaded, Saving data in the background thread
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.
My usual boring signature: Nothing
 
-
Nov 11th, 2007, 01:34 AM
#7
Thread Starter
Frenzied Member
Re: Multi-Threaded, Saving data in the background thread
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
-
Nov 11th, 2007, 03:37 AM
#8
Frenzied Member
Re: Multi-Threaded, Saving data in the background thread
Hi,
no - try
Code:
Application.DoEvents();
this.LoadStartersData();
Application.DoEvents();
this.LoadBeverageData();
Application.DoEvents();
this.LoadOrdersData(staffID);
Application.DoEvents();
this.LoadMenus();
Application.DoEvents();
this.LoadMaincourse();
Application.DoEvents();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|