Create a infinity loop and stop it - loops

I'd like to create a infinity loop when I push a button and stop it when I release the button, so I create this, but it doesn't stop when I release the button...
can you help me?
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if (v.getId() == R.id.up) {
for(;;){
//make my action
if(event.getAction() == MotionEvent.ACTION_UP) {
break;
}
}
}
}
return false;
}
}

The infinity loop will drive the EventQueue into no respond to any other Events. So the MotionEvent.ACTION_UP could not be handled also. You should make the event handler method faster. Otherwise the event dispatching thread could hang.

Related

Want to disable the command based on canexecute

I have a simple DelegateCommand from Telerik. I have a case,
I have a screen with a textbox and a button. Whenever I click save the button should be disabled and should not accept any clicks until the operation completes.
The implementation is as below,
public DelegateCommand SaveRemarksCommand
{
get
{
return _saveRemarksCommand = new DelegateCommand((r) =>
{
CanSaveRemarks = false;
SaveRemarksCommand.InvalidateCanExecute();
SaveRemarks(null);
},
(result) =>
{
return CanSaveRemarks;
});
}
}
Here I am manipulating the CanSaveRemarks as false until the SaveRemarks executes.
The problem now is if I click multiple times fast on the button the button accepts multiple clicks saving duplicates.
You can additionally check the CanSaveRemarks property in your command handler.
This property is already available to you, so you don't need to introduce something else.
new DelegateCommand((r) =>
{
if (!CanSaveRemarks)
{
return;
}
CanSaveRemarks = false;
try
{
SaveRemarksCommand.InvalidateCanExecute();
SaveRemarks(null);
}
finally
{
CanSaveRemarks = true;
}
},
(result) =>
{
return CanSaveRemarks;
});

Preventing line breaks - ENTER keypresses. Instead - fire custom event

I'd like to use Aloha to WYSIWYG-edit single-line headlines.
For that I need to intercept ENTER keypress (to prevent line-break) and fire custom save event.
Is there a way to do that?
Thanks!
Here's what worked for me on "hotfix" branch:
Aloha.bind('aloha-smart-content-changed', function(e, data) {
value = data.editable.getContents();
// save it to database.
});
Aloha.bind('aloha-command-will-execute', function(e, data) {
if (data.commandId == 'insertlinebreak') {
data.preventDefault = true;
}
});
More info here:
https://github.com/alohaeditor/Aloha-Editor/issues/1502

Busy indicator does not show until data shows

