I have a probably simple question about the task factory. I have to following code:
In this task is a loop that is polling data from the RS232 and a counter that stops polling after 10 times. After this "doCollect" will be set to false.
And now comes the strange thing: The task runs repeatedly. The caller code is:
// class Main()
RS232DataAquisition _RS232DataAquisition = new RS232DataAquisition();
public override void Run()
{
System.Diagnostics.Stopwatch timeout = new System.Diagnostics.Stopwatch();
timeout.Start();
_RS232DataAquisition.Start();
while ((timeout.ElapsedMilliseconds <= (dataGatherTime_inSeconds * 1000)) && _RS232DataAquisition.DoCollect)
{
System.Threading.Thread.Sleep(100);
}
timeout.Stop();
_RS232DataAquisition.Stop();
}
Per my understanding the Run() function should start the thread and return into the while-loop waiting for the thread to finish. But it never does?!
Here's the code for ReadDataFromRS232:
// sealed class RS232DataAquisition
private bool doCollect = false;
public bool DoCollect
{
get { return doCollect; }
}
public void Start()
{
doCollect = true;
currentTask = System.Threading.Tasks.Task.Factory.StartNew(() =>
{
this.ReadDataFromRS232();
});
}
private void ReadDataFromRS232(int NumtoRead = 10)
{
var port = new System.IO.Ports.SerialPort(PortName);
int waitCount = 5;
var portExists = System.IO.Ports.SerialPort.GetPortNames().Any(x => x == PortName);
if (!portExists)
{
throw new ArgumentException("Port does not exist!");
}
while (port.IsOpen && waitCount-- > 0)
{
doCollect = false;
Wait();
}
doCollect = true;
if (!port.IsOpen)
{
port.Open();
port.NewLine = _NewLine;
port.ReadTimeout = 2000;
int number;
try { }
finally { }
port.Write("flashon\r");
while (doCollect && (_readCounter <= NumtoRead))
{
string s;
try
{
s = port.ReadLine();
}
catch
{
s = "-1";
}
int i;
if (int.TryParse(s, out i))
{
number = Convert.ToInt32(s, 10);
}
else
{
number = 0;
}
lock (thisLock) _data.Add(number);
_readCounter++;
}
port.Write("flashoff\r");
port.Close();
port.Dispose();
Wait(); Wait();
}
}
private void Wait()
{
System.Threading.Thread.Sleep(10);
System.Threading.Thread.SpinWait(1);
}
I don't get, why "ReadDataFromRS232" is beeing repeated until the timeout stops this task.
Thank you for any help :)
EDIT: Added some missing code.
As Dennis said the problem seemed to come from the missing volatile. It works now even though I have no idea why it didn't before.
Related
I have an abstract class which runs threads:
protected volatile bool HasError = false;
public void Run()
{
var readingThread = new Thread(ReadInFile);
var compressingThreads = new List<Thread>();
for (var i = 0; i < Environment.ProcessorCount; i++)
{
var j = i;
ProcessEvents[j] = new AutoResetEvent(false);
compressingThreads.Add(new Thread(() => Process(j)));
}
var writingThread = new Thread(WriteOutFile);
readingThread.Start();
foreach (var compressThread in compressingThreads)
{
compressThread.Start();
}
writingThread.Start();
WaitHandle.WaitAll(ProcessEvents);
OutputDictionary.SetCompleted();
writingThread.Join();
Console.WriteLine(!HasError ? "Successfully competed" : "Error");
}
Well, and I don't know how I can check the Exception?
This is class realizes abstract class.
This is one method :
protected override void Process(int processEventId)
{
try
{
while (InputQueue.Dequeue(out Chunk chunk) && !HasError)
{
var compressedChunk = GZip.GZip.CompressByBlocks(chunk.Bytes);
OutputDictionary.Add(chunk.Id, compressedChunk);
}
ProcessEvents[processEventId].Set();
}
catch (Exception e)
{
HasError = true;
}
}
As you can see, I change the value of a variable when I catch an exception, but will it work? I do not understand how to check.
The better answer is probably not to use Threads but use Parallel.For(), it manages your errors and also has better options to handle workload.
But in your current setup, just add a wrapper method:
var j = i;
ProcessEvents[j] = new AutoResetEvent(false);
compressingThreads.Add(new Thread(() => SafeCallProcess(j) ));
and
private void SafeCallProcess(int j)
{
try
{
Process (j);
}
catch(Exception e)
{
// deal with it
}
}
You could move the Set() to the wrapper too, up to your taste I guess.
My requirement is to insert item in a queue and process it but the items should be added first and after a while they should be processed (as some other things needs to be set before processing the items. Here is the coding I have done so far.
#region Variables Declarations
private Thread threadTask = null;
ConcurrentQueue<string> concurrentQueue = new ConcurrentQueue<string>();
string currentSeqNo;
string previousSeqNo = "-1";
#endregion
private void test1_Load(object sender, EventArgs e)
{
AddItems();
if (threadTask == null)
{
threadTask = new Thread(Kick);
Thread.Sleep(5000);
threadTask.Start();
}
}
private void AddItems()
{
for (Int64 i = 100000; i < 300000; i++)
{
concurrentQueue.Enqueue(i.ToString());
this.Invoke(new MethodInvoker(delegate()
{
label1.Text = i.ToString();
label1.Update();
}));
}
}
private void Kick()
{
while (true)
{
int recordCountNew = concurrentQueue.Count();
if (recordCountNew != 0)
{
RemoveItems();
}
}
}
private void RemoveItems()
{
string item;
while (concurrentQueue.TryDequeue(out item))
{
this.Invoke(new MethodInvoker(delegate()
{
label2.Text = item;
label2.Update();
}));
currentSeqNo = item; // second time does not start wil 100000
if (previousSeqNo != "-1")
{
if (long.Parse(currentSeqNo) != long.Parse(previousSeqNo) + 1)
{
Reconnect();
}
else
{
//Process item
previousSeqNo = currentSeqNo;
}
}
else
{
//Process item
previousSeqNo = currentSeqNo;
}
}
}
private void Reconnect()
{
currentSeqNo = "";
previousSeqNo = "-1";
string someItem;
while (concurrentQueue.Count > 0)
{
concurrentQueue.TryDequeue(out someItem);
}
this.Invoke(new MethodInvoker(delegate()
{
label1.Text = "";
label2.Text = "";
label1.Update();
label2.Update();
}));
AddItems();
if (threadTask == null)
{
threadTask = new Thread(Kick);
threadTask.Start();
}
}
private void button1_Click_1(object sender, EventArgs e)
{
Reconnect();
}
To reproduce the issue: Run the app and in the middle click on the button. Now the queue should again be started from 100000 but it shows the number somewhere greater than 100000.
Please advise how do I release all the resources to make a fresh start after clicking a button. Though I am setting them to default and also clearing the queue but it still shows the old values in currentSeqNo when 'RemoveItems' method is called.
What you see is a race condition between the Kick thread and the button click handler. When you press the button you execute Reconnect() in it you clean the queue and then call the AddItems() function. But all this time the Kick function tries to Dequeue and so you end up each time with an arbitrary amount of items in it. What you should do is to synchronize between these functions or prevent the Kick from executing while you are adding items.
Couple of comments:
1) You Kick() method have an infinite loop, that too without sleep. Every thread started will keep on running as you didn't have a scope for thread to come out.
You can have a member variable like bKeepRunning with default value as true. Set that variable to false in beginning of Reconnect() function. Something like:
private void Kick()
{
while (bKeepRunning)
{
int recordCountNew = concurrentQueue.Count();
if (recordCountNew != 0)
{
RemoveItems();
}
}
}
Why do you have Thread.Sleep(5000); in test1_Load()? I dont think that is needed.
I made small change in your code, something like:
private void AddItems()
{
for (Int64 i = 100000; i < 300000; i++)
{
concurrentQueue.Enqueue(i.ToString());
this.Invoke(new MethodInvoker(delegate()
{
label1.Text = i.ToString();
label1.Update();
}));
if (i < 100004)
Thread.Sleep(1000);
}
}
private void Kick()
{
while (bKeepRunning)
{
int recordCountNew = concurrentQueue.Count();
if (recordCountNew != 0)
{
RemoveItems();
}
}
}
private void Reconnect()
{
currentSeqNo = "";
previousSeqNo = "-1";
bKeepRunning = false;
threadTask = null;
string someItem;
while (concurrentQueue.Count > 0)
{
concurrentQueue.TryDequeue(out someItem);
}
this.Invoke(new MethodInvoker(delegate()
{
label1.Text = "";
label2.Text = "";
label1.Update();
label2.Update();
}));
Thread.Sleep(2000);
AddItems();
bKeepRunning = true;
if (threadTask == null)
{
threadTask = new Thread(Kick);
threadTask.Start();
}
}
It helped me to see that value is starting from 100000. You can try the same at your end.
Note: I have stopped thread and restarted after clicking on button. Hence i dont see any flaw in your code as such. It just runs fast so that you are not able to realize start values.
You should make UI thread and threadTask thread sync, just use ManualResetEventSlim Signal Construct to, like this:
static ManualResetEventSlim guard = new ManualResetEventSlim(true);
private void button1_Click_1(object sender, EventArgs e)
{
guard.Reset();
Reconnect();
guard.Set();
}
private void RemoveItems()
{
string item;
while (concurrentQueue.TryDequeue(out item))
{
guard.Wait();
//......
}
}
see:
ManualResetEventSlim Class
From code below, Sometimes, it will not reply all ping request, and will wait for infinite period of time.Is there any deadlock issue? if yes How to handle it? How can I get correct result for every ip without any deadlock?
class Program
{
static Semaphore PingSender { get; set; }
static object lockObj = new object();
static void Main(string[] args)
{
long pingSent = 0;
long pingReceived = 0;
PingSender = new Semaphore(10000, 10000);
for (int dotNoDot = 1; dotNoDot <= 255; dotNoDot++)
{
for (int dotNo = 1; dotNo <= 255; dotNo++)
{
try
{
string ipAddress = string.Format("111.222.{0}.{1}", dotNoDot, dotNo); //Please change necessary ip base
PingSender.WaitOne();
Ping ping = new Ping();
ping.PingCompleted += (sender, eventArgs) =>
{
//lock (lockObj)
//{
PingSender.Release(1);
Console.WriteLine(string.Format("{0}) Address: {1} and Status: {2}", pingReceived, eventArgs.UserState, eventArgs.Reply.Status));
if (eventArgs.Reply.Status == IPStatus.Success)
{
//collect online machines
}
Interlocked.Increment(ref pingReceived);
if (pingReceived == pingSent)
{
//done
}
//}
};
//lock (lockObj)
//{
pingSent++;
// }
ping.SendAsync(ipAddress, 10000, ipAddress);
}
catch (Exception ex)
{
}
}
}
}
}
Hi guys I have such construction:
Start threads:
Thread[] thr;
int good_auth, bad_auth, good_like, bad_like;
static object accslocker = new object();
static object limitlocker = new object();
string acc_path, proxy_path, posts_path;
int position_of_limit, position = 0;
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
decimal value = numericUpDown1.Value;
int i = 0;
int j = (int)(value);
thr = new Thread[j];
for (; i < j; i++)
{
thr[i] = new Thread(new ThreadStart(go));
thr[i].IsBackground = true;
thr[i].Start();
}
}
And than function go:
public void go()
{
while (true)
{
string acc = "";
string proxy = "";
lock (limitlocker)
{
if (position_of_limit >= int.Parse(textBox2.Text) - 1)
{
position_of_limit = 0;
if (position < posts.Count - 1)
position++;
else
{
break;
}
}
}
lock (accslocker)
{
if (accs.Count == 0)
{
break;
}
else
acc = accs.Dequeue();
}
OD od = new OD(acc);
string login = od.Auth();
if (login == "Login")
{
lock (accslocker)
{
good_auth++;
log_good_auth(good_auth);
}
string like = od.like(posts[position], textBox1.Text);
if (like == "Good")
{
lock (accslocker)
{
position_of_limit++;
good_like++;
log_good_like(good_like);
}
}
else if (like == "Failed")
{
lock (accslocker)
{
bad_like++;
log_bad_like(bad_like);
}
}
else
{
lock (accslocker)
{
bad_like++;
log_bad_like(bad_like);
}
}
}
else if (login == "Spamblock")
{
lock (accslocker)
{
bad_auth++;
log_bad_auth(bad_auth);
}
}
else if (login == "Locked")
{
lock (accslocker)
{
bad_auth++;
log_bad_auth(bad_auth);
}
}
else if (login == "Invalid")
{
lock (accslocker)
{
bad_auth++;
log_bad_auth(bad_auth);
}
}
else if (login == "Bad_proxy")
{
lock (accslocker)
{
accs.Enqueue(acc);
Proxy.proxies.Remove(proxy);
}
}
else
{
lock (accslocker)
{
accs.Enqueue(acc);
Proxy.proxies.Remove(proxy);
}
}
}
}
I start for example 20 threads, when position_of_limit becomes bigger than int.Parse(textBox2.Text) - 1 all threads need to take next posts[position] in next loop. But I receive an exception on line string like = od.like(posts[position], textBox1.Text);
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
How to solve this problem? Thanks
Locking the GUI thread is evil, since it's an STA thread.
That means it must manage a message loop and is not allowed to block. So blocking is likely to cause deadlocks.
Rather use callbacks like BackgroundWorker.RunWorkerCompleted.
Some other informative links about locking:
Guidelines of when to use locking
Obtaining lock on a UI thread
I've made this producer-consumer sample, but I don't know why it freezes at the end.
Where is the problem? If I put a breakpoint at line setNum(-99); and then after break continue it finishes OK.
Please also tell me if this code is ok and threadsafe. It must work like that, so meanwhile consumer is processing its given value, all other values from producer must be ignored.
I am very new to multithreading.
class Program
{
delegate void SetNumberDelegate(int number);
static void Main(string[] args)
{
Random rnd = new Random();
ConsumerClass consumerClass = new ConsumerClass();
SetNumberDelegate setNum = new SetNumberDelegate(consumerClass.setNumber);
Thread.Sleep(20);
int num;
int count = 0;
Console.WriteLine("Start");
while (count++ < 100)
{
num = rnd.Next(0, 100);
Console.WriteLine("Generated number {0}", num);
if (num > 30)
{
setNum(num);
}
}
setNum(-99);
Console.WriteLine("End");
Console.ReadKey();
}
}
class ConsumerClass : IDisposable
{
private int number;
private object locker = new object();
private EventWaitHandle _wh = new AutoResetEvent(false);
private Thread _consumerThread;
public ConsumerClass()
{
number = -1;
_consumerThread = new Thread(consumeNumbers);
_consumerThread.Start();
}
public void Dispose()
{
setNumber(-99);
_consumerThread.Join();
_wh.Close();
}
public void setNumber(int num)
{
if (Monitor.TryEnter(locker))
{
try
{
number = num;
Console.WriteLine("Setting number {0}", number);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(locker);
}
_wh.Set();
}
}
public void consumeNumbers()
{
while (true)
{
Monitor.Enter(locker);
if (number > -1)
{
try
{
Console.WriteLine("Processing number:{0}", number);
// simulate some work with number e.g. computing and storing to db
Thread.Sleep(20);
Console.WriteLine("Done");
number = -1;
}
finally
{
Monitor.Exit(locker);
}
}
else
{
if (number == -99)
{
Console.WriteLine("Consumer thread exit");
return;
}
Monitor.Exit(locker);
_wh.WaitOne(); // No more tasks - wait for a signal
}
}
}
}
Rewrite setNumber like this to see your problem:
public void setNumber(int num) {
if (Monitor.TryEnter(locker)) {
// etc..
}
else Console.WriteLine("Number {0} will never make it to the consumer", num);
}
You'll have to block, waiting for the consumer to be ready to consume or use a queue.
Monitor.TryEnter(locker); will usually fail (including for -99) so you aren't going to set a fair number of the values, which is why the output is lacking in setting statements. This is because it will not wait to acquire the lock it will just return false.
The problem appears to be in the last part of the code. You're holding the lock when you execute this:
else
{
if (number == -99)
{
Console.WriteLine("Consumer thread exit");
return;
}
Monitor.Exit(locker);
_wh.WaitOne(); // No more tasks - wait for a signal
}
So if number == 99, the method returns without releasing the lock.
Your ConsumeNumbers method is overly complex. You can simplify it:
while (true)
{
_wh.WaitOne();
lock (locker)
{
if (number == -99)
break;
if (number > -1)
{
// process the number.
number = -1;
}
}
}
That will do the same thing, and is much simpler code.
By the way, the construct:
lock (locker)
{
// do stuff here
}
is the same as:
Monitor.Enter(locker);
try
{
// do stuff here
}
finally
{
Monitor.Exit(locker);
}