Why does the callback/endinvoke happen after all begin invokes called? - c#

What I'm trying to do is queue up a big list of similar tasks. The time it takes each task to complete is small but there are a lot of them. For each item in the list, I create a delegate and call the delegate.BeginInvoke method then I move to creating the next one.
I would expect in the below example that I would get at least one "****** End Invoke ****" to occur BEFORE all of the begin invokes are called. Instead, it looks like the compiler is starting all of my begin invokes before any end invokes are received.
Is this normal behavior? Is there another way I can send off a task/method on another thread and receive it back while continuing to spur up more tasks?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace AsyncResultTesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting");
for (int i = 0; i < 100; i++)
{
delMeth d = new delMeth(sleepMethod);
Console.WriteLine(string.Format("Calling the begin invoke from thread: {0} for ID: {1}", Thread.CurrentThread.ManagedThreadId.ToString(), i.ToString()));
IAsyncResult ar = d.BeginInvoke(i, callbackMessage, d);
}
Console.ReadLine();
}
private delegate int delMeth(int id);
private static int sleepMethod(int id)
{
Console.WriteLine(Environment.NewLine + String.Format("Thread: {0} is sleeping. Delegate id is {1}", Thread.CurrentThread.ManagedThreadId.ToString(),id.ToString()));
Console.WriteLine(String.Format("Thread Properties IsThreadPoolThread? = {0} isThreadBackground? = {1} ThreadState: = {2}", Thread.CurrentThread.IsThreadPoolThread.ToString(), Thread.CurrentThread.IsBackground.ToString(), Thread.CurrentThread.ThreadState.ToString()));
Console.WriteLine("");
Thread.Sleep(100);
return id;
}
private static void callbackMessage(IAsyncResult ar)
{
delMeth d = (delMeth)ar.AsyncState;
int result = d.EndInvoke(ar);
Console.WriteLine(Environment.NewLine + "************************ END INVOKE *****************************");
Console.WriteLine(String.Format("Delegate was just called back for id: {0}", result.ToString()));
}
}
}
Unfortunately I have to design this using .NET 3.5 so Task Asynchronous Processing is not available to me.

USR is correct. Everything is functioning how I would expect it to. If i sleep for 10 ms then I see EndInvokes come back in the middle of BeginInvokes.
Thanks USR.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace AsyncResultTesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting");
for (int i = 0; i < 150; i++) {
delMeth d = new delMeth(sleepMethod);
Console.WriteLine(string.Format("Calling the begin invoke from thread: {0} for ID: {1}", Thread.CurrentThread.ManagedThreadId.ToString(), i.ToString()));
IAsyncResult ar = d.BeginInvoke(i, new AsyncCallback(callbackMessage), d);
}
Console.ReadLine();
}
private delegate int delMeth(int id);
private static int sleepMethod(int id)
{
Console.WriteLine(Environment.NewLine + String.Format("Thread: {0} is sleeping. Delegate id is {1}", Thread.CurrentThread.ManagedThreadId.ToString(),id.ToString()));
Console.WriteLine(String.Format("Thread Properties IsThreadPoolThread? = {0} isThreadBackground? = {1} ThreadState: = {2}", Thread.CurrentThread.IsThreadPoolThread.ToString(), Thread.CurrentThread.IsBackground.ToString(), Thread.CurrentThread.ThreadState.ToString()));
Console.WriteLine("");
Thread.Sleep(10);
return id;
}
private static void callbackMessage(IAsyncResult ar)
{
delMeth d = (delMeth)ar.AsyncState;
int result = d.EndInvoke(ar);
Console.WriteLine(Environment.NewLine + "************************ END INVOKE *****************************");
Console.WriteLine(String.Format("Delegate was just called back for id: {0}", result.ToString()));
}
}
}

Related

How to print thread code in command prompt

