A While Loop Thread - c#

So I've been trying to create a bit of code that sends data on a while loop, specifically an alive packet to a server through a UdpClient.
static void doSend(string ip, int port)
{
while (isSending)
{
_sockMain = new UdpClient(ip, port);
// Code for datagram here, took it out
_sockMain.Send(arr_bData, arr_bData.Length);
}
}
But when I call the "Stop" method, it gets stuck in a constant loop and doesn't come out. How can I put the while loop into a Thread? So I can abort the thread on stop, cancelling the loop?

It hangs because your doSend method works on UI thread. You can use something like the below class to make it run on a seperate thread or you can use BackgroundWorkerClass
public class DataSender
{
public DataSender(string ip, int port)
{
IP = ip;
Port = port;
}
private string IP;
private int Port;
System.Threading.Thread sender;
private bool issending = false;
public void StartSending()
{
if (issending)
{
// it is already started sending. throw an exception or do something.
}
issending = true;
sender = new System.Threading.Thread(SendData);
sender.IsBackground = true;
sender.Start();
}
public void StopSending()
{
issending = false;
if (sender.Join(200) == false)
{
sender.Abort();
}
sender = null;
}
private void SendData()
{
System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP, Port);
while (issending)
{
// Define and assign arr_bData somewhere in class
_sockMain.Send(arr_bData, arr_bData.Length);
}
}
}

You can use the backgroundworker thread http://www.dotnetperls.com/backgroundworker
and inside dowork() put your while loop.
You can stop the code by using CancelAsync() and set backgroundWorker1.WorkerSupportsCancellation == true
BackgroundWorker bw = new BackgroundWorker();
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
// Run your while loop here and return result.
result = // your time consuming function (while loop)
}
// when you click on some cancel button
bw.CancelAsync();

static bool _isSending;
static void doSend(string ip, int port)
{
_isSending = true;
while (_isSending)
{
_sockMain = new UdpClient(ip, port);
// ...
_sockMain.Send(arr_bData, arr_bData.Length);
}
}
static void Stop()
{
// set flag for exiting loop here
_isSending = false;
}
Also consider to name your methods in PascalCase, i.e. DoSend (even StartSending will be better), StopSending.

How about using BREAK statement?

Related

How to stop my Thread in a function from a other function?

I need to stop a Thread when my timer is done.
But this all from a other function.
My Timer starts after Pressing Key: L. a Messagebox appears "Timer started" and my Thread starts too.
after 10 seconds, Timer stops with message but my Thread is still running.
What can i do? :/
void StartFunction()
{
Thread AB = new Thread(SEARCHING) { IsBackground = true };
AB.Start();
}
void StopFunction()
{
Thread AB = new Thread(SEARCHING);
AB.Abort();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L)
{
StartFunction();
timer1.Start();
MessageBox.Show("Timer 1 started!");
}
}
int time = 0;
private void timer1_Tick(object sender, EventArgs e)
{
time++;
if (time == 10 && timer1.Enabled)
{
StopFunction();
MessageBox.Show("Timer 1 stoped!");
timer1.Stop();
time = 0;
}
}
Idle_Mind is correct on how to accomplish this. Below is a working example using .NET 6.
One important detail is to use Thread.Join(). This will tell your caller to block until the loop is exited and the method returns.
Here I use the command console to key off the switching of the _running flag. You can do the same with a timer or whatever else. Keep in mind that you should probably also implement IDisposable in your class with the thread in it and set _running to false and do the join there as well. That way, you can instantiate the object with using.
namespace Lala
{
class AB : IDisposable
{
private bool _running = false;
private readonly Thread _thread;
public AB() => _thread = new Thread(Method);
private void Method()
{
while (_running)
{
Console.WriteLine("doing stuff");
Thread.Sleep(1000);
}
}
public void StartMethod()
{
_running = true;
_thread.Start();
}
public void StopMethod()
{
_running = false;
_thread.Join();
}
public void Dispose() => StopMethod();
}
public class Program
{
public static void Main()
{
Console.WriteLine("Launching a Thread. Press any key to stop it");
using AB ab = new();
// AB ab = new(); // if using is not appropriate
ab.StartMethod();
while (!Console.KeyAvailable)
Thread.Sleep(10);
// ab.StopMethod();// if using is not appropriate
}
}
}
Using modern methods you would write something like
private async void Form1_KeyDown(object sender, KeyEventArgs e)
{
var cts = new CancellationTokenSource(10000);
var task = Task.Run(() => Search(cts.Token));
try
{
var result = await task;
// handle result
}
catch (OperationCanceledException)
{
// handle cancelled
}
catch (Exception)
{
// handle other exceptions
}
}
public int Search(CancellationToken cancel)
{
while (true)
{
cancel.ThrowIfCancellationRequested();
// Do searching
if (found)
return result;
}
}
This would use thread pool threads instead of dedicated threads, and avoids the need to manually managing a timer. It also makes it easy to handle the result from the operation, if there are any.
Unfortunately, everything posted before didn't work for me or i just had not understand what i have to do.
Iam a C# Novice and I have a hard time understanding technical terms.
But i found a solution to make this possible.
This stops not the Thread but it Stops the while there has a function in a Thread.
First set a bool on top under public partial class:
public partial class Form1 : Form
{
private volatile bool m_StopThread;
then you have to give your while in the function this:
while (!m_StopThread)
this means that your while is still not running until this is set true.
After this is set, you give your Button or Timer a function maybe like this:
if ()
{
m_StopThread = true;
}
If this function is active your Thread will Start, because now its true and not longer false.
at the same way you can stop this again by set this function to false again.
If the solution I'm explaining has already been suggested, I thank you.
And hope it helps others.
But unfortunately I couldn't understand how to proceed now.
Thank you to those who go out of their way to help people like me every day. :)

