What to call a class/method for 'waiting till user has stopped clicking' - naming

I have a static class with a single method, which I use for delaying an action until the user has stopped clicking/typing/whatever:
public void SomeControl_Click(object sender, EventArgs e)
{
// DoSomething once user has stopped clicking for 300ms
MyClass.MyMethod(() => DoSomething(), 300);
}
Currently I've gone for DelayAction.StartNew, but that doesn't really convey what it does. I wondered about Stabilize, but I'm not sure that's any clearer.

DelayAction.OnIdleUser
Since "being idle" means no activity, I'd go for this naming.

DelayAction.OnTimerExpired()

Related

IrrKlang Sound Library and Stop Event threads

I have a question about using external c++ library (irrKlang.dll) which is an audio playback engine. Now, the problem is that when I get a SoundStopped event out of it, and do an action in the main form, all kinds of stack related errors arise. Let me show the code:
namespace WindowsFormsApplication4
{
public class IsoundFinished : ISoundStopEventReceiver
{
public delegate void OnSoundStoppedEventHandler(object source, EventArgs e);
public event OnSoundStoppedEventHandler IStopped;
public void OnSoundStopped(ISound iSound, StopEventCause reason, object userData)
{
if (reason.ToString() == "SoundFinishedPlaying")
IStopped?.Invoke(this, EventArgs.Empty);
}
}
}
That is an extended class for me to do custom actions (for example - if sound finished, raise the event...) I am creating an instance of it, for the event action to get exposed in my main Form1 class:
IsoundFinished iStopReceiver = new IsoundFinished();
Now in my main form, I have this line in my Form1() method, just under my InitializeComponent():
iStopReceiver.IStopped += new soundFinished.OnSoundStoppedEventHandler(OnStopped);
It's for subscribing to the event handler. And finally - my OnStopped() method which is supposed to do stuff when the song ends it's playback - it's on the same Form1:
private void OnStopped(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke(new Action<object, EventArgs>(OnStopped), sender, e);
return;
}
btnStop1.PerformClick();
}
My Stop1 button method is (for those who work with the IrrKlang) ISound.Stop(); and few more lines of code, dealing with the display of playlist and so on. Although I have invoked it from the main UI thread - which should provide me with some degree of thread misalignment protection, all kinds of errors appear, mostly
Cannot evaluate expression because a native frame is on the top of the call stack.
Of course, if I do it without event handler, ISound.Stop(); drops the sound from the engine, like it should. I know something wrong is happening with the threads, but I can't figure out what's going on. If someone would give me few tips, I'd appreciate that a lot.
Well it seems I've solved it myself ! It's all about understanding how the threads are working in Visual C#. The problem was this : I was actually PAUSING the background thread where my audioengine was triggering the event - so 'till I performed an action after INVOKE in the main UI thread, background thread was paused along with the whole irrKlang engine. It was unable to purge itself properly, so it's call stack got clogged!
Using BEGININVOKE solved the problem, as it doesn't PAUSE the background task. It lets it run instead. Diagram on this answer gave me much needed piece of info I was looking for.
Maybe someone will need this answer too, glad I helped myself :P
private void OnStopped(object sender, EventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new Action<object, EventArgs>(OnStopped), sender, e);
return;
}
btnStop1.PerformClick();
}

Action listeners not firing

I've been developing with codenameone for over a year, and I never ran into this problem before, I feel like I'm losing my mind. I just redesigned one part of an app I'm working on, and now the ActionListeners aren't firing. I'm attaching them to a Button and a SpanButton in the code:
ActionListener goToDoc = new ActionListener() {
String mDocId = docId;
#Override
public void actionPerformed(ActionEvent evt) {
mStateMachine.currentExpertId = mDocId;
mStateMachine.showForm("DoctorDetails", null);
}
};
name.addActionListener(goToDoc);
Util.downloadImageToStorage(mStateMachine.URL_PREFIX+"images/doctors/"+(String)value.get("doc_pic"),
"doc_post_pic_"+(String)value.get("doc_id")+".png", new Callback<Image>() {
#Override
public void onSucess(Image img) {
pic.setIcon(img.scaledWidth(mStateMachine.getProportionalWidth(.23)));
StateMachine.applyGreenBorder(pic);
pic.addActionListener(goToDoc);
pic.getParent().revalidate();
}
#Override
public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
System.out.println("Unable to download expert profile picture");
}
});
When I debug the code, the components do show that the ActionListener is attached, but the actionPerformed method is never reached, no matter how many times I click on the buttons. I experience this problem both on the simulator and on an Android device. I have yet to test on an iPhone.
Did you set a parent to be a lead component or focusable?
The reason the click event wasn't firing was because the Components weren't enabled, possibly a bug in the BUI Builder. After checking off the 'Enabled' and 'Focusable' checkboxes in the GUI Builder, and seeing they were unchecked every time I went back to that form, I just used component.setFocusable(true) and component.setEnabled(true) in the code, and it worked fine after that.

