Why Task canceled run again with new Task? - task-parallel-library

I have a example about Task. I start a new Task to do a LongTask and after I cancel the Task and after start a new Task to do a LongTask again. But the TaskA is run not normally. It run the work of Task canceled and new Task.
How to run a new Task immediately and the Task canceled don't run again ?
Here my codes:
class Program
{
private static CancellationTokenSource tokenSource = new CancellationTokenSource();
static void Main(string[] args)
{
Program a = new Program();
Task.Factory.StartNew(() => a.LongTask(), tokenSource.Token);
Console.ReadLine();
//After enter
tokenSource.Cancel();
//Create new CancellationTokenSource to make Task continue run
tokenSource = new CancellationTokenSource();
Task.Factory.StartNew(() => a.LongTask(), tokenSource.Token);
Console.ReadLine();
}
public void LongTask()
{
StringBuilder display = new StringBuilder();
while (!tokenSource.IsCancellationRequested)
{
display.Append("1");
Console.WriteLine(display);
Thread.Sleep(1000);
}
}
}

I will visualize what happens when the first task is sleeping
while (!tokenSource.IsCancellationRequested)
{
display.Append("1");
Console.WriteLine(display);
Thread.Sleep(1000);
// tokenSource.Cancel();
// tokenSource = new CancellationTokenSource();
// Task.Factory.StartNew second task
}
then after sleeping timeout, task #1 continues its loop to check tokenSource.IsCancellationRequested which is false (because this token is the new one).
Now you understand why two tasks are running that way. Let's try my code:
static void Main(string[] args)
{
var a = new Program();
var token = new CancellationTokenSource();
Task t1 = Task.Factory.StartNew(() => a.LongTask("#1", token1.Token), token1.Token);
Console.ReadLine();
token1.Cancel();
token = new CancellationTokenSource();
Task.Factory.StartNew(() => a.LongTask("#2", token2.Token), token2.Token);
Console.ReadLine();
}
public void LongTask(string name, CancellationToken killer)
{
var display = new StringBuilder();
while (!killer.IsCancellationRequested)
{
display.Append("1");
Console.WriteLine("{0} says: {1}",name, display);
Thread.Sleep(1000);
}
}
The trick here is to give each task a different token other than share same static token instance.

Related

Execute a collection of tasks int the order they where add to a list in batchs

I am wondering if anyone can help me, I am trying to get my head around how to execute concurrent tasks in batches and apologies if it is a silly question. However, I can only get them to execute all at the same time. I created a class below which is meant to be able to add task(s) to a collection and when the method ExecuteTasks() is called, run the tasks in batches in the order to which they where added in the list. As soon as the tasks are created they start to be executed it seems, is there a way around this.
public class TaskBatches
{
private readonly List<Task> _tasks;
private int _batchSize;
public TaskBatches()
{
_tasks = new List<Task>();
_batchSize = 2;
}
public void Add(Task task)
{
if (task == null)
return;
_tasks.Add(task);
}
public void Add(List<Task> task)
{
if (!task.Any())
return;
_tasks.AddRange(task);
}
public void Add(params List<Task>[] toAdd)
{
if (!toAdd.Any())
return;
foreach (var element in toAdd.SelectMany(items => items))
Add(element);
}
public async Task ExecuteTasks()
{
foreach (var batch in _tasks.Where(x => !x.IsCompleted || !x.IsCanceled).Chunk(_batchSize))
await Task.WhenAll(batch);
_tasks.RemoveAll(x => x.IsCompleted);
}
public void SetBatchSize(int batchSize)
{
_batchSize = batchSize;
}
}
public async Task ExecuteTasks()
{
foreach (var task in _tasks)
{
await task;
}
}

How can we make app in kiosk mode using xamarin?