Thread seems to stop running when I call a given method in the same class, why?

I have a class that constantly refreshes devices physically connected to PC via USB. The monitoring method runs on a thread checking a _monitoring flag, and Start and Stop methods just set and unset that flag.
My current problem is: when the thread is running, I get the expected "busy" and "not busy" console prints, but when I call Stop method, it keeps running while(_busy) forever, because somehow the _monitoringThread seems to stop running!
I suspect it stops running because the last print is always busy, that is, the ExecuteMonitoring runs midway and then nobody knows (at least I don't).
Pause debugging and looking at StackTrace didn't help either, because it keeps in the while(_busy) statement inside Stop() method, forever.
public class DeviceMonitor
{
bool _running;
bool _monitoring;
bool _busy = false;
MonitoringMode _monitoringMode;
Thread _monitoringThread;
readonly object _lockObj = new object();
// CONSTRUTOR
public DeviceMonitor()
{
_monitoringThread = new Thread(new ThreadStart(ExecuteMonitoring));
_monitoringThread.IsBackground = true;
_running = true;
_monitoringThread.Start();
}
public void Start()
{
_monitoring = true;
}
public void Stop()
{
_monitoring = false;
while (_busy)
{
Thread.Sleep(5);
}
}
void ExecuteMonitoring()
{
while (_running)
{
Console.WriteLine("ExecuteMonitoring()");
if (_monitoring)
{
lock (_lockObj)
{
_busy = true;
}
Console.WriteLine("busy");
if (_monitoringMode == MonitoringMode.SearchDevices)
{
SearchDevices();
}
else
if (_monitoringMode == MonitoringMode.MonitorDeviceConnection)
{
MonitorDeviceConnection();
}
lock (_lockObj)
{
_busy = false;
}
Console.WriteLine("not busy");
}
Thread.Sleep(1000);
_busy = false;
}
}
private void SearchDevices()
{
var connected = ListDevices();
if (connected.Count > 0)
{
Device = connected.First();
ToggleMonitoringMode();
}
else
Device = null;
}
void MonitorDeviceConnection()
{
if (Device == null)
{
ToggleMonitoringMode();
}
else
{
bool responding = Device.isConnected;
Console.WriteLine("responding " + responding);
if (!responding)
{
Device = null;
ToggleMonitoringMode();
}
}
}
void ToggleMonitoringMode()
{
if (_monitoringMode == MonitoringMode.SearchDevices)
_monitoringMode = MonitoringMode.MonitorDeviceConnection;
else
if (_monitoringMode == MonitoringMode.MonitorDeviceConnection)
_monitoringMode = MonitoringMode.SearchDevices;
}
enum MonitoringMode
{
SearchDevices,
MonitorDeviceConnection
}
}
The most likely explanation is: optimization: The compiler sees that _busy is never changed inside the Stop method and it is therefore allowed to convert this to an endless loop by replacing _busy with true. This is valid, because the _busy field is not marked as being volatile and as such the optimizer doesn't have to assume changes happening on another thread.
So, try marking _busy as volatile. Or, even better - actually A LOT BETTER - use a ManualResetEvent:
ManualResetEvent _stopMonitoring = new ManualResetEvent(false);
ManualResetEvent _monitoringStopped = new ManualResetEvent(false);
ManualResetEvent _stopRunning = new ManualResetEvent(false);
public void Stop()
{
_stopMonitoring.Set();
_monitoringStopped.Wait();
}
void ExecuteMonitoring()
{
while (!_stopRunning.Wait(0))
{
Console.WriteLine("ExecuteMonitoring()");
if(!_stopMonitoring.Wait(0))
{
_monitoringStopped.Unset();
// ...
}
_monitoringStopped.Set();
Thread.Sleep(1000);
}
}
Code is from memory, might contain some typos.

C# Lock Threading

I am currently trying to do some reading for locking for threads in C#
.
If I have a class similar to below
public class Connection
{
bool Connected;
internal bool startConnection(Device dev)
{
// start connection
Connected = true;
}
internal bool endConnection(Device dev)
{
// End connection
Connected = false;
}
private void readFromConnected(Device dev)
{
if(Connected)
{
// read values from connected device
}
}
}
The problem I have right now is that i have multiple threads using this class to read values from different devices
A problem arises when a thread tries to read values when it is actually disconnected, but attempts to read the values anyways because another thread has kept the Connected bool to true;
The thread that is calling this class looks like this.
Device deviceReceived;
public PollingInstance(Device deviceSent)
{
deviceReceived = deviceSent;
aTimer = new System.Timers.Timer(2500); //1000 = 1 sec
aTimer.Elapsed += OnTimedEvent;
aTimer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
for (int i = 0; i < 10; i++)
{
Connection.startConnection(deviceReceived);
Connection.readFromConnected(deviceReceived);
Connection.endConnection(deviceReceived);
}
}
The part of the main class calling Polling Instance looks similar to this
foreach(Device dev in listOfDev)
{
Task.Factory.StartNew(() => pollThread(dev));
}
private void pollThread(Device dev)
{
PollingInstance testingPoll = new PollingInstance(dev);
}
Simple. Why is connected a bool?
Try this.
public class Connection
{
private int _connections = 0;
internal bool startConnection(Device dev)
{
// start connection
if(Interlocked.Increment(ref _connections) == 1)
{
//do work to connect.
}
}
internal bool endConnection(Device dev)
{
// End connection
if(Interlocked.Decrement(ref _connections) == 0)
{
//do work to disconnect.
}
}
private void readFromConnected(Device dev)
{
if(_connections > 0)
{
// read values from connected device
}
}
}
This will "works" for some values of work. But is prone to connections being left open due to exceptions and sloppy/forgetful programming. Therefore I would advise the following.
Device device = ...
using(var connection = device.CreateConnection())
{
var results = connection.Read();
}
public abstract class Connection : IDisposable
{
public object Read();
}
public class Device
{
private class DeviceConnection : Connection
{
private Device Parent { get; set; }
void Dispose()
{
Parent.StopConnection();
}
public object Read()
{
return Device.readFromConnected();
}
}
private int _connections = 0;
public Connection CreateConnection()
{
// start connection
if(Interlocked.Increment(ref _connections) == 1)
{
//do work to connect.
}
return new DeviceConnection { Parent = this };
}
private bool StopConnection()
{
// End connection
if(Interlocked.Decrement(ref _connections) == 0)
{
//do work to disconnect.
}
}
private object readFromConnected()
{
//Device is guaranteed to be connected now!
}
}
It's hard to say exactly what will happen, because the code you posted won't even compile.
you wan't something like this:
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Connection connection = whereverThisComesFrom();
if(!Monitor.TryEnter(connection)) return; // another timer is in progress
try
{
for (int i = 0; i < 10; i++)
{
connection.startConnection(deviceReceived);
connection.readFromConnected(deviceReceived);
connection.endConnection(deviceReceived);
}
}
finally
{
Monitor.Exit(connection);
}
}
You said:
A problem arises when a thread tries to read values when it is
actually disconnected, but attempts to read the values anyways because
another thread has kept the Connected bool to true;
Can you use a try/finally to ensure you set the boolean properly?
The lock keyword is equivalent to a Monitor try/finally.
object syncObject = new object();
Monitor.Enter(syncObject);
try {
// Code updating shared data
}
finally {
Monitor.Exit(syncObject);
}
object syncObject = new object();
lock (syncObject) {
// Code updating shared data
}

