Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi threaded - saving data in the background 1

Status
Not open for further replies.

robert201

Programmer
Jul 18, 2007
80
TH
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);
            }
        }
 
A) This is C++ or C# so wrong forum.

B) I don't know either for vb.net as yet, but I'll try to point you in a direction. You want your Save/Load you would want in its own class/module. We will say class for now. You can create either a delegate or a thread. We will say thread for now. Since we are doing a thread you would want to have a global variable(s) for the dataset in the class. Set the data then call a function in the class to start your save/thread.

VB.NET would be something like this:
Code:
Imports System.Threading

Public Class Class1
    Private cDataSet As DataSet

    Public Sub SaveData()
        Dim SaveThread As New Thread(AddressOf SaveDataThread)
        SaveThread.Start()
    End Sub

    Private Sub SaveDataThread()
        'code to save
    End Sub

    Public WriteOnly Property SetDataSet() As DataSet
        Set(ByVal value As DataSet)
            cDataSet = value
        End Set
    End Property

End Class

-I hate Microsoft!
-Forever and always forward.
 
I don't know either for vb.net as yet = I don't know either for .net as yet

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top