what is this called "the thread 0x2ef0" and how can I display it in the command prompt? I tried executing the code and it was only display in the output box or the debugger code, is there a possible way to print this hex decimal thread code? to the command prompt? i was using an windows application with windows property but when i tried in windows application with console property the "the thread 0x2ef0" is not showing off so i want to display it in command prompt.
Probably one of these:
using System;
using System.Diagnostics;
using System.Threading;
namespace Demo
{
internal static class Program
{
private static void Main()
{
Console.WriteLine("0x{0:x4}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("0x{0:x4}", Thread.CurrentThread.GetHashCode());
Console.WriteLine("0x{0:x4}", AppDomain.GetCurrentThreadId());
Console.WriteLine("0x{0:x4}", Process.GetCurrentProcess().Threads[0].Id);
}
}
}
Try using the CurrentThread property in order to get the data you are looking for.
Task<Double> t = Task.Run( () => { ShowThreadInformation("Main Task(Task #" + Task.CurrentId.ToString() + ")");
for (int ctr = 1; ctr <= 20; ctr++)
tasks.Add(Task.Factory.StartNew(
() => { ShowThreadInformation("Task #" + Task.CurrentId.ToString());
long s = 0;
for (int n = 0; n <= 999999; n++) {
lock (rndLock) {
s += rnd.Next(1, 1000001);
}
}
return s/1000000.0;
} ));
Task.WaitAll(tasks.ToArray());
Double grandTotal = 0;
Console.WriteLine("Means of each task: ");
foreach (var child in tasks) {
Console.WriteLine(" {0}", child.Result);
grandTotal += child.Result;
}
Console.WriteLine();
return grandTotal / 20;
} );
Console.WriteLine("Mean of Means: {0}", t.Result);

How many threads in the application? How to set the count of semaphore?

The semaphore means to specify the count how many threads can enter into critical region at a time.
The following code set the count is 5.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static Semaphore semaphore = new Semaphore(5, 5);
static void Main(string[] args)
{
Task.Factory.StartNew(() =>
{
for (int i = 1; i <= 15; ++i)
{
PrintSomething(i);
if (i % 5 == 0)
{
Thread.Sleep(2000);
}
}
});
Console.ReadLine();
}
public static void PrintSomething(int number)
{
semaphore.WaitOne();
Console.WriteLine(number);
semaphore.Release();
}
}
}
Which means each time we can have 5 threads running at any time. The printing result verifies the point.
My question is that I don't see so many threads running. Say 15 threads running, each time only can let 5 threads to be coexisting. There are only two threads.
Am I misunderstood something?
You start one additional thread using Task.Factory.StartNew() and that's it. In that thread you are calling the PrintSomething() method sequentially, so your thread output screenshot is correct.
Change your code as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SempahoreTest1
{
class Program
{
static Semaphore semaphore = new Semaphore(5, 5);
static void Main(string[] args)
{
for (int i = 1; i <= 15; ++i)
{
Task.Factory.StartNew((state) =>
{
int number = (int)state;
PrintSomething(number);
if (i % 5 == 0)
{
Thread.Sleep(2000);
}
}, i);
}
Console.ReadLine();
}
public static void PrintSomething(int number)
{
semaphore.WaitOne();
try
{
Console.WriteLine("Thread: {0}, Number: {1}", Thread.CurrentThread.ManagedThreadId, number);
}
finally
{
semaphore.Release();
}
}
}
}
Update:
A better explanation, how a semaphore works, would be:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SempahoreTest1
{
class Program
{
static Semaphore semaphore = new Semaphore(5, 5);
static Random random = new Random();
static void Main(string[] args)
{
Parallel.For(1, 16, PrintSomething);
Console.ReadLine();
}
public static void PrintSomething(int number)
{
semaphore.WaitOne();
try
{
Console.WriteLine("Thread: {0}, Number: {1}, Access granted", Thread.CurrentThread.ManagedThreadId, number);
// sleep to simulate long running method
Thread.Sleep(random.Next(1000, 5000));
}
finally
{
semaphore.Release();
Console.WriteLine("Thread: {0}, Number: {1}, Semaphore released", Thread.CurrentThread.ManagedThreadId, number);
}
}
}
}
The parallel loop calls PrintSomething() from different threads. On every call the semaphore counter is decreased, until it reaches 0. The next call of PrintSomething() blocks on semaphore.WaitOne(), until the first calls semaphore.Release(), that increments the semaphore counter again. And so on...
The first 5 calls are very fast, because the maximum semaphore count (5) is available. Then, due to the different sleep times, the method call outputs show you, how the semaphore works.

