I have created Scheduler class which is call MailBot.Start static method while ASP.NET application is started. I suspect that the code is not thread safe because some variables(maybe, not sure about this) in MailBot.Start method is mixed. Is it true?
I would like to have only one method running for the whole ASP.NET app.
void Application_Start(object sender, EventArgs e)
{
WebHelper.Scheduler(TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(10), MailBot.Start);
}
public static class MailBot
{
public static void Start()
{
//The actual code...
}
}
public delegate void SchedulerEvent();
public static void Scheduler(TimeSpan firstTime, TimeSpan interval, SchedulerEvent callback)
{
var timer = new System.Timers.Timer { Interval = firstTime.TotalMilliseconds };
timer.Elapsed += delegate
{
timer.Enabled = false;
try
{
timer.Interval = interval.TotalMilliseconds;
callback();
}
finally
{
timer.Enabled = true;
}
};
timer.Start();
}
Related
I am trying to call the event handler from droid side, however it doesn't get fired. everyone else works however this event doesn't get fired. I am not sure what am I doing wrong. I have my dependency service registered at MainActivity and set dependency in my service.
public partial class MainPage : ContentPage
{
DocumentResults results;
IScanService scanService;
public MainPage()
{
InitializeComponent();
results = new DocumentResults();
BindingContext = new MainPageViewModel();
System.Diagnostics.Debug.WriteLine("test cw");
Console.WriteLine("test cw");
scanService = DependencyService.Get<IScanService>();
scanService.ResultsParsedEvent += (s, ev) => { ResultsParsed(null, ev); };
}
void Button_Clicked(System.Object sender, System.EventArgs e)
{
scanService.ScanService();
if (!String.IsNullOrWhiteSpace(test))
{
scanService.Parsing(test);
}
}
private void ResultsParsed(DocumentResults results,EventArgs e)
{
Console.WriteLine("update ");
testLbl.Text = results.Name;
}
}
My interface
public interface IScanService
{
event EventHandler ResultsParsedEvent;
string ScanService();
void Parsing(string test);
void resultsParsed(DocumentResults results, EventArgs e);
}
Droid implementation
public class RegService : IScanService
{
public event EventHandler ResultsParsedEvent;
DocumentResults results;
public string Test;
public String ScanService()
{
Test = "scan";
return Test;
}
public void Parsing(string test)
{
Test = "parsing";
var results= new DocumentResults();
results.Name = Test;
Thread thread1 = new Thread(() => resultsParsed(results,null));
System.Threading.Thread.Sleep(10);
resultsParsed(results,null);
}
public void resultsParsed(DocumentResults results, EventArgs e)
{
ResultsParsedEvent?.Invoke(results, e);
}
}
I want to execute Java code when Service is started but I cannot find suitable Java method for this:
service.setOnRunning(new EventHandler<WorkerStateEvent>()
{
#Override
public void handle(WorkerStateEvent t)
{
Start some logic
}
});
service.setOnSucceeded(new EventHandler<WorkerStateEvent>()
{
#Override
public void handle(WorkerStateEvent t)
{
Stop some logic
}
});
How I can call Java Method only once when Service is started?
You can easily control Service start time, thus there is no special method.
Just add any code you want to be run at start to the call method:
private class MyService extends Service<Void> {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
// "onStart" code
System.out.println("Service started");
// actual service code
//do stuff
return null;
}
};
}
}
When I call the PressCommand.RaiseCanExecuteChanged(); in the TimerOnElapsed method, nothing happened.
What could be the problem?
(GalaSoft.MvvmLight.WPF4 v4.0.30319 and GalaSoft.MvvmLight.Extras.WPF4 v4.0.30319)
Here is my test code:
using System.Timers;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace CommandTest {
public class MainWindowVM : ViewModelBase {
public MainWindowVM() {
PressCommand = new RelayCommand(
() => MessageBox.Show("Pressed"),
() => _canExecute);
PressCommand.CanExecuteChanged += (sender, args) => System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToLongTimeString() + " CanExecuteChanged");
_timer = new Timer(1000);
_timer.Elapsed += TimerOnElapsed;
_timer.Enabled = true;
}
public RelayCommand PressCommand { get; private set; }
#region Private
private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
_canExecute = !_canExecute;
PressCommand.RaiseCanExecuteChanged();
System.Diagnostics.Debug.WriteLine("At {0} enabled: {1}", elapsedEventArgs.SignalTime.ToLongTimeString(), _canExecute);
}
private readonly Timer _timer;
private bool _canExecute;
#endregion
}
}
Thank you in advance
Explanation:
The TimerOnElapsed method runs on a Worker Thread but to invoke the PressCommand.RaiseCanExecuteChanged(); must be on the UI thread.
So this is the solution, the updated TimerOnElapsed method:
private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
_canExecute = !_canExecute;
Application.Current.Dispatcher.Invoke(PressCommand.RaiseCanExecuteChanged);
System.Diagnostics.Debug.WriteLine("At {0} enabled: {1}", elapsedEventArgs.SignalTime.ToLongTimeString(), _canExecute);
}
I implement standart scenario in asp.net session per reqest.
My asp.net module:
public class NHibernateSessionModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
context.EndRequest += context_EndRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
var session = SessionManager.SessionFactory.OpenSession();
session.BeginTransaction();
CurrentSessionContext.Bind(session);
}
void context_EndRequest(object sender, EventArgs e)
{
var session = SessionManager.CurrentSession;
if (session != null)
{
try
{
if (session.Transaction != null && session.Transaction.IsActive)
session.Transaction.Commit();
}
catch (Exception ex)
{
session.Transaction.Rollback();
throw new ApplicationException("Error committing database transaction", ex);
}
finally
{
session.Close();
}
}
CurrentSessionContext.Unbind(SessionManager.SessionFactory);
}
}
My sessionManager is thread-safe singletone:
public class SessionManager
{
private readonly ISessionFactory sessionFactory;
public static ISessionFactory SessionFactory
{
get { return Instance.sessionFactory; }
}
private ISessionFactory GetSessionFactory()
{
return sessionFactory;
}
public static ISession OpenSession()
{
return Instance.GetSessionFactory().OpenSession();
}
public static ISession CurrentSession
{
get
{
if (!CurrentSessionContext.HasBind(Instance.GetSessionFactory()))
return null;
return Instance.GetSessionFactory().GetCurrentSession();
}
}
public static SessionManager Instance
{
get
{
return NestedSessionManager.sessionManager;
}
}
private SessionManager()
{
Configuration configuration = new Configuration().Configure();
sessionFactory = configuration.BuildSessionFactory();
}
class NestedSessionManager
{
internal static readonly SessionManager sessionManager =
new SessionManager();
}
}
The main idea open session in begin of request and then use session through SessionManager.CurrentSession;
Session is stored in configured context:
<property name="current_session_context_class">web</property>
My repository:
public class RepositoryNew<T> : BaseRepository<T>, IDisposable
{
public RepositoryNew()
{
if (NHibernateSession == null)
//Start session for not web version
}
public void Dispose()
{
//flush session for not web version
}
protected override sealed ISession NHibernateSession
{
get
{
return SessionManager.CurrentSession;
}
}
}
Usage
protected void Page_Load(object sender, EventArgs e)
{
var repo = new RepositoryNew<Client>()
clients = repo.GetAll();
}
By some reason this repository doesn't use opened session in module.
CurrentSessionContext.HasBind(Instance.GetSessionFactory())
returns false, so my code starts second session in request.
At debugger I see that I have instantieted my SessionManager twice.
My be I have two different ISesssion factories.
I haven't ideas yet what's wrong. I have spent on it a lot of hours.
Maybe another thing open session in Http Begin Request because every http request will open new session like request static image you must change this strategy to eliminate this unnecessary session in every Http request you can read this blog and change your strategy http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx
It was strange error. When I remove link to SessionManager from my project, it starts work properly.
I'm using a NativeActivity with a child activity called Body and a ActivityAction called OnFault, this is my code:
[Designer(typeof(RetryDesigner))]
public sealed class Retry : NativeActivity {
public Activity Body { get; set; }
public ActivityAction<Exception> OnFault { get; set; }
protected override void CacheMetadata(NativeActivityMetadata metadata) {
metadata.AddChild(Body);
metadata.AddDelegate(OnFault);
base.CacheMetadata(metadata);
}
protected override void Execute(NativeActivityContext context) {
context.ScheduleActivity(Body, OnBodyCompleted, OnBodyFaulted);
}
void OnFaultCompleted(NativeActivityContext context, ActivityInstance instance) {
context.ScheduleActivity(Body);
}
void OnBodyCompleted(NativeActivityContext context, ActivityInstance instance) {
}
void OnBodyFaulted(NativeActivityFaultContext faultContext, Exception propagatedException, ActivityInstance propagatedFrom) {
faultContext.HandleFault();
if (OnFault != null) {
faultContext.ScheduleAction<Exception>(OnFault, propagatedException, OnFaultCompleted);
}
}
}
and my main looks like this:
static void Main(string[] args) {
Variable<Exception> ex = new Variable<Exception>();
DelegateInArgument<Exception> exception = new DelegateInArgument<Exception>();
Retry retry = new Retry {
Body = new Sequence {
Variables = { ex },
Activities = {
new Assign<Exception> {
To = new OutArgument<Exception>(ex),
Value = new InArgument<Exception>((env) => new Exception("test"))
},
new Throw {
Exception = ex
}
}
},
OnFault = new ActivityAction<Exception> {
Argument = exception,
Handler = new Sequence {
Activities = {
new WriteLine{
Text = new InArgument<string>(env =>
exception.Get(env).Message)
}
}
}
}
};
var workflow = new WorkflowInvoker(retry);
workflow.Invoke();
Console.WriteLine();
}
The problem is that the exception don't stop in the OnBodyFaulted callback and appear on the main as an unhedled expetion.
How can I stop the Exception inside the OnBodyFault callback, is there any state or property on the workflow to do that?
The problem is that in the OnFaultCompleted() you are calling context.ScheduleActivity(Body) again. This time without a fault handler so that causes the same fault to occur again and the complete workflow to fault.