I'm creating new app using xamarin. I have already completed some part using some sample codes. I'm able to disable back buttons, volume buttons and power button.
But when trying to disable home button I'm getting error on debugging.
I'm following this link,Kiosk mode in Andriod.
But when trying to disable home button I'm getting error on debugging.
Since you didn't post your code and your error message, we don't know what happened, I just tried to create such a sample followed the blog your posted and it works fine by my side.
Here is the service:
namespace KioskModeAndroid
{
[Service]
[IntentFilter(new[] { "KioskModeAndroid.KioskService" })]
public class KioskService : Service
{
private static long INTERVAL = Java.Util.Concurrent.TimeUnit.Seconds.ToMillis(2);
private static string TAG = typeof(KioskService).Name;
private static string PREF_KIOSK_MODE = "pref_kiosk_mode";
private Thread t = null;
private Context ctx = null;
private bool running = false;
public override void OnDestroy()
{
Log.Info(TAG, "Stopping service 'KioskService'");
running = false;
base.OnDestroy();
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
Log.Info(TAG, "Starting service 'KioskService'");
running = true;
ctx = this;
t = new Thread(() =>
{
while (running)
{
handleKioskMode();
Thread.Sleep(INTERVAL);
}
StopSelf();
});
t.Start();
return StartCommandResult.NotSticky;
}
private void handleKioskMode()
{
if (isKioskModeActive(ctx))
{
}
if (isInBackground())
{
restoreApp();
}
}
private bool isKioskModeActive(Context context)
{
var sp = PreferenceManager.GetDefaultSharedPreferences(context);
return sp.GetBoolean(PREF_KIOSK_MODE, false);
}
private bool isInBackground()
{
var am = ctx.GetSystemService(Context.ActivityService) as ActivityManager;
var processes = am.RunningAppProcesses;
foreach (var process in processes)
{
if (process.Importance == ActivityManager.RunningAppProcessInfo.ImportanceForeground)
{
foreach (var activeprocess in process.PkgList)
{
if (activeprocess == ctx.PackageName)
return false;
}
}
}
return true;
}
private void restoreApp()
{
Intent i = new Intent(ctx, typeof(MainActivity));
i.AddFlags(ActivityFlags.NewTask);
ctx.StartActivity(i);
}
public override IBinder OnBind(Intent intent)
{
return null;
}
}
}
I started this service in the OnCreate of MainActivity:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
StartService(new Intent(this, typeof(KioskService)));
}

TcpSocketClient- UnhandledException when I try read a response inside of a Task that not arrived yet

