Results 1 to 8 of 8

Thread: Multi-Threaded, Saving data in the background thread

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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);
                }
            }
    steve

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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
    steve

  4. #4
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    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
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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:
    1. //Background worker thread for saving the data
    2.                     Thread workerThreadSaveXML = new Thread(BackgroundProcessSaveXML);
    3.                     workerThreadSaveXML.IsBackground = true;
    4.                     workerThreadSaveXML.Start();
    5.                     Application.DoEvents();
    6.  
    7.                     Cursor.Current = Cursors.Default;
    8.                     frmOrders objOrders = new frmOrders();
    9.                     objOrders.Show();
    10.                     this.Hide();
    steve

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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
    steve

  8. #8
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    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();
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width