C# - Close console application when calculations take more time

I wrote app that is doing repeatedly some task every minute. Problem is that sometimes ( inner calculations ( because CPU was busy or because of any reasons) take more than 50 seconds, then I have to close app. "App monitor that is independent application will turn it on again" so that is not a problem. I implemented watchdog which can tell me if time is finished and then shut down my app.
And my question is if I'm doing it correctly. I test this solution for some time and it looks like working ok.
Main program
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AppTimer
{
class Program
{
public static appScheduler ap = new appScheduler();
static void Main(string[] args)
{
Console.WriteLine("Press ESC to stop");
while (ap.getWatchDogSeconds() < 55)
{
if (Console.KeyAvailable)
{
if (Console.ReadKey(true).Key == ConsoleKey.Escape)
{
break;
}
}
Thread.Sleep(100);
}
Console.WriteLine("Watchdog Fired ....sw finished !!");
Thread.Sleep(2000);
}
/// <summary>
/// Emulation of long calculation process
/// </summary>
public static void doJob()
{
Console.Clear();
Thread.Sleep(5000);
Console.WriteLine("Busy...");
Console.WriteLine("WatchDog Seconds : " + ap.getWatchDogSeconds());
ap.setWatchDogSeconds(40);
Console.WriteLine("Waiting longer than normal...");
Console.WriteLine("WatchDog Seconds : " + ap.getWatchDogSeconds());
Thread.Sleep(5000);
Console.WriteLine("Maybe sth is down !! or too busy");
Console.WriteLine("WatchDog Seconds : " + ap.getWatchDogSeconds());
while (true)
{
Thread.Sleep(200);
}
}
}
}
Below appScheduler class :
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AppTimer
{
public class appScheduler
{
static readonly object _lockerWD = new object();
static int minutes = 0;
static int seconds = 50;
static int days = 0;
static int hours = 0;
private static Task calculationsTask;
private int watchDogSeconds = 0;
private int maxSecondsForWatchdog = 50;
public appScheduler()
{
startTimer();
startWatchdogTimer();
}
private void ExecuteSeconds()
{
if (seconds == 0)
{
executeRepeatdTask();
}
calculateTime();
showTimerScreen();
}
private void executeRepeatdTask()
{
calculationsTask = Task.Factory.StartNew(() =>
Program.doJob());
}
public void startTimer()
{
var timer = new System.Threading.Timer((e) =>
{
ExecuteSeconds();
}, null, 0, 1000);
}
public void startWatchdogTimer()
{
var timer = new System.Threading.Timer((e) =>
{
ExecuteSecondsWatchDog();
}, null, 0, 1000);
}
private void ExecuteSecondsWatchDog()
{
Trace.WriteLine("Current amount of WD Seconds : " + watchDogSeconds);
increaseWatchDogSeconds();
}
private void showTimerScreen()
{
if ((calculationsTask == null) || (calculationsTask.IsCompleted == true))
{
//making watchdog zero if time is running and we are showing it!!
Console.Clear();
Console.WriteLine("This is software v1.2");
Console.WriteLine(String.Format("Current execution time : {0} days : {1} hours : {2} : minutes : {3} seconds",
days, hours, minutes, seconds));
Console.WriteLine("WatchDog Seconds : " + watchDogSeconds);
setWatchDogSeconds(0);
}
}
private void calculateTime()
{
seconds++;
if (seconds > 59)
{
seconds = 0;
minutes++;
if (minutes > 59)
{
minutes = 0;
hours++;
if (hours > 23)
{
hours = 0;
days++;
}
}
}
}
public int getMinutes()
{
return minutes;
}
public int getWatchDogSeconds()
{
return watchDogSeconds;
}
public void setWatchDogSeconds(int seconds)
{
Monitor.Enter(_lockerWD);
watchDogSeconds = seconds;
Monitor.Exit(_lockerWD);
}
public void increaseWatchDogSeconds()
{
var seconds = getWatchDogSeconds();
setWatchDogSeconds(seconds += 1);
if (seconds > maxSecondsForWatchdog)
{
Trace.WriteLine(string.Format("More than {0} seconds!", maxSecondsForWatchdog)); ;
Environment.Exit(0);
}
}
}
}
I do a great deal of workflow type tasks every day. I have tried to kick off jobs in the past the way you have here. The only time I use Thread.Sleep is to simulate some long running process in a test environment. I would look into using SetTimer. Also using Thread.Sleep in a loop is not going to give you accurate results. Now, that being said, keeping this running is going to be tricky.
Ultimately kicking off jobs on a regular basis is a breeze if you have an SQL server and can set the call up in an SSIS package. Especially if you are are trying to do this in a web application which might just stop running after a period of time. Good Luck

the use of the lock keyword over different threads

I am thinking that when I start my first thread, it should print "one + n" and lock l,then after this, it should start the second thread and print "two + n".
What actually happens, is that when I run the program I get random results, sometimes printing "one + n", other times printing "two + n"
My understanding of this is obviously flawed - why?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class locked
{
public long numtochange { get; set; }
public string threadname { get; set; }
}
class Program
{
public static locked l;
static void Main(string[] args)
{
l = new locked();
(new Thread(x => { l.threadname = "one"; Print(l); })).Start();
(new Thread(x => { l.threadname = "two"; Print(l); })).Start();
Console.ReadLine();
}
public static void Print(locked l)
{
lock (l)
{
for (long i = 0; i < 1000; i++)
{
l.numtochange = i;
Console.WriteLine(l.threadname + " " + l.numtochange);
}
}
}
}
}
This part of your code:
l.threadname = "one";
and the corresponding one with = "two" are not locked. Hence they can interleave randomly - sometimes the string "one" ends up in l.threadname and sometimes it is being overwritten by "two". Then, the first thread that manages to get to the lock statement in the Print function does its job and the other one waits.
The simplest fix, if you want them to run sequentially, is to wrap both statements with the lock keyword, like this:
lock (l) { l.threadname = "one"; Print(l); }
(lock is reentrant so there will be no problem with another lock in Print).
However if they always run one after another then there is no point in using threads.

