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.
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);
You should use the SynchronizationContext class.
In the UI thread (in any class), set a field (perhaps static) to SynchronizationContext.Current.
You can then call Send or Post on the saved instance to execute code on the UI thread.
Here is another approach using the ISynchronizeInvoke interface. This is the same pattern the System.Timers.Timer class uses to raise the Elapsed event.
public class GpsStatusManager
{
public ISynchronizeInvoke SynchronizingObject { get; set; }
public event EventHandler Update;
public void UpdateGpsData()
{
// Code omitted for brevity.
OnUpdate(_gpsStatus);
return true;
}
private OnUpdate(GpsStatus status)
{
if (SynchronizingObject != null && SynchronizingObject.IsInvokeRequired)
{
ThreadStart ts = () => { OnUpdate(status); };
SynchronizingObject.Invoke(ts, null);
}
else
{
if (Update != null)
{
Update(this, status);
}
}
}
public class UpdateEventArgs : EventArgs
{
public GpsStatus Status { get; set; }
}
}
Related
I have the following code, I would like to call the function RefreshProcess(SaveEventTriggerModelArgs obj) from MainWindow_Loaded.
However the problem I am running into due to lack of knowledge working with window apps I calling this method inside.
It will not let me because of the arguments SaveEventTriggerModelArgs obj and if I add those into RefreshProcess, they are different from void MainWindow_Loaded(object sender, RoutedEventArgs e). How to do it?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded+= Window_Loaded;
}
private void RefreshProcess(SaveEventTriggerModelArgs obj)
{
var rect = new Rect();
Dispatcher.Invoke(() =>
{
obj.CurrentEventTriggerModel.ProcessInfo = new ProcessInfo()
{
ProcessName = "Nox" != null ? $"Nox" : "",
Position = rect,
};
});
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
}
public class SaveEventTriggerModelArgs : INotifyEventArgs
{
public Model CurrentEventTriggerModel { get; set; }
}
public class MousePointEventArgs : INotifyEventArgs
{
public ViewModel MousePointViewMode { get; set; }
}
public class ViewModel
{
}
public class Model
{
public ProcessInfo ProcessInfo { get;set;}
}
public class ProcessInfo
{
public string ProcessName { get;set;}
public Rect Position { get;set;}
}
I am implementing a scheduler. As part of its core logic it processes a custom object Schedule. Basically it iterates over an array of schedules and try to process it. The problem is who ever creates a Schedule needs to register it with the container using an ISchedule interface. My Scheduler then pulls all the ISchedule references from the container. SO far this is working but it does not have the flexibility of loading the schedules runtime. What design and implementation I can adapt to implement a Scheduler that can load those Schedules run time. I am giving some sample code.
Something that is coming to my mind is having the developers writing a json representation of the Schedules and put that inside a config or implementing an endpoint that returns that config to the Scheduler. But can I avoid this? I want the Scheduler to be completely agonistic of developer code.
You can use the factory to register information about schedule classes. And dynamically change the call interval by finding the schedule by Id.
You register in the container: <IScheduleFactory,ScheduleFactory> and <IScheduleManager,ScheduleManager>
public interface ISchedule
{
public string Id { get; set; }
public TimeSpan Interval { get; set; }
public DateTime? LastExecution { get; set; }
public bool CanStart { get; }
void Start();
void Stop();
}
public sealed class Schedule : ISchedule
{
public string Id { get; set; }
public TimeSpan Interval { get; set; }
public DateTime? LastExecution { get; set; }
public bool CanStart {
get
{
lock (_sync)
{
return !LastExecution.HasValue || LastExecution.Value.Add(Interval) >= DateTime.UtcNow;
}
}
}
private readonly object _sync = new object();
public void Start()
{
lock (_sync)
{
if (!LastExecution.HasValue || LastExecution.Value.Add(Interval) >= DateTime.UtcNow)
{
// DO WORK
LastExecution = DateTime.UtcNow;
}
}
}
public void Stop()
{
throw new NotImplementedException();
}
}
public interface IScheduleFactory
{
ISchedule Create();
}
public sealed class ScheduleFactory: IScheduleFactory
{
private readonly IScheduleManager _manager;
public ScheduleFactory(IScheduleManager manager)
{
_manager = manager;
}
public ISchedule Create()
{
var schedule = new Schedule();
_manager.Register(schedule);
return schedule;
}
}
public interface IScheduleManager
{
void Register(ISchedule schedule);
ISchedule Get(string id);
void Start();
void Stop();
}
public sealed class ScheduleManager : IScheduleManager
{
private readonly Dictionary<string,ISchedule> _items = new Dictionary<string, ISchedule>();
private readonly object _sync = new object();
public void Register(ISchedule schedule)
{
lock (_sync)
{
if (_items.ContainsKey(schedule.Id))
_items.Add(schedule.Id, schedule);
}
}
public ISchedule Get(string id)
{
lock (_sync)
{
if (_items.ContainsKey(id))
return _items[id];
}
return null;
}
private bool _isStart;
public void Start()
{
_isStart = true;
while (_isStart)
{
ISchedule[] array = null;
lock (_sync)
{
array = _items.Values.ToArray();
}
foreach (var schedule in array)
{
if (schedule.CanStart)
Task.Factory.StartNew(()=>schedule.Start());
}
}
}
public void Stop()
{
_isStart = false;
}
}
I would like to use MVP Design pattern for a WinForm App but i'm facing the problem of calling a View Update from another thread.
Here's my code
MODEL
public class Model : IModel
{
public string Status { get; set; }
public async void LongOperation(IHomeView View)
{
for (int i = 0; i < 1000; i++)
{
View.StatusListView = i.ToString();
}
}
}
PRESENTER
public class HomePresenter
{
IHomeView _IView;
IModel _IModel;
Model _Model = new Model();
public HomePresenter(IHomeView IView)
{
_IView = IView;
}
public async void LaunchLongOperation()
{
await Task.Run(() => _Model.LongOperation(_IView));
}
}
INTERFACE VIEW-PRESENTER
public interface IHomeView
{
string StatusListView { get; set; }
}
INTERFACE PRESENTER-MODEL
public interface IModel
{
string Status { get; set; }
}
FORM:
public partial class frmMain : Form, IHomeView
{
HomePresenter _Presenter;
public frmMain()
{
InitializeComponent();
_Presenter = new HomePresenter(this);
}
public string StatusListView
{
get
{
return lstActivityLog.Text;
}
set
{
lstActivityLog.Items.Add(value);
}
}
private void btnAvvia_Click(object sender, EventArgs e)
{
_Presenter.launchLongOperation();
}
}
i would like to update a list view in the Main form during the long operations of the Model class.
Which is the best way to do that?
Try this code without debugging, you'll be surprised about it works!
The quick and dirty way to make it work in debugging mode as well is to add Control.CheckForIllegalCrossThreadCalls = false; into the constructor of your form.
public partial class MainForm : Form, IHomeView
{
HomePresenter _Presenter;
public MainForm()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //<-- add this
_Presenter = new HomePresenter(this);
}
public string StatusListView
{
get
{
return lstActivityLog.Text;
}
set
{
lstActivityLog.Items.Add(value);
}
}
private void button1_Click(object sender, EventArgs e)
{
_Presenter.LaunchLongOperation();
}
}
I have a property that can be modified from a method with Invoke()(from thread) and other without invoke() in the same class.
what happen if they are called in the same moment?
This is possible? Since can affect the condition in some method.
For example:
public class Test{
public bool testBool { get; set; }
public void MethodWIthInvoke(){
this.Invoke(new Action(() =>
{
if (testBool)
{
testBool = false;
}
}));
}
public void Method(){
if (testBool)
{
testBool = false;
}
}
}
I am not sure why do you need to make the code this way, anyway, since both of the methods will be called from the same thread then it will be fine. I want to suggest another way to write your code as follows:
public class Test{
public bool testBool { get; set; }
public void Method()
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
if (testBool)
{
testBool = false;
}
}));
}
else
{
if (testBool)
{
testBool = false;
}
}
}
}
The following code is a silverlight application but the same happens in WPF, so it seems to be just something I'm missing regarding the delegate, event, etc.
Can anyone tell me why the following code successfully executes this event:
OnLoadingComplete(this, null);
but never executes this event handler?
void initialDataLoader_OnLoadingComplete(object obj, DataLoaderArgs args)
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Diagnostics;
namespace TestEvent22928
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
DataLoader initialDataLoader = new DataLoader("initial");
initialDataLoader.RegisterText("test1", "http://test:111/testdata/test1.txt");
initialDataLoader.RegisterText("test2", "http://test:111/testdata/test2.txt");
initialDataLoader.BeginLoading();
initialDataLoader.OnLoadingComplete += new DataLoader.LoadingComplete(initialDataLoader_OnLoadingComplete);
}
void initialDataLoader_OnLoadingComplete(object obj, DataLoaderArgs args)
{
Debug.WriteLine("loading complete"); //WHY DOES EXECUTION NEVER GET HERE?
}
}
public class DataManager
{
public DataLoader CreateDataloader(string dataloaderIdCode)
{
DataLoader dataLoader = new DataLoader(dataloaderIdCode);
return dataLoader;
}
}
public class DataLoader
{
public string IdCode { get; set; }
public List<DataItem> DataItems { get; set; }
public delegate void LoadingComplete(object obj, DataLoaderArgs args);
public event LoadingComplete OnLoadingComplete = delegate { };
private int dataItemCurrentlyLoadingIndex;
public DataLoader(string idCode)
{
IdCode = idCode;
DataItems = new List<DataItem>();
dataItemCurrentlyLoadingIndex = -1;
}
public void RegisterText(string idCode, string absoluteSourceUrl)
{
DataItem dataItem = new DataItem
{
IdCode = idCode,
AbsoluteSourceUrl = absoluteSourceUrl,
Kind = DataItemKind.Text
};
DataItems.Add(dataItem);
}
public void BeginLoading()
{
LoadNext();
}
private void LoadNext()
{
dataItemCurrentlyLoadingIndex++;
if (dataItemCurrentlyLoadingIndex < DataItems.Count())
{
DataItem dataItem = DataItems[dataItemCurrentlyLoadingIndex];
Debug.WriteLine("loading " + dataItem.IdCode + "...");
LoadNext();
}
else
{
OnLoadingComplete(this, null); //EXECUTION GETS HERE
}
}
}
public class DataItem
{
public string IdCode { get; set; }
public string AbsoluteSourceUrl { get; set; }
public DataItemKind Kind { get; set; }
public object DataObject { get; set; }
}
public enum DataItemKind
{
Text,
Image
}
public class DataLoaderArgs : EventArgs
{
public string Message { get; set; }
public DataItem DataItem { get; set; }
public DataLoaderArgs(string message, DataItem dataItem)
{
Message = message;
DataItem = dataItem;
}
}
}
You're registering the handler only after you start loading:
initialDataLoader.BeginLoading();
initialDataLoader.OnLoadingComplete += new DataLoader.LoadingComplete(initialDataLoader_OnLoadingComplete);
The way your code is currently written, it looks like BeginLoading() blocks until completion, which means the handler will never be called, as you don't set it until after you've finished loading.