Exit Thread and TcpListener in C#

I've the following problem: I created a simple HTTP server component. The server should be controlled with buttons on the GUI. I can start the server without any problems, but if I want to stop the server the whole program is killed. I think that's an error of aborting the thread but I don't know how I can solve this problem.
Here's my code:
public class HttpServer {
private int port;
public HttpServer(int port) {
this.port = port;
}
public void Listen() {
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
try {
while (true) {
TcpClient client = listener.AcceptTcpClient();
HttpProcessor processor = new HttpProcessor(client);
Thread thread = new Thread(new ThreadStart(processor.Process));
thread.Start();
Thread.Sleep(1);
}
}
catch { }
listener.Stop();
}
}
public class HttpProcessor {
private TcpClient client;
private StreamReader reader;
private StreamWriter writer;
public HttpProcessor(TcpClient client) {
this.client = client;
this.reader = null;
this.writer = null;
}
public void Process() {
reader = new StreamReader(client.GetStream());
writer = new StreamWriter(client.GetStream());
ParseRequest();
// some method calls to process the request and generate the response
SendResponse();
client.Close();
}
}
public partial class MainForm : Form {
private HttpServer server;
private Thread servthread;
private void Form_Load(object sender, EventArgs e) {
server = new HttpServer(8080);
}
private void Button1_Click(object sender, EventArgs e) {
servthread = new Thread(new ThreadStart(server.Listen));
servthread.Start();
Thread.Sleep(1);
}
private void Button2_Click(object sender, EventArgs e) {
servthread.Abort();
}
}
Do not use Thread.Abort(), ever! Use other means of communicating to the thread that it should stop, like a WaitHandle or even a private volatile bool stopThread; flag!
If you ever feel the need to call any other methods on a Thread than Start and Join you're probably doing something wrong and you should think about your design ;-)
See this: How to: Create and Terminate Threads (C# Programming Guide)
On your comment about AcceptTcpClient being a blocking call: Yes, it is. However, as others have noted too, you could easily change your class to avoid this problem:
public class HttpServer {
private int port;
private TcpListener listener; // Make the listener an instance member
public HttpServer(int port) {
this.port = port;
this.listener = new TcpListener(IPAddress.Any, port); // Instantiate here
}
public void Listen() {
listener.Start();
try {
while (true) {
TcpClient client = listener.AcceptTcpClient();
HttpProcessor processor = new HttpProcessor(client);
Thread thread = new Thread(new ThreadStart(processor.Process));
thread.Start();
Thread.Sleep(1);
}
}
catch { }
listener.Stop();
}
public void StopListening()
{
listener.Server.Close();
}
}
Then, instead of servthread.Abort(); you'd call server.StopListening();.
You may need to wrap the listener.Stop() line in a try/catch as well, but you'll have to try.
To make everything "kinda" work "kinda" correctly:
in HttpServer move listener variable from local var to class member
in HttpServer introduce a method:
public void Stop()
{
listener.Stop();
}
Change your Button2_Click method to:
private void Button2_Click(object sender, EventArgs e)
{
server.Stop();
servthread.Join();
}
PS: I assume that this is one of your first projects, so instead of writing a long post of how to do your stuff correctly, I suggested the changes that will allow you to continue your project. Bugs and architecture issues may come or may not come)
Happy learning.