I have a control in my project that provides a busy indicator (rotating circle). I'd like it to run when a user selects a file to load data into a data grid. The busy indicator does not show up until my data grid is populated though. How can I get my busy indicator to show while the data is being retrieved? I believe I'm supposed to use a thread, but am not too knowledgeable with them yet and am trying to learn. I've tried many different ways and below is my latest attempt, which I do not know if I am anywhere close.
public void DoWork()
{
this.StartProgressBar();
Task.Factory.StartNew(() =>
{
UIDispatcher.Current.BeginInvoke(() =>
{
if (fileCompleted != null && !string.IsNullOrEmpty(fileCompleted.SelectedFile))
{
this.TestResults.Clear();
LoadedFileInfo info = this.Model.OpenTestCompleted(fileCompleted.SelectedFile);
foreach (var model in info.Sequence.Models)
{
foreach (var details in model.Tests)
{
this.TestResults.Add(new TestResultsModel(details, model.Name.Substring(0, model.Name.IndexOf('.'))));
}
}
}
});
});
}
private void StartProgressBar()
{
TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
CancellationToken cancelationToken = new CancellationToken();
Task.Factory.StartNew(() => this.StopProgressBar()).ContinueWith(
m =>
{
this.ToggleProgressBar = true;
},
cancelationToken,
TaskContinuationOptions.None,
scheduler);
}
private void StopProgressBar()
{
this.ToggleProgressBar = false;
}
I really agree with #Ben, you should research how to use Tasks. You are creating background threads, and doing work on the UI thread in them anyway, which inevitably hold the UI thread. Try something simpler and see if it works. As far as your cancellation token goes, I don't see how and were you'd be able to reset it, as it is not a property in your class, so here's a sample without it..
How about something like this:
public void DoWork()
{
//done on the UI thread
this.ToggleProgressBar = true;
//done on the background thread, releasing UI, hence should show the progress bar
Task.Factory.StartNew(() =>
{
if (fileCompleted != null && !string.IsNullOrEmpty(fileCompleted.SelectedFile))
{
this.TestResults.Clear();
LoadedFileInfo info = this.Model.OpenTestCompleted(fileCompleted.SelectedFile);
foreach (var model in info.Sequence.Models)
foreach (var details in model.Tests)
this.TestResults.Add(new TestResultsModel(details, model.Name.Substring(0, model.Name.IndexOf('.'))));
}
//after all that (assumingly heavy work is done on the background thread,
//use UI thread to notify UI
UIDispatcher.Current.BeginInvoke(() =>
{
this.ToggleProgresBar = false;
}
});
}

C# mixed use of Task and Dispatcher.Invoke, why it halts?

I tried below snippet:
public Task RunUiTask(Action action)
{
var task = Task.Factory.StartNew(() =>
{
Dispatcher.Invoke(DispatcherPriority.Background, action);
});
return task;
}
private void OnCreateTask(object sender, RoutedEventArgs e)
{
var task = RunUiTask(() =>
{
for(int i=0;i<10;i++)
{
ResultTextBlock.Text += i.ToString();
}
});
task.Wait(); //(a) Program stopped here
ResultTextBlock.Text += "Finished"; //(b) Never called;
}
I couldn't understand why, when OnCreateTask (a button click event handler) is called, the program halts at (a), and (b) is never called.
Note: I know I can use Dispatcher.BeginInvoke to make program responsive, but this is not my concern here.
Can any body tell why the program halts at (a), and why (b) is never called? Thanks.
The call of
Dispatcher.Invoke(DispatcherPriority.Background, action);
will execute Action in your UI Thread and will return after Action is executed.
The Problem is, that your UI Thead is blocked because of the task.Wait() in your OnCreateTask, so the Action will never be executed and you have a Deadlock.
EDIT
Instead of your task.Wait() you should use a Continuation and Update ResultTextBlock.Text
task.ContinueWith(t=>{
ResultTextBlock.Text += "Finished";
}, TaskScheduler.FromCurrentSynchronizationContext());

Why winform filckers on button click event

I have winform app which has 3 forms, in 2 forms I am taking user inputs and when user click button my form flickers (i means its like form disappear and appears, and sometimes sends my app to back of other app). Can anyone tell me what is the problem with my application? Has anyone faced this kind of strange behavior in winform?
EDITED
Cursor currentCursor = this.Cursor;
try
{
this.Cursor = Cursors.WaitCursor;
this.btnSave.Enabled = false;
if (isDataModified)
{
if (CheckMandatoryData(mpgUserInfo, ref errorMessage))
{
AppMessageBoxResult appMessageBoxResult =
AppMessageBox.ShowQuestion("Confirmation",
"Do you want to continue?", Environment.NewLine));
if (appMessageBoxResult == AppMessageBoxResult.Yes)
{
if (customerInformation != null)
{
//Assigning value to variable and saving
RefreshData();
}
}
}
else
{
AppMessageBoxResult appMessageBoxResult =
AppMessageBox.ShowQuestion("Confirmation",
"Do you want to continue to save?",
errorMessage, Environment.NewLine));
if (appMessageBoxResult == AppMessageBoxResult.Yes)
{
if (customerInformation != null)
{
//Assigning value to variable and saving
RefreshData();
}
}
errorMessage.Clear();
}
}
}
catch (Exception exception)
{
AppMessageBox.ShowException("Error", exception.Message,
exception.InnerException.Message, exception);
}
finally
{
this.Cursor = currentCursor;
this.btnSave.Enabled = true;
}
here AppMessageBox is our extended MessageBox and is part of our custom framework.
I think you are having focus / modal dialog issues in your AppMessageBox

Resources