No benefits from computationally intensive C# Program?

Im trying to see a performance difference between the following two programs (was expecting). But I find no difference. Is this normal? Im running on a Windows Core 2 Duo M/C
Visual Studio 2010 Express Edition
Program 1 (averaged over 100 runs: 824.11 ms):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace MultiThreading
{
class Program
{
public static Stopwatch stopwatch;
static void Main(string[] args)
{
stopwatch = new Stopwatch();
stopwatch.Start();
//Thread t = new Thread(WriteY);
//t.Start();
for (int i = 0; i < 10000; i++)
{
Console.Write("x{0} ", i);
}
WriteY();
Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
Console.ReadLine();
}
static void WriteY()
{
for (int i = 0; i < 10000; i++)
{
Console.Write("y{0} ", i);
}
//Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
//Console.ReadLine();
}
Program 2(averaged over 100 runs: 828.11 ms):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace MultiThreading
{
class Program
{
public static Stopwatch stopwatch;
static void Main(string[] args)
{
stopwatch = new Stopwatch();
stopwatch.Start();
Thread t = new Thread(WriteY);
t.Start();
for (int i = 0; i < 10000; i++)
{
Console.Write("x{0} ", i);
}
//WriteY();
Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
Console.ReadLine();
}
static void WriteY()
{
for (int i = 0; i < 10000; i++)
{
Console.Write("y{0} ", i);
}
//Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
//Console.ReadLine();
}
}
}
My guess is that both are bound by the speed of Console.Write which presumably needs to lock the resource (the screen) so that only one thread accesses it at a time.
The problem is that your application is IO-bound because you use Console.WriteLine all the time. If you do something which isn't using IO you will see a boost.
And as mentioned in the other answer WriteLine does indeed synchronize: Calling Console.WriteLine from multiple threads

Categories