To debug a firewall delay issue I need an application that will produce a beep on server side when it detects an HTTP GET request.
This code (test.ashx):
<%# WebHandler Language="C#" Class="TestHandler" %>
using System;
using System.Web;
public class TestHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = context.Response;
try
{
Response.Write("Before beep");
Console.Beep();
Response.Write("After beep");
}
catch (Exception ex)
{
Response.Write(ex.Message + "<br />\n" + ex.InnerException.Message);
}
}
public bool IsReusable { get { return false; } }
}
produces sound only when debugging in IIS Express. After moving the web app to IIS, the sound disappears.
The three easy ways of producing a sound are System.Console.Beep(), System.Media.SoundPlayer, and System.Media.SystemSounds.Beep().
Unfortunately, these methods only work in desktop applications, and won't work in service applications. When ASP.Net apps are run under IIS Express (a desktop app), these sound methods work. However, when ASP.Net apps are run under the IIS service, the sound methods don't work.
System.Console.Beep() ultimately calls the kernel32.dll Beep() function. It's restricted to desktop apps only (scroll down to the Requirements section).
Same for System.Media.SoundPlayer and System.Media.SystemSounds.Beep(). They call the kernel32.dll MessageBeep() and the winmm.dll PlaySound() functions, respectively. They, too, are restricted to desktop apps.
One way to get sounds to play in a service is use NAudio. It's easy to install via NuGet.
This chunk of code is the only way I could get the sound to play. It has to be played on a separate worker thread, and the execution of the worker thread needs to be paused to let the .wav file finish playing.
using System;
using System.Diagnostics;
using System.Threading;
using NAudio.Dsp;
using NAudio.Wave;
...
protected void Button1_Click(object sender, EventArgs e)
{
var waveFilename = #"c:\Windows\Media\tada.wav";
/* Trying to play the .wav file on the main thread
doesn't seem to work. */
ThreadPool.QueueUserWorkItem(
(state) =>
{
using (var audioPlayback = new AudioPlayback())
{
audioPlayback.Load(waveFilename);
audioPlayback.Play(); // Asynchronous.
/* Need to sleep for the approximate length of .wav file,
otherwise no sound is produced because of the
asynchronous Play() call. */
Thread.Sleep(2000);
}
});
}
Here's the supporting code taken from code in NAudio's NAudioWPFDemo project:
public class MaxSampleEventArgs : EventArgs
{
[DebuggerStepThrough]
public MaxSampleEventArgs(float minValue, float maxValue)
{
this.MaxSample = maxValue;
this.MinSample = minValue;
}
public float MaxSample { get; private set; }
public float MinSample { get; private set; }
}
public class FftEventArgs : EventArgs
{
[DebuggerStepThrough]
public FftEventArgs(Complex[] result)
{
this.Result = result;
}
public Complex[] Result { get; private set; }
}
public class SampleAggregator : ISampleProvider
{
// volume
public event EventHandler<MaxSampleEventArgs> MaximumCalculated;
private float maxValue;
private float minValue;
public int NotificationCount { get; set; }
int count;
// FFT
public event EventHandler<FftEventArgs> FftCalculated;
public bool PerformFFT { get; set; }
private readonly Complex[] fftBuffer;
private readonly FftEventArgs fftArgs;
private int fftPos;
private readonly int fftLength;
private int m;
private readonly ISampleProvider source;
private readonly int channels;
public SampleAggregator(ISampleProvider source, int fftLength = 1024)
{
channels = source.WaveFormat.Channels;
if (!IsPowerOfTwo(fftLength))
throw new ArgumentException("FFT Length must be a power of two");
this.m = (int) Math.Log(fftLength, 2.0);
this.fftLength = fftLength;
this.fftBuffer = new Complex[fftLength];
this.fftArgs = new FftEventArgs(fftBuffer);
this.source = source;
}
private bool IsPowerOfTwo(int x)
{
return (x & (x - 1)) == 0;
}
public void Reset()
{
count = 0;
maxValue = minValue = 0;
}
private void Add(float value)
{
if (PerformFFT && FftCalculated != null)
{
fftBuffer[fftPos].X = (float) (value * FastFourierTransform.HammingWindow(fftPos, fftLength));
fftBuffer[fftPos].Y = 0;
fftPos++;
if (fftPos >= fftBuffer.Length)
{
fftPos = 0;
// 1024 = 2^10
FastFourierTransform.FFT(true, m, fftBuffer);
FftCalculated(this, fftArgs);
}
}
maxValue = Math.Max(maxValue, value);
minValue = Math.Min(minValue, value);
count++;
if (count >= NotificationCount && NotificationCount > 0)
{
if (MaximumCalculated != null)
MaximumCalculated(this, new MaxSampleEventArgs(minValue, maxValue));
Reset();
}
}
public WaveFormat WaveFormat { get { return source.WaveFormat; } }
public int Read(float[] buffer, int offset, int count)
{
var samplesRead = source.Read(buffer, offset, count);
for (int n = 0; n < samplesRead; n += channels)
Add(buffer[n + offset]);
return samplesRead;
}
}
public class AudioPlayback : IDisposable
{
private IWavePlayer _playbackDevice;
private WaveStream _fileStream;
public void Load(string fileName)
{
Stop();
CloseFile();
EnsureDeviceCreated();
OpenFile(fileName);
}
private void CloseFile()
{
if (_fileStream != null)
{
_fileStream.Dispose();
_fileStream = null;
}
}
private void OpenFile(string fileName)
{
try
{
var inputStream = new AudioFileReader(fileName);
_fileStream = inputStream;
var aggregator = new SampleAggregator(inputStream);
aggregator.NotificationCount = inputStream.WaveFormat.SampleRate / 100;
aggregator.PerformFFT = true;
_playbackDevice.Init(aggregator);
}
catch
{
CloseFile();
throw;
}
}
private void EnsureDeviceCreated()
{
if (_playbackDevice == null)
CreateDevice();
}
private void CreateDevice()
{
_playbackDevice = new WaveOut { DesiredLatency = 200 };
}
public void Play()
{
if (_playbackDevice != null && _fileStream != null && _playbackDevice.PlaybackState != PlaybackState.Playing)
_playbackDevice.Play();
}
public void Pause()
{
if (_playbackDevice != null)
_playbackDevice.Pause();
}
public void Stop()
{
if (_playbackDevice != null)
_playbackDevice.Stop();
if (_fileStream != null)
_fileStream.Position = 0;
}
public void Dispose()
{
Stop();
CloseFile();
if (_playbackDevice != null)
_playbackDevice.Dispose();
}
}
Try this System.Media.SystemSounds.Beep.Play();
Related
In Unity3d Webgl I download and write big file (about 100 mb and more) to cache (indexeddb).
I downloaded file with UnityWebRequest and write file in downloadHandler.
Sometimes after call filestream.Write() or filestream.Flush() Chrome will crash.
Exception not available, only crash tab of chrome.
Crash happens abount in 50 percent of dowloads.
public class Downloader : MonoBehaviour
{
public IEnumerator DownloadFileCoroutine(string url, string filePath, string clientName, int fileId, Action<int> downloadedCallback)
{
var currentWebRequest = UnityWebRequest.Get(url);
currentWebRequest.downloadHandler = new ToFileDownloadHandler(new byte[64 * 1024], filePath);
var downloadHendler = (currentWebRequest.downloadHandler as ToFileDownloadHandler);
currentWebRequest.Send();
while (!currentWebRequest.isDone && !currentWebRequest.isNetworkError && !currentWebRequest.isHttpError)
{
if (currentWebRequest.isNetworkError)
{
yield break;
}
}
if (currentWebRequest.isNetworkError)
{
downloadHendler.Cancel();
}
else
{
/// suceess
}
}
public class ToFileDownloadHandler : DownloadHandlerScript
{
private int _expected = -1;
private long _received = 0;
private readonly string _filepath;
private readonly FileStream _fileStream = null;
private bool _canceled = false;
public ToFileDownloadHandler(byte[] buffer, string filepath) : base(buffer)
{
CreateDir();
_filepath = filepath;
_fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write);
}
protected override byte[] GetData() { return null; }
protected override bool ReceiveData(byte[] data, int dataLength)
{
if (data == null || data.Length < 1)
{
return false;
}
_received += dataLength;
if (!_canceled)
{
_fileStream.Write(data, 0, dataLength);
_fileStream.Flush();
}
return true;
}
protected override float GetProgress()
{
if (_expected < 0) return 0;
return (float)_received / _expected;
}
protected override void CompleteContent()
{
_fileStream.Close();
_isComplete = true;
}
protected override void ReceiveContentLength(int contentLength)
{
_expected = contentLength;
}
public void Cancel()
{
if (_canceled) return;
_canceled = true;
if (_fileStream != null)
{
_fileStream.Close();
}
File.Delete(_filepath);
}
private void CreateDir()
{
cachePath = Application.persistentDataPath;
if (!Directory.Exists(cachePath))
{
Directory.CreateDirectory(cachePath);
}
}
}
}
I am working on my own multithreading for my algorithm independed pathfinding for unity. However, when I am executing two the same class I get a memory leak and when only executing one instance I am having no issues. I really want to use at least two threads if it is necessary.
Below is the class I have issues with. Keep in mind, that two independend threads will have to execute parts of this script. AddJob can be called from the main unity thread but will most likely be called from another update thread for the agents.
namespace Plugins.PathFinding.Threading
{
internal class PathFindingThread
{
private Thread m_Worker;
private volatile Queue<CompletedProcessingCallback> m_CallbackQueue;
private volatile Queue<IAlgorithm> m_QueuedTasks;
internal int GetTaskCount
{
get
{
return m_QueuedTasks.Count;
}
}
internal PathFindingThread()
{
m_Worker = new Thread(Run);
m_CallbackQueue = new Queue<CompletedProcessingCallback>();
m_QueuedTasks = new Queue<IAlgorithm>();
}
private void Run()
{
Debug.Log("<b><color=green> [ThreadInfo]:</color></b> PathFinding Thread Started ");
try
{
while(true)
{
if (m_QueuedTasks.Count > 0)
{
IAlgorithm RunningTask = m_QueuedTasks.Dequeue();
RunningTask.FindPath(new IAlgorithmCompleted(AddCallback));
}
else
break;
}
Debug.Log("<b><color=red> [ThreadInfo]:</color></b> PathFinding Worker is idle and has been Stopped");
}
catch(Exception)
{
Debug.Log("<b><color=red> [ThreadInfo]:</color></b> PathFinding thread encountred an error and has been aborted");
}
}
internal void AddJob(IAlgorithm AlgorithmToRun)
{
m_QueuedTasks.Enqueue(AlgorithmToRun);
//Debug.Log("Added Job To Queue");
}
private void AddCallback(CompletedProcessingCallback callback)
{
m_CallbackQueue.Enqueue(callback);
}
private void Update()
{
if (m_CallbackQueue.Count > 0)
{
if (m_CallbackQueue.Peek().m_Callback != null) { }
m_CallbackQueue.Peek().m_Callback.Invoke(m_CallbackQueue.Peek().m_Path);
m_CallbackQueue.Dequeue();
}
if (m_Worker.ThreadState != ThreadState.Running && m_QueuedTasks.Count != 0)
{
m_Worker = new Thread(Run);
m_Worker.Start();
}
}
}
internal delegate void IAlgorithmCompleted(CompletedProcessingCallback callback);
internal struct CompletedProcessingCallback
{
internal volatile FindPathCompleteCallback m_Callback;
internal volatile List<GridNode> m_Path;
}
}
namespace Plugins.PathFinding
{
internal enum TypeOfNode
{
Ground,
Air
}
//used to store location information since array can only take rounded numbers
internal struct Position
{
internal int x;
internal int y;
internal int z;
}
internal class GridNode
{
internal Position M_PostitionInGrid { get; private set; }
internal Vector3 M_PostitionInWorld { get; private set; }
internal TypeOfNode M_type { get; private set; }
internal bool m_IsWalkable = true;
internal GridNode m_ParrentNode;
internal int Hcost;
internal int Gcost;
internal int Fcost { get { return Hcost + Gcost; } }
internal GridNode(Position postion , Vector3 WorldPosition)
{
M_PostitionInGrid = postion;
m_IsWalkable = true;
M_PostitionInWorld = WorldPosition;
}
}
}
internal delegate void FindPathCompleteCallback(List<GridNode> Path);
internal abstract class IAlgorithm
{
protected GridNode m_SavedStart;
protected GridNode m_SavedTarget;
protected List<GridNode> m_LocatedPath;
protected FindPathCompleteCallback m_Callback;
internal FindPathCompleteCallback GetCallback
{
get
{
return m_Callback;
}
}
protected PathFindingGrid m_grid;
internal abstract void FindPath(IAlgorithmCompleted callback);
protected abstract List<GridNode> CreatePath(PathFindingGrid Grid, GridNode Start, GridNode Target);
protected abstract List<GridNode> RetracePath(GridNode start, GridNode target);
}
namespace Plugins.PathFinding.Astar
{
internal class AstarFinder : IAlgorithm
{
//construction of the Algorithm
internal AstarFinder(GridNode start, GridNode target, FindPathCompleteCallback Callback)
{
m_SavedStart = start;
m_SavedTarget = target;
m_Callback = Callback;
m_LocatedPath = new List<GridNode>();
m_grid = PathFindingGrid.GetInstance;
}
//function to start finding a path
internal override void FindPath(IAlgorithmCompleted callback)
{
//running Algorithm and getting the path
m_LocatedPath = CreatePath(PathFindingGrid.GetInstance, m_SavedStart, m_SavedTarget);
callback.Invoke(
new CompletedProcessingCallback()
{
m_Callback = m_Callback,
m_Path = m_LocatedPath
});
}
//Algorithm
protected override List<GridNode> CreatePath(PathFindingGrid Grid, GridNode Start, GridNode Target)
{
if(Grid == null ||
Start == null ||
Target == null)
{
UnityEngine.Debug.Log("Missing Parameter, might be outside of grid");
return new List<GridNode>();
}
List<GridNode> Path = new List<GridNode>();
List<GridNode> OpenSet = new List<GridNode>();
List<GridNode> ClosedSet = new List<GridNode>();
OpenSet.Add(Start);
int Retry = 0;
while (OpenSet.Count > 0)
{
if(Retry > 3000 || Grid == null)
{
UnityEngine.Debug.Log("Path Inpossible Exiting");
break;
}
GridNode CurrentNode = OpenSet[0];
for (int i = 0; i < OpenSet.Count; i++)
{
if(OpenSet[i].Fcost < CurrentNode.Fcost || OpenSet[i].Fcost == CurrentNode.Fcost && OpenSet[i].Hcost < CurrentNode.Hcost)
{
CurrentNode = OpenSet[i];
}
}
OpenSet.Remove(CurrentNode);
ClosedSet.Add(CurrentNode);
if(CurrentNode == Target)
{
Path = RetracePath(CurrentNode,Start);
break;
}
GridNode[] neighbour = Grid.GetNeighbouringNodes(CurrentNode);
for (int i = 0; i < neighbour.Length; i++)
{
if (!neighbour[i].m_IsWalkable || ClosedSet.Contains(neighbour[i]))
continue;
int CostToNeighbour = CurrentNode.Gcost + Grid.GetDistance(CurrentNode, neighbour[i]);
if(CostToNeighbour < neighbour[i].Gcost || !OpenSet.Contains(neighbour[i]))
{
neighbour[i].Gcost = CostToNeighbour;
neighbour[i].Hcost = Grid.GetDistance(neighbour[i], Target);
neighbour[i].m_ParrentNode = CurrentNode;
if (!OpenSet.Contains(neighbour[i]))
OpenSet.Add(neighbour[i]);
}
}
Retry++;
}
return Path;
}
//retracing the path out of a node map
protected override List<GridNode> RetracePath(GridNode start, GridNode target)
{
List<GridNode> Output = new List<GridNode>();
GridNode current = start;
while(current != target)
{
Output.Add(current);
current = current.m_ParrentNode;
}
Output.Reverse();
return Output;
}
}
}
This shows the core of your code made thread safe.
internal class PathFindingThread
{
Task m_Worker;
ConcurrentQueue<CompletedProcessingCallback> m_CallbackQueue;
ConcurrentQueue<IAlgorithm> m_QueuedTasks;
internal int GetTaskCount
{
get
{
return m_QueuedTasks.Count;
}
}
internal PathFindingThread()
{
m_CallbackQueue = new ConcurrentQueue<CompletedProcessingCallback>();
m_QueuedTasks = new ConcurrentQueue<IAlgorithm>();
m_Worker = Task.Factory.StartNew(() =>
{
while (true)
{
IAlgorithm head = null;
if (m_QueuedTasks.TryDequeue(out head))
{
head.FindPath(new IAlgorithmCompleted(AddCallback));
}
else
{
Task.Delay(0);
}
}
});
}
internal void AddJob(IAlgorithm AlgorithmToRun)
{
m_QueuedTasks.Enqueue(AlgorithmToRun);
}
private void AddCallback(CompletedProcessingCallback callback)
{
m_CallbackQueue.Enqueue(callback);
}
private void Update()
{
CompletedProcessingCallback cb = null;
if (m_CallbackQueue.TryDequeue(out cb))
{
cb.m_Callback.Invoke(cb.m_Path);
}
}
}
Volatile is only good for changing the value of the field - not calling methods on a collection that is referenced by the field.
You propably do not need to have Volatile in CompletedProcessingCallback, but it depends where else this is used. Certainly having volatile on a struct field is a bad smell.
Resolve these thread issues first, then see if you still have the problem.
I have an desktop console application created in visual studio 2010.How do i convert it to windows service?. basically in debug mode i want it as normal app , but in release mode i want a service build output
You can do it this way:
namespace Program
{
static class Program
{
public static bool Stopped = false;
[STAThread]
static void Main(string[] args)
{
Interactive.Initialize();
Interactive.OnStopped += new Interactive.StopedDelegate(OnStopped);
Interactive.Title = Path.GetFileNameWithoutExtension(
Assembly.GetExecutingAssembly().Location);
if (args.Length == 0) Interactive.Run(RunProc);
else if (args[0] == "-svc") ServiceBase.Run(new Service());
}
public static void RunProc() { yourConsoleMain(); }
public static void OnStopped() { Stopped = true; exitFromMain(); }
}
public class Service : ServiceBase
{
public static string Name = Path.GetFileNameWithoutExtension(
Assembly.GetExecutingAssembly().Location);
public static string CmdLineSwitch = "-svc";
public static ServiceStartMode StartMode = ServiceStartMode.Automatic;
public static bool DesktopInteract = true;
public bool Stopped = false;
public Service() { ServiceName = Name; }
public void Start() { OnStart(null); }
protected override void OnStart(string[] args)
{
System.Diagnostics.EventLog.WriteEntry(
ServiceName, ServiceName + " service started.");
Thread thread = new Thread(MainThread);
thread.Start();
}
protected override void OnStop()
{
System.Diagnostics.EventLog.WriteEntry(
ServiceName, ServiceName + " service stopped.");
Stopped = true;
Application.Exit();
}
private void MainThread()
{
Interactive.Run(Program.RunProc);
if (!Stopped) Stop();
}
}
}
Let me explain this... Basically, in Main you define that your program starts as a service if it is started with argument '-svc'.
Put in RunProc() what you normally do in main(), and in OnStopped() event handler some code that will cause main() to exit.
Then, override ServiceBase and perform some basic start/stop service.
In Windows 7 and later you must explicitly define that your service can interact with desktop if you want to see some output. But there is another problem, console window cannot be shown. So I created this console simulator which can write and also read input.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace ProgramIO.Control
{
public delegate void WriteDelegate(string value, int x, int y);
public delegate void ReadDelegate(out string value, bool readLine);
public delegate void EnableInputDelegate(bool enable);
public partial class InteractiveForm : Form
{
private delegate void ClearInputBufferDelegate();
public enum EIOOperation { None = 0, Write, Read }
private EventWaitHandle eventInvoke =
new EventWaitHandle(false, EventResetMode.AutoReset);
private EventWaitHandle eventInput =
new EventWaitHandle(false, EventResetMode.AutoReset);
private bool readLine = false;
private string inputBuffer = "";
private int inputPosition = 0;
private int inputBufferPosition = 0;
private EIOOperation IOOperation;
private int bufferSize = 0x10000;
private bool CaretShown = false;
private delegate object DoInvokeDelegate(Delegate method, params object[] args);
private delegate void SetTitleDelegate(string value);
private delegate void SetForegroundcolorDelegate(Color value);
public string Title {
get { return Text; }
set {
if (InvokeRequired) InvokeEx(
(SetTitleDelegate)delegate(string title) { Text = title; },
1000, new object[] { value });
else Text = value; }}
public Color ForegroundColor {
get { return ForeColor; }
set {
if (InvokeRequired) InvokeEx(
(SetForegroundcolorDelegate)delegate(Color color) { ForeColor = color; },
1000, new object[] { value });
else ForeColor = value; }}
public InteractiveForm()
{
InitializeComponent();
DoubleBuffered = true;
}
#region Asynchronous Methods
private bool InvokeEx(Delegate method, int timeout, params object[] args)
{
BeginInvoke((DoInvokeDelegate)DoInvoke, new object[] { method, args });
if (eventInvoke.WaitOne(timeout)) return true;
else return false;
}
private void EnableInput(bool enable)
{
if (InvokeRequired)
InvokeEx((EnableInputDelegate)DoEnableInput, 1000, new object[] { enable });
else DoEnableInput(enable);
}
private void ClearInputBuffer()
{
if (InvokeRequired)
InvokeEx((ClearInputBufferDelegate)DoClearInputBuffer, 1000, new object[0]);
else DoClearInputBuffer();
}
public void Write(string value, int x = -1, int y = -1)
{
lock (this) {
IOOperation = EIOOperation.Write;
if (InvokeRequired)
InvokeEx((WriteDelegate)DoWrite, 1000, new object[] { value, x, y });
else DoWrite(value, x, y);
IOOperation = EIOOperation.None; }
}
public string Read(bool readLine)
{
lock (this) {
EnableInput(true);
IOOperation = EIOOperation.Read; this.readLine = readLine; string value = "";
ClearInputBuffer(); eventInput.WaitOne();
object[] args = new object[] { value, readLine };
if (InvokeRequired) {
InvokeEx((ReadDelegate)DoRead, 1000, args); value = (string) args[0]; }
else DoRead(out value, readLine);
//inputPosition = textBox.Text.Length; inputBuffer = "";
ClearInputBuffer();
IOOperation = EIOOperation.None;
EnableInput(false);
return value;
}
}
#endregion //Asynchronous Methods
#region Synchronous Methods
protected override void OnShown(EventArgs e) { base.OnShown(e); textBox.Focus(); }
public object DoInvoke(Delegate method, params object[] args)
{
object obj = method.DynamicInvoke(args);
eventInvoke.Set();
return obj;
}
private void CorrectSelection()
{
if (textBox.SelectionStart < inputPosition) {
if (textBox.SelectionLength > (inputPosition - textBox.SelectionStart))
textBox.SelectionLength -= inputPosition - textBox.SelectionStart;
else textBox.SelectionLength = 0;
textBox.SelectionStart = inputPosition; }
}
protected void DoClearInputBuffer()
{
inputPosition = textBox.Text.Length; inputBuffer = "";
}
protected void DoEnableInput(bool enable)
{
if (enable) { textBox.ReadOnly = false; textBox.SetCaret(true); }
else { textBox.ReadOnly = true; textBox.SetCaret(false); }
}
protected void DoWrite(string value, int x, int y)
{
string[] lines = textBox.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
string[] addLines = new string[0];
if (y == -1) y = lines.Length - 1;
if (lines.Length - 1 < y) addLines = new string[y - lines.Length - 1];
if (y < lines.Length) {
if (x == -1) x = lines[y].Length;
if (lines[y].Length < x)
lines[y] += new String(' ', x - lines[y].Length) + value;
else
lines[y] = lines[y].Substring(0, x) + value +
((x + value.Length) < lines[y].Length ?
lines[y].Substring(x + value.Length) : ""); }
else {
y -= lines.Length;
if (x == -1) x = addLines[y].Length;
addLines[y] += new String(' ', x - addLines[y].Length) + value; }
textBox.Text = (string.Join("\r\n", lines) +
(addLines.Length > 0 ? "\r\n" : "") + string.Join("\r\n", addLines));
textBox.Select(textBox.Text.Length, 0); textBox.ScrollToCaret();
inputBuffer = "";
}
protected void DoRead(out string value, bool readLine)
{
value = "";
if (readLine) {
int count = inputBuffer.IndexOf("\r\n");
if (count > 0) { value = inputBuffer.Substring(0, count); }}
else if (inputBuffer.Length > 0) {
value = inputBuffer.Substring(0, 1); }
inputBuffer = "";
}
private void textBox_TextChanged(object sender, EventArgs e)
{
if (IOOperation == EIOOperation.Read) {
inputBuffer = textBox.Text.Substring(inputPosition);
if (!readLine || inputBuffer.Contains("\r\n")) eventInput.Set(); }
if (textBox.Text.Length > bufferSize) { textBox.Text =
textBox.Text.Substring(textBox.Text.Length - bufferSize, bufferSize);
textBox.Select(textBox.Text.Length, 0); textBox.ScrollToCaret(); }
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (IOOperation != EIOOperation.Read ||
(e.KeyCode == Keys.Back && inputBuffer.Length == 0))
e.SuppressKeyPress = true;
}
private void textBox_MouseUp(object sender, MouseEventArgs e)
{
CorrectSelection();
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (!(IOOperation == EIOOperation.Read) ||
((e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) &&
textBox.SelectionStart < inputPosition))
CorrectSelection();
}
private void InteractiveForm_FormClosing(object sender, FormClosingEventArgs e)
{
eventInput.Set();
lock (this) { }
}
#endregion //Synchronous Methods
}
public class InteractiveWindow : TextBox
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
private delegate void SetCaretDelegate(bool visible);
private const int WM_SETFOCUS = 0x0007;
private bool CaretVisible = true;
public void SetCaret(bool visible)
{
if (InvokeRequired) Invoke((SetCaretDelegate)DoSetCaret, new object[] { visible });
else DoSetCaret(visible);
}
private void DoSetCaret(bool visible)
{
if (CaretVisible != visible)
{
CaretVisible = visible;
if (CaretVisible) ShowCaret(Handle);
else HideCaret(Handle);
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_SETFOCUS)
{
if (CaretVisible) { ShowCaret(Handle); }
else HideCaret(Handle);
}
}
}
}
namespace ProgramIO
{
using ProgramIO.Control;
public static class Interactive
{
public delegate void StopedDelegate();
public delegate void RunDelegate();
public static bool Initialized = false;
private static InteractiveForm frmIO = null;
private static Thread IOThread = null;
private static EventWaitHandle EventStarted =
new EventWaitHandle(false, EventResetMode.AutoReset);
public static string Title {
get { return frmIO.Title; }
set { frmIO.Title = value; } }
public static Color ForegroundColor {
get {return frmIO.ForeColor; }
set { frmIO.ForeColor = value; } }
public static event StopedDelegate OnStopped = null;
private static void form_Show(object sender, EventArgs e)
{
frmIO = sender as InteractiveForm;
EventStarted.Set();
}
private static void form_FormClosed(object sender, FormClosedEventArgs e)
{
lock (frmIO) {
frmIO = null;
Application.Exit(); }
}
public static void Initialize()
{
IOThread = new Thread(IOThreadProc);
IOThread.Name = "Interactive Thread"; IOThread.Start();
EventStarted.WaitOne();
Initialized = true;
}
public static void Run(RunDelegate runProc = null)
{
if (!Initialized) Initialize();
if (runProc != null) runProc();
Application.Run();
if (OnStopped != null) OnStopped();
}
public static void IOThreadProc()
{
InteractiveForm form = new InteractiveForm();
form.Shown += new EventHandler(form_Show);
form.FormClosed += new FormClosedEventHandler(form_FormClosed);
Application.Run(form);
}
public static void Write(string value, int x = -1, int y = -1)
{
if (frmIO != null) lock (frmIO) { frmIO.Write(value, x, y); }
}
public static void WriteLine(string value)
{
if (frmIO != null) lock (frmIO) {
Interactive.Write(value); Interactive.Write("\r\n"); }
}
public static int Read()
{
if (frmIO != null) lock (frmIO) {
string input = frmIO.Read(false);
if (input.Length > 0) return input[0]; }
return 0;
}
public static string ReadLine()
{
if (frmIO != null) lock (frmIO) { return frmIO.Read(true); }
else return "";
}
}
}
This last class, Interactive, actually serve as invoker for asynchronous methods, and it is used in Main() at the beginning.
You can skip this whole second section of code if you don't want to see console window when program is run as a windows service.
I have also created an Installer class for this, but it would be just to much code on this page.
EDIT: This InteractiveForm is actually a form with designer class, but very simple, consisting only of Form and EditBox inside filling its area.
Basicallly you need 3 projects in your solution:
Application itself
WinService for production
Console Application for test purposes
So your application must have some kind of Start() method with e.g. infinite loop that does all work and maybe Stop() method to stop processing.
Winservice project must contain class derived from ServiceBase, it'l have OnStartmethod that calls your application's Start and OnStop that calls application's Stop method.
Next, in console application you do pretty much same - calling Start method in console's entry point.
So far for debug you run your console app, and for release you publish your winservice project
Winservice class might look like:
upd
Winservice codez:
public class MyWinService : ServiceBase
{
IMyApplicationService _myApplicationService;
//constructor - resolve dependencies here
public MyWinService()
{
_myApplicationService = new MyApplicationService();
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
try
{
_myApplicationService.Start();
}
catch (Exception exception)
{
//log exception
}
}
protected override void OnStop()
{
base.OnStop();
try
{
_myApplicationService.Stop();
}
catch (Exception exception)
{
//log exception
}
}
}
Application service:
public class MyApplicationService : IMyApplicationService
{
public MyApplicationService()
{
//some initializations
}
public Start()
{
//do work here
}
public Stop()
{
//...
}
}
Figure 1 - Working Demo
I'm running a winforms application and I have implemented a status / progress TreeView. It can display the status (by icon) and progress of a (possibly hierarchical) set of tasks to accomplish. My question is not about how to implement the TreeView control itself. I've got that part covered. The TreeView is merely the way the background work is going to status / progress itself to the user.
I have a set of methods that I want to run on not the main UI thread. They need to run in order. They are steps in a larger process. I could organize them into a hierarchy; that would make a nice tree structure.
Each of these methods will be represented by a node in the tree. I probably got the idea for this method of visualization from the old Sql Server DTS status panel. I still like that idea.
I want to know when each method finishes and it's outcome, and possibly a few textual statuses along the way. I also want a general mechanism that I can use to bubble up progress. I will use those to do an owner drawn progress bar in the TreeView on the node that corresponds to that method.
I've read up some on multi-threading, and also on the Task class, but don't really understand it fully. And I don't care if the solution uses that or not. But maybe that would be more elegant I don't know. It seems more straight forward than call backs but maybe you know better.
The Task Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Linq;
namespace DeveloperWorkbench.Nodes
{
public class Task : INode
{
public delegate void TaskStatusDelegate(Task sender, TaskStatus taskStatus);
public event ProgressDelegate ProgressChanged;
public event StatusDelegate Status;
public event TaskStatusDelegate TaskStatusChanged;
public Task()
{
_children = new List<Task>();
}
[XmlIgnore()]
public bool CanHaveChildren { get; private set; }
private List<Task> _children;
public List<Task> Children
{
get
{
_children.ForEach(x => x.Parent = this);
return _children;
}
set
{
_children = value;
_children.ForEach(x => x.Parent = this);
}
}
[XmlIgnore()]
public List<string> ChildTypes { get; private set; }
public string FullName { get; set; }
private float _maxProgress = 0;
[Browsable(false)]
[XmlIgnore()]
public float MaxProgress
{
get { return _maxProgress; }
set
{
_maxProgress = value;
RaiseProgress(this, Progress, MaxProgress);
}
}
private Delegate _method;
[Browsable(false)]
[XmlIgnore()]
public Delegate Method
{
get { return _method; }
set
{
if (_method == value) return;
_method = value;
Name = Method.Method.Name;
TypeName = Method.Method.ReflectedType.FullName;
}
}
private string _name;
[ReadOnly(true)]
public string Name
{
get { return _name; }
set
{
if (_name == value) return;
_name = value;
//Method = GetMethodByName(???, _name);
FullName = ProperCaseToSpaces(_name);
}
}
[Browsable(false)]
[XmlIgnore()]
public INode Parent { get; set; }
private float _progress = 0;
[Browsable(false)]
[XmlIgnore()]
public float Progress
{
get { return _progress; }
set
{
_progress = value;
RaiseProgress(this, Progress, MaxProgress);
}
}
public List<KeyValuePair<string, object>> RelatedItems { get; set; }
private TaskStatus _taskStatus = TaskStatus.Created;
[Browsable(false)]
[XmlIgnore()]
public TaskStatus TaskStatus
{
get { return _taskStatus; }
set
{
_taskStatus = value;
TaskStatusChanged(this, _taskStatus);
}
}
[ReadOnly(true)]
public string TypeName { get; set; }
public bool Visited { get; set; }
public Task Add(Task child)
{
Children.Add(child);
child.Parent = this;
child.ProgressChanged += Child_Progress;
return child;
}
private void Done(System.Threading.Tasks.Task task)
{
TaskStatus = TaskStatus.RanToCompletion;
}
public void Execute()
{
Progress = 0;
TaskStatus = TaskStatus.Running;
var systemTask = new System.Threading.Tasks.Task((Action)Method);
systemTask.ContinueWith(Done);
systemTask.Start();
if (Parent != null)
systemTask.Wait();
}
private static string ProperCaseToSpaces(string text)
{
return Regex.Replace(text, #"(\B[A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))", " $1");
}
public void RaiseProgress(INode sender, float progress = 0, float maxProgress = 100)
{
ProgressChanged(sender, progress, maxProgress);
}
public void RaiseStatus(string status = "Ready")
{
Status(status);
}
public void Refresh(bool force)
{
throw new NotImplementedException();
}
public void RefreshChildren(bool force, string childType = null)
{
throw new NotImplementedException();
}
public List<KeyValuePair<string, INode>> RefreshRelatedItems(bool force)
{
throw new NotImplementedException();
}
//Usage: myTask.SetMethod(() => MyMethod(0, 40));
public void SetMethod(Action method)
{
Method = method;
}
//Usage: myTask.SetMethod(() => MyFunction(myArgument));
public void SetMethod<T>(Func<T> function)
{
Method = function;
}
public void SetMethod(Object target)
{
if (target.GetType().FullName == TypeName)
Method = GetMethodByName(target, Name);
else
{
var name = Name;
SetMethod(() => FakeExecute(this));
Name = name;
TypeName = null;
}
foreach (var child in Children)
{
child.SetMethod(target);
}
}
public void Child_Progress(INode sender, float progress = 0, float maxProgress = 100)
{
MaxProgress = _children.Sum(x => x.MaxProgress);
Progress = _children.Sum(x => x.Progress);
}
public static Task Create<T>(Func<T> method, Task parent = null)
{
var task = InnerCreate(parent);
task.SetMethod(method);
return task;
}
public static Task Create(Action method, Task parent = null)
{
var task = InnerCreate(parent);
task.SetMethod(method);
return task;
}
public static Task Create(string methodName, Task parent = null)
{
var task = InnerCreate(parent);
task.SetMethod(() => FakeExecute(task));
task.Name = methodName;
task.TypeName = null;
return task;
}
private static Task InnerCreate(Task parent)
{
var task = new Task();
if (parent != null)
parent.Add(task);
return task;
}
public static Task CurrentTask(Task rootTask, int stackFrame = 1)
{
var taskMethodName = new StackFrame(stackFrame).GetMethod().Name;
return Find(rootTask, taskMethodName);
}
private static void FakeExecute(Task task)
{
foreach (Task child in task.Children)
{
child.MaxProgress = 100;
child.Progress = 0;
child.TaskStatus = TaskStatus.WaitingToRun;
}
foreach (Task child in task.Children)
{
child.Execute();
}
}
private static Task Find(Task task, string methodName)
{
return task.Method.Method.Name == methodName ?
task :
task.Children.Select(child => Find(child, methodName)).FirstOrDefault(found => found != null);
}
static Delegate GetMethodByName(object target, string methodName)
{
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
MethodInfo method = target.GetType().GetMethod(methodName, bindingFlags);
return method.ReturnType == typeof(void) ? Delegate.CreateDelegate(typeof(Action), target, method) : null;
}
}
}
The StatusList class:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Retalix.R10.DeveloperWorkbench.Nodes;
using Retalix.R10.DeveloperWorkbench.UI.Helpers;
using Task = Retalix.R10.DeveloperWorkbench.Nodes.Task;
namespace DeveloperWorkbench.UI.Controls
{
public partial class StatusList : UserControl
{
// Import the SetWindowRgn function from the user32.DLL
// From the Unmanaged Code
[DllImport("user32.DLL", EntryPoint = "SetWindowRgn")]
private static extern int SetWindowRgn(int hWnd, int hRgn, int bRedraw);
[System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern System.IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
[System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
private static extern bool DeleteObject(System.IntPtr hObject);
public StatusList()
{
InitializeComponent();
}
private TreeNode Add(TreeNodeCollection nodes, string text, string imageKey, object tag)
{
var treeNode = nodes.Add(tag.GetHashCode().ToString(), text);
treeNode.Tag = tag;
treeNode.ImageKey = imageKey;
treeNode.SelectedImageKey = imageKey;
tvTreeView.ExpandAll();
return treeNode;
}
public TreeNode Add(Task task)
{
var nodes = tvTreeView.Nodes;
if (task.Parent != null)
nodes = Find(task.Parent).Nodes;
task.TaskStatusChanged += Task_TaskStatusChanged;
task.ProgressChanged += Task_Progress;
var treeNode = Add(nodes, task.FullName, StatusIcon(task.TaskStatus), task);
foreach(var child in task.Children)
{
Add(child);
}
return treeNode;
}
private TreeNode Find(object tag)
{
var treeNodes = tvTreeView.Nodes.Find(tag.GetHashCode().ToString(), true);
if (treeNodes.Length > 0)
return treeNodes[0];
return null;
}
private string StatusIcon(System.Threading.Tasks.TaskStatus status)
{
switch (status)
{
case TaskStatus.Canceled:
case TaskStatus.Created:
case TaskStatus.Faulted:
case TaskStatus.RanToCompletion:
return status.ToString();
break;
case TaskStatus.Running:
case TaskStatus.WaitingForChildrenToComplete:
return TaskStatus.Running.ToString();
break;
default:
if (status.ToString().StartsWith("Waiting"))
return "Waiting";
break;
}
return "Created";
}
private void tvTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
var task = (Task) e.Node.Tag;
if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
//e.Graphics.DrawRectangle(SystemPens.ControlDark, e.Bounds.Left, e.Bounds.Top , e.Bounds.Width - 1, e.Bounds.Height - 1);
}
if(task.TaskStatus == TaskStatus.Running)
{
var borderBrush = new LinearGradientBrush(new Point(e.Bounds.Left + 1, e.Bounds.Top + 3), new Point(e.Bounds.Left + 1, e.Bounds.Bottom), Color.White, Color.FromArgb(200, Color.LightGray));
var borderRectangle = new Rectangle(e.Bounds.Left + 1, e.Bounds.Top + 3, e.Bounds.Width - 10, e.Bounds.Height - 6);
var borderGraphicsPath = RoundedRectangle.Create(borderRectangle);
e.Graphics.FillPath(borderBrush, borderGraphicsPath);
e.Graphics.DrawPath(Pens.DarkGray, borderGraphicsPath);
//e.Graphics.FillRectangle(borderBrush, borderRectangle);
//e.Graphics.DrawRectangle(pen, borderRectangle);
if (task.Progress > 0)
{
//pen.DashStyle = DashStyle.Dot;
var width = (task.Progress / task.MaxProgress) * (e.Bounds.Width - 11);
var progressRectangle = new Rectangle(e.Bounds.Left + 2, e.Bounds.Top + 4, (int)width, e.Bounds.Height - 7);
var progressGraphicsPath = RoundedRectangle.Create(progressRectangle, 5, RoundedRectangle.RectangleCorners.TopLeft | RoundedRectangle.RectangleCorners.BottomLeft);
//e.Graphics.DrawRectangle(pen, rectangle);
var progressBrush = new LinearGradientBrush(new Point(progressRectangle.Left, progressRectangle.Top - 1), new Point(progressRectangle.Left, progressRectangle.Bottom), Color.White, Color.LimeGreen);
e.Graphics.FillPath(progressBrush, progressGraphicsPath);
//e.Graphics.FillRectangle(progressLinearGradientBrush, progressRectangle);
//GraphicsPath path = RoundedRectangle.Create(rectangle);
//e.Graphics.DrawPath(Pens.Black, path);
//System.IntPtr ptrBorder = CreateRoundRectRgn(e.Bounds.Left, e.Bounds.Top, e.Bounds.Left + 50, e.Bounds.Bottom, 5, 5);
//try { SetWindowRgn(tvTreeView.Handle.ToInt32(), ptrBorder.ToInt32(), 1) ; }
//finally { DeleteObject(ptrBorder); }
}
}
var textSize = e.Graphics.MeasureString(task.Name, tvTreeView.Font);
var controlText = SystemBrushes.ControlText;
e.Graphics.DrawString(task.Name, tvTreeView.Font, controlText, e.Bounds.Left - 1, e.Bounds.Top + e.Bounds.Height / 2f - textSize.Height / 2f);
//if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
// controlText = SystemBrushes.HighlightText;
}
public void Task_Progress(Nodes.INode sender, float progress = 0, float maxProgress = 100)
{
if (IsDisposed) return;
if (InvokeRequired)
{
Invoke(new ProgressDelegate(Task_Progress), sender, progress, maxProgress);
}
else
{
if (tvTreeView.IsDisposed) return;
var treeNode = Find(sender);
if (treeNode != null)
{
tvTreeView.Invalidate(treeNode.Bounds);
}
}
}
public void Task_TaskStatusChanged(Task sender, TaskStatus taskStatus)
{
if (IsDisposed) return;
if (InvokeRequired)
{
Invoke(new Task.TaskStatusDelegate(Task_TaskStatusChanged), sender, taskStatus);
}
else
{
if (tvTreeView.IsDisposed) return;
var treeNode = Find(sender);
if (treeNode != null)
{
treeNode.ImageKey = StatusIcon(taskStatus);
treeNode.SelectedImageKey = treeNode.ImageKey;
}
}
}
}
}
And how it's used:
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Task = Retalix.R10.DeveloperWorkbench.Nodes.Task;
namespace DeveloperWorkbench.UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BuildTaskHierarchy();
}
private Task _rootTask;
public void BuildTaskHierarchy()
{
var roottaskXml = #"c:\temp\roottask.xml";
if (File.Exists(roottaskXml))
{
//method hierarchy can be deserialized...
_rootTask = (Task)Serialization.Deserialize(typeof(Task), roottaskXml);
_rootTask.SetMethod(target: this);
}
else
{
//...or constructed from scratch
_rootTask = Task.Create("Avert War With The Klingons");
Task.Create(GetToTheEnterprise, _rootTask);
var taskC = Task.Create("Kill General Chang", _rootTask);
Task.Create(FindThatThingsTailpipe, taskC);
Task.Create(TargetThatExplosionAndFire, taskC);
Task.Create(ThwartCampKhitomerAssassination, _rootTask);
Task.Create(ExplainToHighCommand, _rootTask);
Serialization.Serialize(_rootTask, roottaskXml);
}
statusList1.Add(_rootTask);
}
private void GetToTheEnterprise()
{
LongOp();
}
private void FindThatThingsTailpipe()
{
LongOp();
}
private void TargetThatExplosionAndFire()
{
LongOp();
}
private void ThwartCampKhitomerAssassination()
{
LongOp();
}
private void ExplainToHighCommand()
{
LongOp();
}
private void LongOp()
{
var task = Task.CurrentTask(_rootTask, 2);
task.MaxProgress = 100;
for (var i = 0; i <= 50; i++)
{
task.Progress = i*2;
Thread.Sleep(25);
}
}
private void button1_Click(object sender, EventArgs e)
{
_rootTask.Execute();
}
}
}
I'm just posting my progress. I have tested this in my actual application and it's working. I still need a convenience function to raise progress from within any method. I am still looking for feedback about how I can reduce the instrumentation required here. I want the least invasive strategy possible. Something that watches the call chain at run time would be an awesome addition.
The Progress class makes updating a UI with progress quite easy.
Just create a progress instance from within your UI; something that can take whatever information the background process currently has an update the UI appropriately:
Progress<Tuple<Operation, int>> progress = new Progress<Tuple<Operation, int>>();
progress.ProgressChanged += (_, info) =>
{
TreeView node = GetTreeviewFromOperation(info.Item1);
UpdateNodeWithProgress(node, info.Item2);
};
You can adjust that to whatever your circumstances are. Presumably the background process will have some sort of type that represents an operation, and you can map that back to the tree node that represents it. You could also pass in any other info you need to use to update the UI. If you have a lot of information to pass consider creating a new named type to represent it, rather than just using a Tuple as I did here.
Then just pass the progress to your background process, whatever that is (it could be a new thread, a task, the callback of an asynchronous method, or whatever).
//this is the work to do in the background
public static void DoWork(IProgress<Tuple<Operation, int>> progress)
{
Thread.Sleep(1000); //placeholder for real work
progress.Report(something, 50);
}
//start that work in a new task; call from the UI thread
//right after creating the `Progress` instance
Task.Run(()=> DoWork(progress));
If you don't have .NET 4.5 you can create your own version of this class rather easily:
public interface IProgress<T>
{
void Report(T data);
}
public class Progress<T> : IProgress<T>
{
SynchronizationContext context;
public Progress()
{
context = SynchronizationContext.Current
?? new SynchronizationContext();
}
public Progress(Action<T> action)
: this()
{
ProgressReported += action;
}
public event Action<T> ProgressReported;
void IProgress<T>.Report(T data)
{
var action = ProgressReported;
if (action != null)
{
context.Post(arg => action((T)arg), data);
}
}
}
Read up on the BackgroundWorker class. It's an old but very simple way to do background work without going into the complexities of threading.
All you got to do is create one, handle its DoWork event to perform your logic (which will run in the background) and pass progress back to the main ui thread through its ReportProgress function which you will then handle to update the ui of your tree however you like.
The best option to update UI is to leave the synchronisation responsibilities to .NET itself. Make use of async and await that helps to switch to UI thread from background thread when the Task is completed.
This library solves the exact purpose that you require.
Take a look at the examples WPF and Blazor here.
NUGET package here.
I´m running a C# application with .NET Framework 2.0 to read data from SerialPort to get the weight from a scale.
The application works fine, does what it is supposed to do, but the number of threads keeps increasing and more memory is consumed until the application crashes, usually after around 4 hours.
When running with a serialport simulator the number of threads is stable around 30. But when I use an actual scale it goes greater than 500 threads.
I used Microsoft Managed Stack Explorer 1.0 to take a dump of the threads and almost all of them have exactly the following stack:
0. System.IO.Ports.SerialPort.CatchReceivedEvents (Source Unavailable)
1. System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents (Source Unavailable)
2. System.Threading._ThreadPoolWaitCallback.WaitCallback_Context (Source Unavailable)
3. System.Threading.ExecutionContext.Run (Source Unavailable)
4. System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal (Source Unavailable)
5. System.Threading._ThreadPoolWaitCallback.PerformWaitCallback (Source Unavailable)
I'm not able to identify the reason these threads are being created. Does anybody have any idea of what I'm missing here? Thanks!
This is my code:
Scale.cs -> creates a thread when method open() is called. The thread reads the value from getWeight().
Scales.cs -> treats events from serial port in method SerialPort_DataReceived(...). It's where m_SerialPort.ReadLine() is called and ends up providing the value to getWeight().
Scale.cs:
using System;
using System.Collections.Generic;
using System.Threading;
using ScalesGSS;
using StateMachine.Exceptions;
using StateMachine.Log;
using StateMachine.MessageOutput;
namespace StateMachine.DriverImplementation
{
class Scale : AScale
{
private const int Scale_version = 1;
private Thread thread = null;
private IScales gScale = null;
//
private string m_Type;
private string m_PortName;
private int m_BaudRate;
private char m_Parity;
private int m_DataBits;
private string m_StopBits;
private int m_CommandReturnLength;
private string m_CommandType;
private string m_CommandValue;
private int m_ReadTimeOutInMilliseconds;
private int m_WeightInitialPosition;
private int m_WeightFinalPosition;
private int m_TimeBetweenReadsInMilliseconds;
private int m_StableReadQuantity;
private int m_MinimumWeight;
private int m_ScaleID;
//
private double m_OldWeight = 0.0;
private double m_Offset = 0.0;
private double m_CurrentWeight = 0.0;
int m_WeightEqualCount = 0;
//
byte m_Status = 3; // "NO COMMUNICATION"
//
private bool m_Closed = false;
private static LogFactory m_Log = new LogFactory(LogCategory.Device, "");
ErrorDialog m_ErrorDialog = new ErrorDialog();
public Scale()
{
this.setClassName("Scale");
this.setDeviceType(DeviceType.Scale);
}
public void run()
{
try
{
if (this.m_Type.ToUpper().Equals("GENERICSCALES")) // GENERICSCALES or MOCKSCALES
this.gScale = new ScalesGSS.GenericScales();
else
this.gScale = new ScalesGSS.MockScales();
this.gScale.PortName = this.m_PortName;
this.gScale.BaudRate = this.m_BaudRate;
this.gScale.Parity = this.m_Parity.ToString();
this.gScale.DataBits = this.m_DataBits;
this.gScale.StopBits = this.m_StopBits;
this.gScale.CommandReturnLength = this.m_CommandReturnLength;
this.gScale.CommandType = this.m_CommandType;
this.gScale.CommandValue = this.m_CommandValue;
this.gScale.ReadTimeOut = this.m_ReadTimeOutInMilliseconds;
this.gScale.WeightInitialPosition = this.m_WeightInitialPosition;
this.gScale.WeightFinalPosition = this.m_WeightFinalPosition;
this.gScale.setParameters();
this.gScale.configurePort();
while (true)
{
if (this.m_Closed)
{
if (this.OpenedPort())
this.gScale.closePort();
break;
}
Thread.Sleep(this.m_TimeBetweenReadsInMilliseconds);
if (!this.OpenedPort())
{
if (!this.OpenPort())
{
m_Log.writeLogWarning("Error opening serialport.", " Port: " + this.m_PortName, true);
}
}
if (this.ErrorReadingWeight())
{
m_Log.writeLogWarning("Invalid weight.", " Port: " + this.m_PortName, true);
}
this.m_CurrentWeight = getWeight();
if (!ReadingTimeout())
{
if (this.m_WeightEqualCount > m_StableReadQuantity)
{
if (m_CurrentWeight > m_MinimumWeight)
m_Status = 2; // "WEIGHT STABLE"
else
{
m_Status = 0; // "SCALE FREE"
m_WeightEqualCount = 0;
}
}
else
{
if (m_CurrentWeight > m_MinimumWeight)
{
m_Status = 1; // "STABILIZING"
if ((this.m_CurrentWeight >= (this.m_OldWeight - this.m_Offset)) && (this.m_CurrentWeight <= (this.m_OldWeight + this.m_Offset)))
this.m_WeightEqualCount++;
else
this.m_WeightEqualCount = 0;
this.m_OldWeight = this.m_CurrentWeight;
}
else
{
m_Status = 0; // "SCALE FREE"
m_WeightEqualCount = 0;
}
}
}
else
{
m_WeightEqualCount = 0;
m_Status = 3; // "NO COMMUNICATION"
string v_Message = "No communication with scale. Port: " + m_PortName;
m_Log.writeLogWarning(v_Message, "", true);
AutoClosingMessageBox.Show(v_Message, "Scale", 10000);
}
}
}
catch (Exception v_Exception)
{
m_Log.writeLogError("run()", v_Exception);
}
}
private bool OpenedPort()
{
return this.gScale.OpenedPort;
}
private bool OpenPort()
{
bool v_OpenPort;
v_OpenPort = this.gScale.openPort();
if (!v_OpenPort)
{
m_ErrorDialog.getScaleErrorMessage(gScale);
}
return v_OpenPort;
}
private bool ErrorReadingWeight()
{
return this.gScale.ErrorReadingWeight;
}
private double getWeight()
{
return this.gScale.getWeight();
}
private DateTime LastGoodReading()
{
return gScale.LastGoodReading;
}
private void setLastGoodReading(DateTime p_Value)
{
gScale.LastGoodReading = p_Value;
}
private bool ReadingTimeout()
{
if (m_ReadTimeOutInMilliseconds > 0)
{
DateTime v_LastGoodReading = LastGoodReading() == DateTime.MinValue ? DateTime.Now : LastGoodReading();
setLastGoodReading(DateTime.Now);
return DateTime.Now > v_LastGoodReading.AddMilliseconds(m_ReadTimeOutInMilliseconds);
}
else
return false;
}
#region "IDriverService"
public override byte getStatus()
{
return m_Status;
}
public override byte[] read()
{
return System.Text.ASCIIEncoding.ASCII.GetBytes(m_CurrentWeight.ToString());
}
public override byte[] read(int p_InitialPosition, int p_Size)
{
return read();
}
public override byte[] write(byte[] p_Data)
{
string v_Temp = System.Text.ASCIIEncoding.ASCII.GetString(p_Data);
if (v_Temp.Equals("getScaleNumber"))
return System.Text.ASCIIEncoding.ASCII.GetBytes(m_ScaleID.ToString());
else
throw new EDriverAccess(1, "Not implemented");
}
public override bool open()
{
this.thread = new Thread(run);
this.thread.Name = "SCALE";
this.thread.IsBackground = true;
this.thread.Start();
return true;
}
public override bool close()
{
try
{
this.release();
return true;
}
catch
{
return false;
}
}
public override int getVersion()
{
return Scale_version;
}
public override void setProperties(Dictionary<string, string> p_props)
{
try
{
this.m_Type = p_props["type"];
this.m_PortName = p_props["portName"];
this.m_BaudRate = Int32.Parse(p_props["baudRate"]);
this.m_Parity = char.Parse(p_props["parity"]);
this.m_DataBits = Int32.Parse(p_props["dataBits"]);
this.m_StopBits = p_props["stopBits"];
this.m_CommandReturnLength = Int32.Parse(p_props["returnLength"]);
this.m_CommandType = p_props["commandType"];
this.m_CommandValue = p_props["commandValue"];
this.m_ReadTimeOutInMilliseconds = Int32.Parse(p_props["readTimeout"]);
this.m_WeightInitialPosition = Int32.Parse(p_props["weightInitPos"]);
this.m_WeightFinalPosition = Int32.Parse(p_props["weightFinPos"]);
this.m_TimeBetweenReadsInMilliseconds = Int32.Parse(p_props["delayLeitura"]);
this.m_StableReadQuantity = Int32.Parse(p_props["qtdeLeituraEstavel"]);
this.m_MinimumWeight = Int32.Parse(p_props["pesoMinimo"]);
this.m_ScaleID = Int32.Parse(p_props["numBalanca"]);
if (p_props.ContainsKey("precision"))
this.m_Offset = Int32.Parse(p_props["precision"]);
}
catch (Exception)
{
throw new Exception();
}
}
public override void release()
{
this.m_Closed = true;
m_Status = 3; // "NO COMMUNICATION"
}
#endregion
}
}
Scales.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Timers;
using Scales.Util;
namespace Scales.DLL
{
public class Scales : Status
{
public event EventHandler StableWeightChanged;
protected virtual void OnCountdownCompleted(EventArgs e)
{
if (StableWeightChanged != null)
StableWeightChanged(this, e);
}
System.Timers.Timer timerTimeWithoutSample;
private int m_IntervalsWithoutSample = 0;
private string m_EndOfWeightChar = "";
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
m_IntervalsWithoutSample++;
}
public int IntervalsWithoutSample { get { return m_IntervalsWithoutSample; } }
private SerialPort m_SerialPort;
public string PortName { get; set; }
public int BaudRate { get; set; }
public int DataBits { get; set; }
private Double m_Weight = 0;
public Double Weight
{
get
{
if (m_BufferWeights.Count > 0)
{
try
{
m_Weight = treatReceivedValue(m_BufferWeights[m_BufferWeights.Count - 1]);
}
catch
{
}
finally
{
ErrorReadingWeight = (m_Weight != -1 ? false : true);
}
}
else
{
m_Weight = 0;
}
return m_Weight;
}
}
public List<Double> getAndFlushPastWeights()
{
List<Double> v_FlushedValues = new List<double>();
Double v_WeightCursor;
while (m_BufferWeights.Count > 1 && v_FlushedValues.Count < 200)
{
v_WeightCursor = treatReceivedValue(m_BufferWeights[0]);
if (v_WeightCursor >= 0)
{
v_FlushedValues.Add(v_WeightCursor);
}
m_BufferWeights.RemoveAt(0);
}
return v_FlushedValues;
}
public void ResetWeights()
{
if (m_BufferWeights != null)
{
m_BufferWeights.Clear();
}
}
public string NewLineCommandType { get; set; }
public string NewLineCommand { get; set; }
public int ReturnLength { get; set; }
public int WeightInitialPosition { get; set; }
public int WeightFinalPosition { get; set; }
public int MotionBitPos { get; set; }
public int ReadTimeOut { get; set; }
public bool OpenedPort { get; private set; }
public bool ErrorReadingWeight { get; private set; }
public DateTime LastGoodReading { get; private set; }
public bool IsStable { get; private set; }
private Parity PortParity { get; set; }
public string SerialParity
{
get { return PortParity.ToString(); }
set
{
setParity(value);
}
}
public int WeightReadLength
{
get
{
if (WeightFinalPosition >= WeightInitialPosition)
{
return WeightFinalPosition - WeightInitialPosition + 1;
}
else
{
return 0;
}
}
}
private StopBits PortStopBits { get; set; }
public string SerialStopBits
{
get { return PortStopBits.ToString(); }
set
{
setStopBits(value);
}
}
private void setParity(string p_Parity)
{
if (p_Parity.Equals(Parity.Even.ToString()))
{
PortParity = Parity.Even;
}
else if (p_Parity.Equals(Parity.Mark.ToString()))
{
PortParity = Parity.Mark;
}
else if (p_Parity.Equals(Parity.Odd.ToString()))
{
PortParity = Parity.Odd;
}
else if (p_Parity.Equals(Parity.Space.ToString()))
{
PortParity = Parity.Space;
}
else
{
PortParity = Parity.None;
}
}
private void setStopBits(string p_StopBits)
{
if (p_StopBits.Equals(StopBits.One.ToString()))
{
PortStopBits = StopBits.One;
}
else if (p_StopBits.Equals(StopBits.OnePointFive.ToString()))
{
PortStopBits = StopBits.OnePointFive;
}
else if (p_StopBits.Equals(StopBits.Two.ToString()))
{
PortStopBits = StopBits.Two;
}
else if (p_StopBits.Equals("1"))
{
PortStopBits = StopBits.One;
}
else if (p_StopBits.Equals("1.5"))
{
PortStopBits = StopBits.OnePointFive;
}
else if (p_StopBits.Equals("2"))
{
PortStopBits = StopBits.Two;
}
else
{
PortStopBits = StopBits.None;
}
}
public Scales()
{
OpenedPort = false;
ErrorReadingWeight = false;
IsStable = false;
m_IntervalsWithoutSample = 999999;
timerTimeWithoutSample = new System.Timers.Timer(5);
timerTimeWithoutSample.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
}
private int ignoreNextXValues;
public void resetScale()
{
ErrorReadingWeight = false;
IsStable = false;
m_IntervalsWithoutSample = 999999;
ignoreNextXValues = 2;
m_BufferWeights.Clear();
m_BufferTime.Clear();
if (m_SerialPort != null && m_SerialPort.IsOpen)
{
m_SerialPort.Close();
m_SerialPort.Open();
m_SerialPort.DiscardInBuffer();
}
}
List<String> m_BufferWeights = new List<String>();
List<String> m_BufferTime = new List<String>();
public bool openPort()
{
try
{
if (m_SerialPort.IsOpen)
{
m_SerialPort.Close();
}
m_SerialPort.Open();
resetScale();
OpenedPort = true;
return true;
}
catch (Exception ex)
{
MessageDetail = ex.Message;
Return = -100;
OpenedPort = false;
return false;
}
}
public bool closePort()
{
try
{
if (m_SerialPort != null)
{
if (m_SerialPort.IsOpen)
{
m_SerialPort.Close();
}
}
OpenedPort = false;
return true;
}
catch (Exception ex)
{
MessageDetail = ex.Message;
Return = -101;
return false;
}
}
public bool configurePort()
{
try
{
m_SerialPort = new SerialPort();
m_SerialPort.PortName = PortName;
m_SerialPort.BaudRate = BaudRate;
m_SerialPort.Parity = PortParity;
m_SerialPort.DataBits = DataBits;
m_SerialPort.StopBits = PortStopBits;
m_SerialPort.ReadTimeout = ReadTimeOut > 0 ? ReadTimeOut : SerialPort.InfiniteTimeout;
m_SerialPort.NewLine = getNewLineCommand();
m_SerialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
return true;
}
catch (Exception ex)
{
MessageDetail = ex.Message;
Return = -102;
return false;
}
}
private string getNewLineCommand()
{
string v_Command = string.Empty;
if (NewLineCommandType.ToUpper().Equals(CommandTypes.CHAR.ToUpper()))
{
byte v_Char = Convert.ToByte(NewLineCommand);
v_Command = Convert.ToChar(v_Char).ToString();
}
else if (NewLineCommandType.ToUpper().Equals(CommandTypes.STRING.ToUpper()))
{
v_Command = NewLineCommand;
}
else
{
char[] v_delimiters = { '|' };
String[] v_Strings = NewLineCommand.Split(v_delimiters);
if (v_Strings.Length == 2)
{
v_Command = v_Strings[0];
m_EndOfWeightChar = v_Strings[1];
}
else
{
v_Command = NewLineCommand;
}
}
return v_Command;
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
LastGoodReading = DateTime.Now;
string ReadLine = m_SerialPort.ReadLine();
m_BufferWeights.Add(ReadLine);
}
catch (Exception)
{
m_Weight = 0;
LastGoodReading = DateTime.MinValue;
}
}
private Double treatReceivedValue(string p_ReceivedValue)
{
try
{
if (ignoreNextXValues > 0) ignoreNextXValues--;
if (ignoreNextXValues > 0) return 0;
double v_Value = double.MinValue;
p_ReceivedValue = p_ReceivedValue.Replace("\r", "").Replace("\n", "");
m_IntervalsWithoutSample = 0;
if (p_ReceivedValue.Length < WeightInitialPosition + WeightReadLength)
{
return -1;
}
if (MotionBitPos != -1 && p_ReceivedValue.Length < MotionBitPos - 1)
{
return -1;
}
string strValor = "";
if (NewLineCommandType.ToUpper().Equals(CommandTypes.VARIABLE_LENGTH.ToUpper()))
{
int v_EndCharPos = p_ReceivedValue.IndexOf(m_EndOfWeightChar);
if (v_EndCharPos != -1)
{
strValor = p_ReceivedValue.Substring(0, v_EndCharPos).Trim();
}
}
else
{
strValor = p_ReceivedValue.Substring(WeightInitialPosition, WeightReadLength).Trim();
}
bool IsDouble = double.TryParse(strValor, out v_Value);
if (IsDouble)
{
if (MotionBitPos != -1)
{
string bit = p_ReceivedValue.Substring(MotionBitPos, 1).Trim();
if (bit == "1")
{
IsStable = true;
}
else IsStable = false;
}
else
{
IsStable = true;
}
return v_Value;
}
else
{
return -1;
}
}
catch (Exception ex)
{
Return = -200;
MessageDetail = ex.Message + " - Fonte:readScales";
ErrorReadingWeight = true;
}
return -1;
}
}
}
I had a similar problem, using SerialPort.ReadExisting() insted of SerialPort.ReadLine() I was able to avoid the creation of infinite threads.
You should try to reduce your problematic code down to something more manageable, as it will make it easier for others to debug. There's a lot of application logic in there that's probably not relevant to the problem which can make it hard for people to see what's going on. You'll get a lot more answers if your example is shorter. You may even figure the problem out yourself in the process!
Having said that, I have a hunch about what's wrong but you'll need to do a little bit of the leg-work yourself to discover if I'm right or wrong:
The .NET serial port works by waiting for data to come in, and then firing the DataReceived event on a worker thread whenever it notices that there's new data. I believe you have 400 or 500 of these worker threads that never complete their work, so they never go away.
Your event handler for the SerialPort.DataReceived event looks like it's blocking waiting for a whole line to come in, but the event is going to be fired whenever there's some amount of new data on the serial port (not necessarily a whole line). If a long line of text comes in, the DataReceived event is going to fire many times, each on it's own worker thread. These worker threads are synchronized to each other, so they're all going to wait for the previous one to finish.
The first thread that gets queued up is going to wait for a while at m_SerialPort.ReadLine() until the whole line comes in.
A bunch of threads queue up behind the first thread as more characters come in. The rest of the threads will end up waiting for the first thread to finish running your event handler.
Finally, the whole line comes in. The first thread finishes, and one of the 5 or 6 that are queued up behind it gets to run and the process starts all over.
The running thread blocks on ReadLine, 5 or 6 more queue up behind it. (We're now back at 1)
Eventually you have so many threads queued up that you run into memory issues.
You probably have the read-timeout on m_SerialPort set to timeout.Infinite. If you set the timeout to something smaller, like 1 second (1000) and you get a lot of TimeoutExceptions in your SerialPort_DataReceived method, then I'm probably right
Side Note You should catch a more specific exception type in your DataReceived event handler. Catching exception can mask exactly this type of problem.
If I've correctly diagnosed the problem, you'll need to change the architecture of your program a little bit. The simplest thing to do is not subscribe to the DataReceived event and to have a single worker thread call m_SerialPort.ReadLine(); with an infinite timeout. When it reads a line, have that worker thread raise an event with the whole line of text received and subscribe to THAT event instead of the SerialPort.DataReceived(); event.
Alternatively, if you want to subscribe to the SerialPort.DataReceived(); event, then read individual characters out of the SerialPort until SerialPort.BytesToRead is zero and stick them in a buffer. Then, when you have a whole line raise some "LineReceived" event that you make yourself that returns the whole line at once as one of the EventArgs. This method is doesn't require you to spool up your own thread that persists for a really long time.