I have a List<MyClass> with a N numbers of items. I process with the following code
foreach (var item in myList)
{
ThreadPool.QueueUserWorkItem(DoWorkWithItem, item);
}
private void DoWorkWithItem(object threadContext)
{
///...
}
but i need get report what item was proccessed, where i can get a Event or something for this task
create event and call it after threat iteration finished, like this:
public class ClassWithDelegate
{
public delegate void ProgressReportEventHandler();
public static ProgressReportEventHandler ProgressReport { get; set; };
private void DoWorkWithItem(object threadContext)
{
// angry code
if (ProgressReport != null)
ProgressReport();
}
}
public class Subscriber
{
public Subscriber()
{
ClassWithDelegate.ProgressReport += ProgressReport;
}
public void ProgressReport()
{
//todo
}
}
or use BackgroundWorker, it has this event
Related
I have long process and sometimes I want to return process status.
I have the following code:
public delegate Task DeviceOnSureMdmResetIMEIHandler(TabletWithoutDeviceIdDto sureMdmTablet, string oldIMEI);
public delegate Task LocalTabletGroupIsNotFoundHandler(TabletWithoutDeviceIdDto sureMdmTablet);
public delegate Task ExistingDeviceIsNotTabletHandler(Device device);
public delegate Task ErrorCombiningDevicesHandler(string result);
public interface ISureMdmTabletService
{
List<TabletWithoutDeviceIdDto> Tablets { get; set; }
event DeviceOnSureMdmResetIMEIHandler DeviceOnSureMdmResetIMEIEvent;
event LocalTabletGroupIsNotFoundHandler LocalTabletGroupIsNotFoundEvent;
event ExistingDeviceIsNotTabletHandler ExistingDeviceIsNotTabletEvent;
event ErrorCombiningDevicesHandler ErrorCombiningDevicesEvent;
List<string> DeviceIsChanged(TabletWithoutDeviceIdDto newItem, Device dbDevice);
string ImeiIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet);
List<string> TabletIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet);
bool ShouldPhoneNumberBeUpdated(TabletWithoutDeviceIdDto newItem, Device device);
bool ShouldICCIDBeUpdated(TabletWithoutDeviceIdDto newItem, Device device);
bool ShouldIMSIBeUpdated(TabletWithoutDeviceIdDto newItem, Device device);
void UpdateAlreadyExistingTablets();
void AddNewTablets();
void DeletedTablets();
Task DuplicateImeiRecordsAsync();
Task<UpdateTabletsFromSureMdmResultDto> UpdateTabletsSaveChangesAsync();
}
public class SureMdmTabletService : ISureMdmTabletService
{
public event DeviceOnSureMdmResetIMEIHandler DeviceOnSureMdmResetIMEIEvent;
public event LocalTabletGroupIsNotFoundHandler LocalTabletGroupIsNotFoundEvent;
public event ExistingDeviceIsNotTabletHandler ExistingDeviceIsNotTabletEvent;
public event ErrorCombiningDevicesHandler ErrorCombiningDevicesEvent;
private List<TabletWithoutDeviceIdDto> _tablets;
public List<TabletWithoutDeviceIdDto> Tablets
{
get
{
return _tablets;
}
set
{
_tablets = value;
}
}
private List<string> _tabletsIMEI { get; set; }
private List<string> _tabletsIds { get; set; }
private List<Tablet> _alreadyExistsTablets { get; set; }
private List<Tablet> _allTablets { get; set; }
private readonly ICombine2DevicesIntoOneService _combine2DevicesIntoOneService;
private readonly DataContext _dbContext;
private readonly IMapper _mapper;
private DeviceModel _deviceModel;
public SureMdmTabletService(DataContext dbContext, ICombine2DevicesIntoOneService combine2DevicesIntoOneService)
{
// .....
}
public List<string> DeviceIsChanged(TabletWithoutDeviceIdDto newItem, Device dbDevice)
{
// .....
}
public string ImeiIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet)
{
// .....
}
public List<string> TabletIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet)
{
// .....
}
// .....
public void UpdateAlreadyExistingTablets()
{
**DeviceOnSureMdmResetIMEIEvent.Invoke(...);**
}
}
and wrapper about it:
public interface ISureMdmTabletUpdateCommandService
{
Task<UpdateTabletsFromSureMdmResultDto> CommandAsync(List<TabletWithoutDeviceIdDto> tablets);
}
public class SureMdmTabletUpdateCommandService : ISureMdmTabletUpdateCommandService
{
private readonly ISureMdmTabletService _sureMdmTabletService;
public SureMdmTabletUpdateCommandService(ISureMdmTabletService sureMdmTabletService)
{
_sureMdmTabletService = sureMdmTabletService;
}
public async Task<UpdateTabletsFromSureMdmResultDto> CommandAsync(List<TabletWithoutDeviceIdDto> tablets)
{
_sureMdmTabletService.Tablets = tablets;
_sureMdmTabletService.UpdateAlreadyExistingTablets();
_sureMdmTabletService.AddNewTablets();
_sureMdmTabletService.DeletedTablets();
await _sureMdmTabletService.DuplicateImeiRecordsAsync();
return await _sureMdmTabletService.UpdateTabletsSaveChangesAsync();
}
}
this wrapper is used by client app:
var resultTablets = await _sureMdmTabletUpdateCommandService.CommandAsync(tablets);
I want to subscribe on events from client app like this:
_sureMdmTabletUpdateCommandService.DeviceOnSureMdmResetIMEIEvent +=
but ISureMdmTabletUpdateCommandService does not have, nested class has it. How to throw these events carefully and without dummy intermediate events inside ISureMdmTabletUpdateCommandService ?
Having wrapper requires you to have all the infrastructure on it and you are not required to have full event there just a intermediate methods to attach and detach so you can do explicit event implementation:
object objectLock = new Object();
event DeviceOnSureMdmResetIMEIHandler DeviceOnSureMdmResetIMEIEvent
{
add
{
lock (objectLock)
{
_sureMdmTabletService.DeviceOnSureMdmResetIMEIEvent += value;
}
}
remove
{
lock (objectLock)
{
_sureMdmTabletService.DeviceOnSureMdmResetIMEIEvent -= value;
}
}
}
the lock is required as += and -= operations are not atomic its read modify write.
Having that sad I do not recommend using events at all. I would just passed as a parameter an Action<TabletWithoutDeviceIdDto, string> not even declaring custom delegate for it. Its just simpler approach and it can event support asynchronous operation if you use Func<TabletWithoutDeviceIdDto, string, Task> in contrast to events.
I am creating a car simulator where I have a key that turns on an engine. The engine is tied to a specific key with a callback method which calls the OnEngineTurn method which raises the event. No matter what I do to the EventHandler, I it never works because it always is null. Here is the code below. I am relatively new to C# so any help is appreciated
public delegate void MyEventHandler(object sender, EventArgs e);
class Engine
{
public event MyEventHandler EngineTurn;
//raise the event
protected virtual void OnEngineTurn(EngineEventArgs e)
{
MyEventHandler engineTurn = EngineTurn;
if (engineTurn != null)
{
MessageBox.Show("Hello World");
engineTurn(this, e);
}
else
{
MessageBox.Show("Null");
}
}
public CarKey GetNewKey()
{
return new CarKey(new KeyCallBack(OnEngineTurn));
}
}
class EngineEventArgs : EventArgs
{
public string name { get; set; }
}
delegate void KeyCallBack(EngineEventArgs e);
class CarKey
{
//we need a way to hook the engine up to the car so we don't crank, but one car with one key
private KeyCallBack keyCallBack;
public CarKey(KeyCallBack callBackDelegate)
{
this.keyCallBack = new KeyCallBack(callBackDelegate);
}
public void TurnTheKey(EngineEventArgs e)
{
if (keyCallBack != null)
{
MessageBox.Show("A");
keyCallBack(e);
}
}
}
carKey = engine1.GetNewKey() should tie a specific key to a specific engine with a callback method that calls back to the EngineTurn Event.... carKey.TurnTheKey(engineEventArgs) is suppose to raise the event.... Below is the constructor for CarKey... I have it inside the Engine class for the callback method...
carKey = engine1.GetNewKey();
engineEventArgs = new EngineEventArgs();
carKey.TurnTheKey(engineEventArgs);
public CarKey GetNewKey()
{
return new CarKey(new KeyCallBack(OnEngineTurn));
}
Solved the problem
class Simulator
{
private Engine engine = new Engine();
private Transmission transmission;
CarKey carKey;
//public ObservableCollection<string> FanSays { get { return fan.FanSays; } }
//public ObservableCollection<string> PitcherSays { get { return pitcher.PitcherSays; } }
// public int Trajectory { get; set; }
//public int Distance { get; set; }
public Simulator()
{
transmission = new Transmission(engine);
carKey = engine.GetNewKey();
}
public async void StartSimulator()
{
EngineEventArgs engineEventArgs = new EngineEventArgs("America!");
await new MessageDialog("made it inside the start method").ShowAsync();
carKey.StartTheEngine(engineEventArgs);
}
}
Just as title says, how to fire property when I do propertyList.Add(something)
So code is:
private List<string> _SomeStrings;
public List<string> SomeStrings
{
get
{
return _SomeStrings;
}
set
{
_SomeStrings = value;
}
odAdd //this is what I need but do not know how to do it
}
You can either use a built in type such as ObservableCollection; examples here and here. MSDN page.
You could also create a new class that derives from List, and overloads the functions you wish to hook.
class ListWithAdd<T> : List<T>
{
public new void Add(T item)
{
base.Add(item);
DoStuff();
}
}
Create a custom generic list, create a event which will be fired when an item is added in the list.
like this:
namespace ConsoleApplication2
{
public delegate void OnAddEventHandler(object item);
public class AddEventArgs : EventArgs
{
public object item { get; set; }
}
public class MyList<T> : List<T>
{
public event OnAddEventHandler OnAdd;
public new void Add(T item)
{
base.Add(item);
if (OnAdd != null)
{
OnAdd(item);
}
}
}
class Program
{
static void Main(string[] args)
{
MyList<string> lst = new MyList<string>();
lst.OnAdd += (item) => {
Console.WriteLine("new item added: " + item);
};
lst.Add("test");
Console.ReadLine();
}
}
}
here's fiddle for you: https://dotnetfiddle.net/tc5Mq1
Look into observablecollection https://learn.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.observablecollection-1?view=netframework-4.7.2
I was given a generic API class, that contains a custom event which always needs to be invoked by the main UI thread.
My job is to banish these invocation call from the custom class, to make it "painless".
It should be synchronized like the default events in WinForms (eg the Timer "Elapsed" event, which also needs no invocation when it published values to a text box)
Is it possible to solve this, since the custom class needs to know where to invoke?
Here's the (important part of the) code:
public class ContactSensorHelper
{
public event OnReleaseStateChanged ReleaseStateChanged;
public delegate void OnReleaseStateChanged(ContactSensorEventArgs e);
private ContactSensorEventArgs.ReleaseState recentReleaseState;
public void ReportStateChanged()
{
if (ReleaseStateChanged != null)
ReleaseStateChanged(new ContactSensorEventArgs()
{
State = recentReleaseState
});
}
public class ContactSensorEventArgs : EventArgs
{
//......
public ReleaseState State { get; set; }
//......
public enum ReleaseState
{
FullReleased,
PartlyReleased,
NotReleased
}
}
}
The call from main UI:
public void SensorInit()
{
//....
sensorHelper.ReleaseStateChanged += releaseStateChanged;
//....
}
private void releaseStateChanged(ContactSensorEventArgs e)
{
//example
textBox1.Text = e.State.ToString(); // Thread exception (obviously)
}
Does anybody have me a hint to start?
You could do this by using your own event calling, and storing a reference to the thread, when the event is attached.
With the event add/remove syntax, you can have the caller attach to the event like before, but internally you store a list, with a reference to the thread (using an AsyncOperation) and the delegate to be called (used a Tuple containing both in the example)
Below is an example. I tested it, and it worked as expected when testing, but you might have to add some locking of the list to make it thread safe in case events are added/removed simultaneously.
public class ContactSensorHelper:IDisposable
{
public delegate void OnReleaseStateChanged(ContactSensorEventArgs e);
private ContactSensorEventArgs.ReleaseState recentReleaseState;
public void ReportStateChanged()
{
if (statechangedList.Count > 0)
{
var e = new ContactSensorEventArgs()
{
State = recentReleaseState
};
statechangedList.ForEach(t =>
t.Item1.Post(o => t.Item2((ContactSensorEventArgs)o), e));
}
}
List<Tuple<AsyncOperation, OnReleaseStateChanged>> statechangedList = new List<Tuple<AsyncOperation,OnReleaseStateChanged>>();
public event OnReleaseStateChanged ReleaseStateChanged
{
add
{
var op = AsyncOperationManager.CreateOperation(null);
statechangedList.Add(Tuple.Create(op, value));
}
remove
{
var toremove = statechangedList.Where(t => t.Item2 == value).ToArray();
foreach (var t in toremove)
{
t.Item1.OperationCompleted();
statechangedList.Remove(t);
}
}
}
public void Dispose()
{
statechangedList.ForEach(t => t.Item1.OperationCompleted());
statechangedList.Clear();
}
public class ContactSensorEventArgs : EventArgs
{
//......
public ReleaseState State { get; set; }
//......
public enum ReleaseState
{
FullReleased,
PartlyReleased,
NotReleased
}
}
}
I have this sample code for async operations (copied from the interwebs)
public class LongRunningTask
{
public LongRunningTask()
{
//do nowt
}
public int FetchInt()
{
Thread.Sleep(2000);
return 5;
}
}
public delegate TOutput SomeMethod<TOutput>();
public class GoodPerformance
{
public void BeginFetchInt()
{
LongRunningTask lr = new LongRunningTask();
SomeMethod<int> method = new SomeMethod<int>(lr.FetchInt);
// method is state object used to transfer result
//of long running operation
method.BeginInvoke(EndFetchInt, method);
}
public void EndFetchInt(IAsyncResult result)
{
SomeMethod<int> method = result.AsyncState as SomeMethod<int>;
Value = method.EndInvoke(result);
}
public int Value { get; set; }
}
Other async approaches I tried required the aysnc page attribute, they also seemed to cancel if other page elements where actioned on (a button clicked), this approach just seemed to work.
I’d like to add a cancel ability and exception handling for the longRunningTask class, but don’t erm, really know how.
In example:
public class ValueEventArgs : EventArgs
{
public int Value { get;set;}
}
public class ExceptionEventArgs : EventArgs
{
public Exception Exception { get;set;}
}
public class LongRunningTask
{
private bool canceled = false;
public event EventHandler<ValueEventArgs> Completed = delegate {}
public event EventHandler<ExceptionEventArgs> GotError = delegate {}
public void Cancel()
{
canceled = true;
}
public void FetchInt()
{
try
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if (canceled)
return;
result++;
}
Completed(this, new ValueEventArgs {Value = result});
}
catch(Exception exc)
{
GotError(this, new ExceptionEventArgs { Exception = exc });
}
}
public void BeginFetchInt()
{
ThreadPool.QueueUserWorkItem(i => FetchInt());
}
}
And somewhere:
LongRunningTask task = new LongRunningTask();
task.Completed +=new EventHandler<ValueEventArgs>(task_Completed);
task.GotError +=new EventHandler<ExceptionEventArgs>(task_GorError);
task.BeginFetchInt();
//in any moment until it calculates you may call:
task.Cancel();