I'm using this library(https://github.com/rdavisau/sockets-for-pcl) to communicate with a TCP Server, that sends me when a event was generated, then, I have to verify all the time if the TCP Server sent to me a event, but if I try read anything before the TCP Server sends me, it's thrown the UnhandledException, but it only happens if I read inside a Task, in the main thread it thrown a timeout exception, the exception that I expected to happen in Task.
Someone can help me? Thanks. below is my code.
public class CentralTcpService
{
#region ConnectTcpAsync
public async void ConnectTcpAsync()
{
try
{
_sockecClient = new TcpSocketClient();
await _sockecClient.ConnectAsync(Central.Ip, Central.Port);
_writter = new ExtendedBinaryWriter(_sockecClient.WriteStream);
_reader = new ExtendedBinaryReader(_sockecClient.ReadStream);
_writter.WriteString(EvenNotProtocol.MobileReceiverCommand);
_sockecClient.ReadStream.ReadTimeout = int.MaxValue;
EnableTcpService();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
#endregion
#region TcpService
private void EnableTcpService()
{
_cancelationTcpService = new CancellationTokenSource();
new Task(StartService, _cancelationTcpService.Token, TaskCreationOptions.LongRunning).Start();
}
private void StartService()
{
while (!_cancelationTcpService.Token.IsCancellationRequested)
{
var ev = EvenNotProtocol.DeserializeEvent(_reader);
if (ev == null) continue;
_writter.WriteString(EvenNotProtocol.MobileOkCommand);
EventReceived?.Invoke(this, new CentralTcpEventArgs(ev));
}
}
}
public class EvenNotProtocol
{
public static Event DeserializeEvent(ExtendedBinaryReader reader)
{
try
{
reader.SkipBytes(1);
.....
}
catch (IOException e)
{
return null;
}
}
}

How to cancel a Timer before it's finished

I am working on a Chat app. After the messages of a chat are loaded and the messages were visible for 5 seconds, I want to send a read confirmation to the server. This is what I've come up with so far:
public async void RefreshLocalData()
{
// some async code to load the messages
if (_selectedChat.countNewMessages > 0)
{
Device.StartTimer(TimeSpan.FromSeconds(5), SendReadConfirmation);
}
}
When RefreshLocalData() is called, I know that either another chat was selected by the user or new messages came in for the current chat. So when RefreshLocalData() is called, I have to cancel the current timer to start a new one.
Another situation where I have to cancel the timer is when I navigate to another Page. This is no problem, because the whole ViewModel is disposed when this happens.
With the code above, if RefreshLocalData() is called again but the stated TimeSpan of 5 seconds is not over yet, the method is still executing.
Is there a way to cancel the timer (if RefreshLocalData() is called again)?
I have found this answer in the Xamarin forum: https://forums.xamarin.com/discussion/comment/149877/#Comment_149877
I have changed it a little bit to meet my needs and this solution is working:
public class StoppableTimer
{
private readonly TimeSpan timespan;
private readonly Action callback;
private CancellationTokenSource cancellation;
public StoppableTimer(TimeSpan timespan, Action callback)
{
this.timespan = timespan;
this.callback = callback;
this.cancellation = new CancellationTokenSource();
}
public void Start()
{
CancellationTokenSource cts = this.cancellation; // safe copy
Device.StartTimer(this.timespan,
() => {
if (cts.IsCancellationRequested) return false;
this.callback.Invoke();
return false; // or true for periodic behavior
});
}
public void Stop()
{
Interlocked.Exchange(ref this.cancellation, new CancellationTokenSource()).Cancel();
}
public void Dispose()
{
}
}
And this is how I use it in the RefreshLocalData() method:
private StoppableTimer stoppableTimer;
public async void RefreshLocalData()
{
if (stoppableTimer != null)
{
stoppableTimer.Stop();
}
// some async code to load the messages
if (_selectedChat.countNewMessages > 0)
{
if (stoppableTimer == null)
{
stoppableTimer = new StoppableTimer(TimeSpan.FromSeconds(5), SendReadConfirmation);
stoppableTimer.Start();
}
else
{
stoppableTimer.Start();
}
}
}
You can try using this class I found, it covers some of the limits to the DeviceTimer:
public class MySystemDeviceTimer
{
private readonly TimeSpan timespan;
private readonly Action callback;
private CancellationTokenSource cancellation;
public bool running { get; private set; }
public MySystemDeviceTimer(TimeSpan timespan, Action callback)
{
this.timespan = timespan;
this.callback = callback;
this.cancellation = new CancellationTokenSource();
}
public void Start()
{
running = true;
start(true);
}
private void start(bool continuous)
{
CancellationTokenSource cts = this.cancellation; // safe copy
Device.StartTimer(this.timespan,
() =>
{
if (cts.IsCancellationRequested)
{
running = false;
return false;
}
this.callback.Invoke();
return continuous;
});
}
public void FireOnce()
{
running = true;
start(false);
running = false;
}
public void Stop()
{
Interlocked.Exchange(ref this.cancellation, new CancellationTokenSource()).Cancel();
}
}
Then for your purpose:
MySystemDeviceTimer timer;
if (timer == null)
{
timer = new MySystemDeviceTimer(TimeSpan.FromSeconds(5), SendReadConfirmation);
timer.FireOnce();
}
else if (timer.running)
timer.Stop();
Yes you can with Device.StartTimer() as long as you return true to have the function repeat. I typically handle this through a Boolean variable that I can control in my ViewModel. Something like below:
bool shouldRun = true;
public async void RefreshLocalData()
{
// some async code to load the messages
if (_selectedChat.countNewMessages > 0)
{
Device.StartTimer(TimeSpan.FromSeconds(5), async() =>
{
await SendReadConfirmationAsync()
return shouldRun;
});
}
}
public async Task SendReadConfirmationAsync()
{
//Do some stuff
if(we want to stop call)
shouldRun = false;
}

android async task executing api's repeteadly

I am using Android Async Task function to execute an api using urlconnection. this api in turn sends emails to selected users.Now the issue is I am getting spammed by these emails at first I thought of it as an server side issue or my script but I created a new api and used it on IOS version of my application and everything works fine.But when I execute it on android I start getting spams,so I think the Issue lies in my android programming.
public class submitparse extends AsyncTask<String ,String,String> {
String Url;
#Override
protected String doInBackground(String... params) {
URL phonelink;
HttpURLConnection urlConnection = null;
try {
phonelink = new URL(params[0]);
urlConnection = (HttpURLConnection) phonelink
.openConnection();
urlConnection.connect();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isw);
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line = br.readLine()) != null) {
buffer.append(line);
}
String finalresult = buffer.toString();
return finalresult;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
urlConnection.disconnect();
} catch (Exception e) {
e.printStackTrace(); //If you want further info on failure...
}
}
return null;
}
I am using this command to call it..
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String link = "";
new submitparse().execute(link);
}
});
On getting result I start another activity,where link is a string containing url.
If you don't care, you can also use just a new Thread. That should fit your needs and works fine. As far as I read, you don't need to use an AsyncTask and therefore IMO a normal Thread would be better.
// Runnable uiThreadRunnable = new Runnable.....
Handler handler = new Handler(); // import android.os.Handler
Runnable runnable = new Runnable() {
public void run() {
// do your stuff
// use 'handler.post(uiThreadRunnable);' to if you NEED to run something on main thread
}
};
Thread thread = new Thread(runnable);
thread.start();

Resources