Monotouch - Getting notified when network status changed

I'm familiar with the "reachability" class to check if there is an internet connection:
https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs
I was now wondering if there is any way to get notified when the Network status has changed?
Let's say that my collection is loaded when there is no internet. The images will not get loaded. I would like to trigger the download again if the iPad suddenly gets internet connection again.
In the reachability class, I see following code:
public static event EventHandler ReachabilityChanged;
static void OnChange (NetworkReachabilityFlags flags)
{
var h = ReachabilityChanged;
if (h != null)
h (null, EventArgs.Empty);
}
However, if I set a breakpoint in the "OnChange" method, it's not called when I switch off Wifi.
Any ideas?
Regards, Matt
Ok, I should have find the answer myself actually.
You should call following functions first before you will be notified:
Reachability.InternetConnectionStatus ();
Reachability.LocalWifiConnectionStatus ();
Reachability.RemoteHostStatus ();
Calling these 3 static function calls will initialize all the necessary objects. Those 3 objects will get notified when there is a change and call "OnChange" in the Reachability call (which notifies your Event Handler).
(off course, even better is to create a special function which initialize all the needed objects so you don't need to call these 3 functions as they will really test the network connection already)
You should be able to simply add an event handler to the ReachabilityChanged event, something like (the not test compiled);
Reachability.ReachabilityChanged += c_ReachabilityChanged;
static void c_ReachabilityChanged(object sender, EventArgs e)
{
Console.WriteLine("Here I can handle that network reachability changed.");
}
Just calling this would work, can be written in AppDelegate
Reachability.RemoteHostStatus ();
Reachability.ReachabilityChanged += ReachabilityChanged;
static void ReachabilityChanged(object sender, EventArgs e){//Do Something};

ServiceBus : Asynchronous MessageSender.BeginSend() , Should I put Thread.Sleep()

Working Code here
static MessageSender TopicClient;
public static void SendTopicMessage(BrokeredMessage message)
{
IAsyncResult result = TopicClient.BeginSend(message, processEndSend, TopicClient);
Thread.Sleep(5000);
}
public static void processEndSend(IAsyncResult result)
{
MessageSender messageSender = result.AsyncState as MessageSender;
messageSender.EndSend(result);
}
The above code is working. But I don't know why should I put Thread.Sleep(). I dont want to keep Thread.Sleep(). But It's not working I remove that Thread.Sleep(). Any Suggestion?
I think you are calling the SendTopicMessage from a thread...
So if you remove Sleep() here your thread Terminated once its sent your first message...If you put Sleep(5000), you are sending next messages with in the 5 sec time duration so the thread kept alive.
I think You are designing Bad Architecture. Correct me If i am wrong.
For Clear understanding of your flow... Please post enough code snippets...

Creating a JavaFX Dialog Box

I am coding a javafx program and i need to create and use my own Stage based (Javafx.Stage) dialog box for showing messages and confirmations. I have written all the necessary code by now but i have a problem:
A dialog box must stop execution of rest of the code until a respond is given like "yes" or "no" or "retry". When i use my dialog box like "DialogBox.ShowMessage", a stage appears with message and buttons. But, as you may think, the rest of the code continues to execute. How can i get around this? When i create the stage, it must stop the other threads or the thread that it depends on. I have searched through internet and here, but i can not find exact solution. One idea is using "javafx.event.EventDispatcher" or "javafx.event.EventDispatchChain" but i couldn't figure out how to use them. And another idea is using "java.awt.EventQueue". And here is somthing that can help: I have a control of stage show and hide events and showing or hiding eventhandlers. I think som sort of thread queue can be used in one of these spesific sections.
I hope i clarified the situation enough. Briefly, ı need to manage threads while using another stage with my own code.
Thank you.
About execution suspending there is a Jira issue for it http://javafx-jira.kenai.com/browse/RT-19783.
As a workaround, I have no idea how to use EventDispatcher and EventDispatchChain to overcome this problem but you can send the EventHandlers as parameter. For instance,
public static void ShowMessage(final EventHandler<ActionEvent> okAction, final EventHandler<ActionEvent> cancelAction){
// Define and add buttons to the "view" and show message
btnOk.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
okAction.handle(null);
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
cancelAction.handle(null);
}
});
}
and use it as,
DialogBox.ShowMessage(
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent arg0) {
// Do stuff when "ok" clicked.
},
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent arg0) {
// Do stuff when "cancel" clicked.
});
I agree with this is a kind of "winding" way however.
Siteye hoş geldin ve kolay gelsin.

Resources