Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 885841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:58:26+00:00 2026-05-15T12:58:26+00:00

I have a background process that i want to regularly maintain the state of

  • 0

I have a background process that i want to regularly maintain the state of gps location. I am not clear on how to invoke a delegate on the main thread in the ui layer when the threaded method is in another class. Here is sample code. My form launches the thread on load:

public partial class MainScreen : Form
    {
    .
    . // form stuff
    .
    private void MainScreen_Load(object sender, EventArgs e)
    {
        var gpsStatusManager = new GpsStatusManager();
        Thread t = new Thread(gpsStatusManager.UpdateLocation);
        t.IsBackground = true;
        t.Start();
    }

    delegate void GpsDataParameterDelegate(GpsStatus value);
    public void UpdateGpsStatus(GpsStatus value)
    {
        if (InvokeRequired)
        {
            // We're not in the UI thread, so we need to call BeginInvoke
            BeginInvoke(new GpsDataParameterDelegate(UpdateGpsStatus), new object[] { value });
            return;
        }
        // Must be on the UI thread if we've got this far
        gpsStatus.SetGpsStatus(value);
    }
}

I have a domain object class for the gps information:

public class GpsStatus
{
    public void SetGpsStatus(GpsStatus gpsStatus)
    {
        Latitude = gpsStatus.Latitude;
        Longitude = gpsStatus.Longitude;
        CurrentDateTime = gpsStatus.CurrentDateTime;
        NumberOfSatellites = gpsStatus.NumberOfSatellites;
        TotalNumberSatellites = gpsStatus.TotalNumberSatellites;
    }

    public float Latitude { get; private set; }
    public float Longitude { get; private set; }
    public DateTime CurrentDateTime { get; private set; }
    public int NumberOfSatellites { get; private set; }
    public int TotalNumberSatellites { get; private set; }
}

Then, my manager class where i update status in the secondary thread:

public class GpsStatusManager
{
    private GpsStatus _gpsStatus;

    public void UpdateLocationx()
    {
        while (UpdateGpsData()) 
        {
            Thread.Sleep(2000);
        }
    }

    private bool UpdateGpsData()
    {
        SError error;
        SGpsPosition gpsPosition;
        try
        {
            if (CApplicationAPI.GetActualGpsPosition(out error, out gpsPosition, true, 0) != 1)
                return false;
        }
        catch (Exception)
        {
             return false;
        }

        var numberOfSatellites = gpsPosition.Satellites;
        var totalSatellites = gpsPosition.satellitesInfo;
        var datetime = gpsPosition.Time;
        var lat = gpsPosition.Latitude;
        var lon = gpsPosition.Longitude;
        _gpsStatus.SetGpsStatus(lat, lon, datetime, numberOfSatellites, totalSatellites);

        //How do I invoke the delegate to send the _gpsStatus data to my main thread?
        return true;
    }
}

Thanks for any assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:58:27+00:00Added an answer on May 15, 2026 at 12:58 pm

    Here’s one way to do it, just off the top of my head:

    public class GpsStatusEventArgs : EventArgs
    {
        public GpsStatus Status { get; private set; }
        public GpsStatusEventArgs(GpsStatus status)
        {
            Status = status;
        }
    }
    
    public class GpsStatusManager
    {
        ...
        public event EventHandler<GpsStatusEventArgs> GpsStatusUpdated;
    
        private void OnGpsStatusUpdated(GpsStatus gpsStatus)
        {
            EventHandler<GpsStatusEventArgs> temp = GpsStatusUpdated;
            if (temp != null)
                temp.Invoke(this, new GpsStatusEventArgs(gpsStatus));
        }
    
    }
    
    public partial class MainScreen : Form
    {
        ...
        private void MainScreen_Load(object sender, EventArgs e)
        {
            var gpsStatusManager = new GpsStatusManager();
            gpsStatusManager.GpsStatusUpdated += new EventHandler<GpsStatusEventArgs>(GpsStatusManager_GpsStatusUpdated);
            ...
        }
    
        private void GpsStatusManager_GpsStatusUpdated(object sender, GpsStatusEventArgs e)
        {
            UpdateGpsStatus(e.Status);
        }
        ...
    }
    

    Then add this to the bottom of UpdateGpsData:

    OnGpsStatusUpdated(_gpsStatus);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want users to upload pics and then have a background process that converts
I have some code that starts a background process for search in my WPF
I have a process I want to run in the background of a page.
We have a background operation (Window service) that we want to use through a
I have a web app that opens and closes background process on the server.
I have a Java process that runs in the background. How can I quickly
I have a background process that modifies records in a database. The models connect
I want to have a background thread that will check for updates on the
I have a background worker process that starts provisioning a new client for our
I have a backend process that maintains state in a PostgreSQL database, which needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.