How to Wake up a sleeping thread?

I made a thread at load event like below:
Thread checkAlert = null;
bool isStop = false;
private void frmMain_Load(object sender, EventArgs e)
{
checkAlert = new Thread(CheckAlert);
checkAlert.Start();
}
void CheckAlert()
{
while (!isStop)
{
Thread.Sleep(60000);
//do work here
}
}
Is there any way to resume the checkAlert thread during it's sleep period?( Thread.Sleep(60000);)
I tried using Thread.Interrupt() but it flows a ThreadInterruptedException, how should I handle this exception? or is there any way to resume the thread?
Edited:
I need to wake up the thread before the "sleep" end because when the user wants to quit the program, the program will have to wait for some time before it really quits ( checkAlert is still running) Is there any way to improve this case?
Based on your comments what it looks like is you need to re-design how CheckAlert works so it does not use Sleep's at all. What you should be doing is using a Timer instead.
System.Timers.Timer timer = null;
public FrmMain()
{
InitializeComponent();
timer = new System.Timers.Timer(60000);
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
//If you want OnTimedEvent to happen on the UI thread instead of a ThreadPool thread, uncomment the following line.
//timer.SynchronizingObject = this;
if(this.components == null)
this.components = new System.ComponentModel.Container();
//This makes it so when the form is disposed the timer will be disposed with it.
this.componets.Add(timer);
}
private void frmMain_Load(object sender, EventArgs e)
{
timer.Start();
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
//It is good practice not to do complicated logic in a event handler
// if we move the logic to its own method it is much easier to test (you are writing unit tests, right? ;) )
CheckAlert();
}
void CheckAlert()
{
//do work here
}
private void frmMain_Close(object sender, EventArgs e)
{
timer.Stop();
}
If you want the thread to exit automatically when your program quits, simply make it a background thread.
checkAlert = new Thread(CheckAlert);
checkAlert.IsBackground = true;
checkAlert.Start();
It looks to me like you're trying to create a thread which handles two types of events: do something and stop running.
Rather than using a shared variable (isStop) and some other technique to interrupt the thread in order to do work, you might want to use threading events (not to be confused high-level UI Event objects) to control your thread.
AutoResetEvent stop = new AutoResetEvent(false);
AutoResetEvent check = new AutoResetEvent(false);
private void CheckAlert() {
WaitHandle[] handles = new WaitHandle[] { stop, check };
for (;;) {
switch (AutoResetEvent.WaitAny(handles)) {
case 0:
return;
case 1:
// do work
break;
}
}
}
Calling check.Set() in your code will trigger the "do work" branch in the thread and stop.Set() will cause the thread to terminate gracefully.
Once your code has called stop.Set() to terminate the thread, it can call the thread's Join() method to wait until the thread terminates.
EDIT
I misunderstood the question. I will leave the code above in case anyone finds it useful.
If all you want to do is have a thread that performs a task once a minute and stop on demand, you can use the following code:
AutoResetEvent stop = new AutoResetEvent(false);
void CheckAlert() {
var time = new TimeSpan(0, 1, 0); // one minute
while (!stop.WaitOne(time)) {
// do work
}
}
private Thread checkThread;
private void frmMain_Load(object sender, EventArgs e) {
checkThread = new Thread(CheckAlert);
checkThread.Start();
}
private void frmMain_Close(object sender, EventArgs e) {
stop.Set(); // signal thread to stop
checkThread.Join(); // wait for thread to terminate
}
You can see an explanation on how to wake a sleeping thread here:
https://msdn.microsoft.com/en-us/library/tttdef8x%28v=vs.100%29.aspx
and this is a complete example (as you can see, Thread.Interrupt is the good choise... however you have to catch it to continue normal thread execution):
public class HVCSensor : HVCDevice, IDisposable
{
private Thread myThread;
private const int execute_timeout = ((10 + 10 + 6 + 3 + 15 + 15 + 1 + 1 + 15 + 10) * 1000);
private bool disposed = false;
private bool paused = false;
public delegate void HVCResultsHandler(HVC_RESULT res);
public event HVCResultsHandler HVCResultsArrived;
private void OnHVCResultsArrived(HVC_RESULT res)
{
if (HVCResultsArrived != null) {
HVCResultsArrived(res);
}
}
public HVCSensor() {
myThread = new Thread(new ThreadStart(this.execute));
}
private void execute(){
while (!disposed) {
if (!paused && this.IsConnected)
{
HVC_RESULT outRes;
byte status;
try
{
this.ExecuteEx(execute_timeout, activeDetections, imageAcquire, out outRes, out status);
OnHVCResultsArrived(outRes);
}
catch (Exception ex) {
}
}
else {
try
{
Thread.Sleep(1000);
}
catch (ThreadInterruptedException e)
{
}
}
}
}
public HVC_EXECUTION_IMAGE imageAcquire
{
get;
set;
}
public HVC_EXECUTION_FLAG activeDetections
{
get;
set;
}
public void startDetection() {
if(myThread.ThreadState==ThreadState.Unstarted)
myThread.Start();
}
public void pauseDetection() {
paused = true;
}
public void resumeDetection() {
paused = false;
if (myThread.ThreadState == ThreadState.WaitSleepJoin)
myThread.Interrupt();
}
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
disposed = true;
myThread.Interrupt();
}
}

Categories