I have this program that creates threads on which i must create queue folders and
check them for files.
Now I noticed my program failed after processing a huge number of files without problems.
I produces a UnauthorizedAccessException so I went looking for that folder and it appears
the folder has been locked out totally?!
Could this be my anti-virus blocking access or is it something I must fix on my thread?
public class worker
{
public bool Stopping = false;
private System.Timers.Timer _timer;
private List<string> _files;
#region Feedback
public event FeedbackHandler Feedback;
public delegate void FeedbackHandler(object sender, string text);
#endregion
#region Properties
private string _name;
public string Name
{
get { return _name; }
}
private string _folder;
public string Folder
{
get { return _folder; }
set { _folder = value; }
}
private string _outfolder = Path.Combine(shared.Root, "out");
public string Outfolder
{
get { return _outfolder; }
set { _outfolder = value; }
}
private string _backupfolder = Path.Combine(shared.Root, "backup");
public string Backupfolder
{
get { return _backupfolder; }
set { _backupfolder = value; }
}
private string _filter = "*.*";
public string Filter
{
get { return _filter; }
set { _filter = value; }
}
private SearchOption _subfolders = SearchOption.TopDirectoryOnly;
public bool Subfolders
{
get { return (_subfolders == SearchOption.AllDirectories); }
set { if (value) { _subfolders = SearchOption.AllDirectories; } else { _subfolders = SearchOption.TopDirectoryOnly; } }
}
#endregion
#region Constructor
public worker(string Name)
{
_name = Name;
_folder = Path.Combine(shared.Root, "queues");
_folder = Path.Combine(_folder, Name);
}
#endregion
#region Destructor
~worker()
{
}
#endregion
#region Control
public void Start()
{
Stopping = false;
Directory.CreateDirectory(_folder);
_timer = new System.Timers.Timer(1);
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
Feedback(this, "[" + _name + "] started!");
}
public void Stop()
{
Stopping = true;
Feedback(this, "[" + _name + "] stopped...");
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (Stopping)
{
_timer.Stop();
_files.Clear();
return;
}
_timer.Stop();
Process();
_timer.Start();
}
#endregion
void Process()
{
if (Directory.Exists(_folder))
{
_files = Directory.GetFiles(_folder, _filter, _subfolders).ToList();
foreach (string _file in _files.ToArray())
{
if (Stopping) { break; }
document _document = new document(_file);
_document.Copy(_backupfolder);
_document.Move(_outfolder);
}
_files = new List<string>();
}
}
}
public class document
{
private string _header;
#region Feedback
public event FeedbackHandler Feedback;
public delegate void FeedbackHandler(object sender, string text);
#endregion
#region Properties
private string _file;
public string File
{
get { return _file; }
}
private job _job;
public job Job
{
get { return _job; }
}
#endregion
#region Constructor
public document(string File)
{
_file = File;
_header = shared.FileOperations.ReadHeader(_file);
_job = new job(_file, _header);
_job.ReadHeader();
}
#endregion Constructor
public void Copy(string Folder)
{
string _backupfile;
_backupfile = Path.Combine(Folder,_job.Name);
_backupfile = Path.Combine(_backupfile,_job.Company);
_backupfile = Path.Combine(_backupfile, DateTime.Now.ToString("yyyy"));
_backupfile = Path.Combine(_backupfile, DateTime.Now.ToString("MMMM"));
Directory.CreateDirectory(_backupfile);
_backupfile = Path.Combine(_backupfile, Path.GetFileName(_file));
shared.FileOperations.CopyFile(_file, _backupfile, true);
}
public void Move(string Folder)
{
string _outfile;
_outfile = Path.Combine(Folder, Path.GetFileNameWithoutExtension(_file));
shared.FileOperations.MoveFile(_file, _outfile, true);
}
}
public struct shared
{
public static string Root
{
get
{
string _base = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
return Directory.GetParent(_base).Parent.FullName.ToString();
}
}
public struct Patterns
{
public const string Header = #"\^?JOB\s(?<JOB>[a-zA-Z0-9]+[0-9]{3})[D]?(?<ACTION>[JFE]+)(?<COMPANY>[A-Z]{2,2})\s" +
#"(?<EMAIL>-emto=.*)?" +
#"-C(?<COPIES>[0-9]{2,2})\s" +
#"-Z""(?<PRINTER>[A-Z0-9]+)""\s" +
#"(?:\^?PAGE 01|(?<FAX>\^?FAX.*)\s\^?PAGE 01?)";
public const string Jump = #"\^PAGE\s[0-9]+";
public const string Pages = #"(\$?PAGE\s)";
public const string Fax = #"\^?FAX FROM_COMPANY\s""(?<FROM>.*)""\s" +
#"\^?FAX FROM_FAX_NUM\s""(?<FFAX>.*)""\s" +
#"\^?FAX FROM_NAME\s""(?<FNAME>.*)""\s" +
#"\^?FAX TO_FAX_NUM\s""(?<TFAX>.*)""\s" +
#"\^?FAX TO_COMPANY\s""(?<TO>.*)""\s" +
#"\^?FAX TO_NAME\s""(?<TNAME>.*)""\s" +
#"\^?FAX WHO\s""(?<WHO>.*)""\s" +
#"\^?FAX ID\s+(?<ID>.*)";
public const string Mail = #"-em([^\s=]+)=(""[^""]*""|[^\s]+)";
public const string Seperator = #"^";
}
public struct FileOperations
{
// Encoding
public static Encoding ReadEncoding = Encoding.GetEncoding(1252);
public static Encoding WriteEncoding = Encoding.UTF8;
// Timeouts
static int Timeout = 1;
static int FileTimeout = 10000; // 10 seconds/file permitted..
// Header
public static string ReadHeader(string SourceFile)
{
return ReadHeader(SourceFile, Patterns.Jump);
}
public static string ReadHeader(string SourceFile, string Beacon)
{
WaitFile(SourceFile);
string r = null;
string l = null;
try
{
StreamReader _reader = new StreamReader(SourceFile, ReadEncoding);
Match _match;
do
{
l = _reader.ReadLine();
r += l + " ";
_match = Regex.Match(l, Beacon);
} while (!_match.Success);
_reader.Close();
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
return r;
}
// Read Contents
public static List<string> ReadFile(string SourceFile)
{
return ReadFile(SourceFile, Patterns.Seperator);
}
public static List<string> ReadFile(string SourceFile, string Seperator)
{
WaitFile(SourceFile);
List<string> lines = new List<string>();
try
{
StreamReader sr = new StreamReader(SourceFile, Encoding.GetEncoding(1250));
string tmp = null;
string line = null;
while (!sr.EndOfStream)
{
line = sr.ReadLine();
if (!string.IsNullOrEmpty(line) && line.Substring(0, 1) == Seperator)
{
if (!string.IsNullOrEmpty(tmp))
{
lines.Add(tmp);
}
tmp = line.Replace(Seperator, "^");
}
else
{
tmp += Environment.NewLine + line;
}
}
sr.Close();
if (!string.IsNullOrEmpty(tmp))
{
lines.Add(tmp);
}
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) {throw ex;}
}
return lines;
}
// Write Contents
public static void WriteFile(string DestinationFile, List<string> Lines)
{
try
{
File.WriteAllLines(DestinationFile, Lines.ToArray(), WriteEncoding);
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
}
public static void WriteFile(string DestinationFile, string Contents)
{
try
{
File.WriteAllText(DestinationFile, Contents);
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
}
// Move File
public static void MoveFile(string SourceFile, string DestinationFile, bool Overwrite)
{
WaitFile(SourceFile);
try
{
string _count = null;
string _destination = Path.GetDirectoryName(DestinationFile);
string _file = Path.GetFileNameWithoutExtension(DestinationFile);
string _extension = Path.GetExtension(DestinationFile);
string[] _files = Directory.GetFiles(_destination, _file + "*");
if (_files.Length > 0)
{
if (Overwrite)
{
for (int x = 0; x <= _files.Length - 1; x++)
{
File.Delete(_files[x]);
}
}
else
{
_count = "_" + (_files.Length - 1).ToString("D4");
}
}
DestinationFile = Path.Combine(_destination, _file + _count + _extension);
File.Move(SourceFile, DestinationFile);
}
catch (Exception ex)
{
if (Debugger.IsAttached) { throw ex; }
}
}
public static void CopyFile(string SourceFile, string DestinationFile, bool Overwrite)
{
WaitFile(SourceFile);
try
{
string _count = null;
string _destination = Path.GetDirectoryName(DestinationFile);
string _file = Path.GetFileNameWithoutExtension(DestinationFile);
string _extension = Path.GetExtension(DestinationFile);
string[] _files = Directory.GetFiles(_destination, _file + "*");
if (_files.Length > 0)
{
if (Overwrite)
{
for (int x = 0; x <= _files.Length - 1; x++)
{
File.Delete(_files[x]);
}
}
else
{
_count = "_" + (_files.Length - 1).ToString("D4");
}
}
DestinationFile = Path.Combine(_destination, _file + _count + _extension);
File.Copy(SourceFile, DestinationFile);
}
catch (Exception ex)
{
if (Debugger.IsAttached) { throw ex; }
}
}
// Delete File
public static void DeleteFile(string SourceFile)
{
WaitFile(SourceFile);
try
{
File.Delete(SourceFile);
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
}
// Check File
static void WaitFile(string SourceFile)
{
Timeout = 1;
while (!File.Exists(SourceFile))
{
System.Threading.Thread.Sleep(Timeout);
Timeout++;
if (Timeout == FileTimeout)
{
// todo
if (Debugger.IsAttached) { throw new Exception("Timout exceeded!"); }
}
}
Timeout = 1;
while (!IsFileReady(SourceFile))
{
System.Threading.Thread.Sleep(Timeout);
Timeout++;
if (Timeout == FileTimeout)
{
// todo
if (Debugger.IsAttached) { throw new Exception("Timout exceeded!"); }
}
}
}
static bool IsFileReady(String SourceFile)
{
try
{
using (FileStream inputStream = File.Open(SourceFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
if (inputStream.Length > 0)
{
return true;
}
else
{
return false;
}
}
}
catch (Exception)
{
return false;
}
}
}
public struct Functions
{
public static string CleanXML(string Text)
{
Text = Text.Replace(#"&", #"&");
Text = Text.Replace(#"<", #"<");
Text = Text.Replace(#">", #">");
Text = Text.Replace(#"""", #""");
Text = Text.Replace(#"'", #"'");
return Text;
}
}
}
void Work(string Name)
{
_worker = _workers.FirstOrDefault(w => w.Name == Name);
if (_worker == null)
{
_worker = new worker(Name);
_worker.Feedback+=new worker.FeedbackHandler(Feedback);
_worker.Folder = Path.Combine(_queuefolder, Name);
_worker.Outfolder = _outfolder;
_worker.Backupfolder = _backupfolder;
_workers.Add(_worker);
Thread _thread = new Thread(_worker.Start);
_thread.Start();
_thread.Join();
}
}
To clarify what i meant:
//worker class
private volatile bool _stopping;
private Thread _thread;
public void Start()
{
_stopping = false;
Directory.CreateDirectory(_folder);
_thread = new Thread(Process);
_thread.Start();
Feedback(this, "[" + _name + "] started!");
}
public void Stop()
{
_stopping = true;
_thread.Join();
Feedback(this, "[" + _name + "] stopped...");
}
private void Process()
{
while(!_stopping)
{
......
Thread.Sleep(100);
}
}
Because the way you are using timers... It's wrong. And while its interesting to know, why windows locks the folder, you should start from doing some refactoring. It might actually solve your problem along the way.
Related
I tried to write an autoupdater for a program from me.
I already got rid of a stackOverflow and such but now my Program seems to run endless when he comes to a variable. And do nothing.
I tried to get info with cw and check where it is hanging but i get nothing and can not find it.
My main
{
updater = new Updater(this);
updater.DoUpdate();
}
public string ApplicationName {
get { return "MyProgram"; }
}
public string ApplicationID {
get { return "MyProgramID"; }
}
public Assembly ApplicationAssembly {
get { return System.Reflection.Assembly.GetExecutingAssembly(); }
}
public Icon ApplicationIcon {
get { return this.Icon; }
}
public Uri UpdateXmlLocation {
get { return new Uri("UrlToXml"); }
}
public Form Context {
get { return this; }
}
in my XML class
public class UpdateXml
{
private Version version;
public Uri uri;
private string fileName;
private string md5;
private string description;
private string launchArgs;
internal Version Version {
get { return this.Version; }
}
internal Uri Uri {
get { return this.Uri; }
}
internal string FileName {
get { return this.fileName; }
}
internal string MD5 {
get { return this.md5; }
}
internal string Description {
get { return this.description; }
}
internal string LaunchArgs {
get { return this.launchArgs; }
}
after a while (code running fine it come to the part that crash)
private void DwnloadUpdate(UpdateXml update)
{
updateDownloadForm form = new updateDownloadForm(update.Uri, this.applicationInfo.ApplicationIcon);
after this code I expect that my dl windows open and the dl starts and the program get update
My Updater class
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace updater
{
public class Updater
{
private Iupdater applicationInfo;
private BackgroundWorker bgWorker;
public Updater(Iupdater applicationInfo)
{
this.applicationInfo = applicationInfo;
this.bgWorker = new BackgroundWorker();
this.bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
this.bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
}
public void DoUpdate()
{
if (!this.bgWorker.IsBusy)
this.bgWorker.RunWorkerAsync(this.applicationInfo);
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
Iupdater application = (Iupdater)e.Argument;
if (!UpdateXml.ExistOnServer(application.UpdateXmlLocation))
{
e.Cancel = true;
}
else
{
UpdateXml ux = UpdateXml.Parse(application.UpdateXmlLocation, application.ApplicationID);
if (ux == null)
{
e.Cancel = true;
}
else
{
e.Result = ux;
}
}
}
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(!e.Cancelled)
{
UpdateXml update = (UpdateXml)e.Result;
if(update == null)
{
Console.WriteLine("Update NULL");
}
Console.WriteLine("test3.1");
Console.WriteLine(this.applicationInfo.ApplicationAssembly.GetName().Version);
if(this.applicationInfo.ApplicationAssembly.GetName().Version != null)
{
Console.WriteLine("YES!");
} else
{
Console.WriteLine("NO!");
}
Console.WriteLine("test3.2");
if (update != null && update.IsNewerThan(this.applicationInfo.ApplicationAssembly.GetName().Version))
{
Console.WriteLine("test4");
if (new updateInformation(applicationInfo, update).ShowDialog(this.applicationInfo.Context) == DialogResult.Yes)
this.DwnloadUpdate(update);
}
}
}
private void DwnloadUpdate(UpdateXml update)
{
Console.WriteLine(update.Uri);
if(update.Uri == null)
Console.WriteLine("null");
updateDownloadForm form = new updateDownloadForm(update.Uri, this.applicationInfo.ApplicationIcon);
Console.WriteLine("ich bin hier drinnen");
DialogResult result = form.ShowDialog(this.applicationInfo.Context);
if(result == DialogResult.OK)
{
string currentPath = this.applicationInfo.ApplicationAssembly.Location;
string newPath = Path.GetDirectoryName(currentPath) + "\\" + update.FileName;
UpdateApplication(form.TempFilePath, currentPath, newPath, update.LaunchArgs);
Application.Exit();
}
else if(result == DialogResult.Abort)
{
MessageBox.Show("The update download was cancelled. \nThis programm has not been modified.", "Update Download Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("There was a Problem downloading the Updat. \nThis programm has not been modified.", "Update Download Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateApplication(string tempFilePath, string currentPath, string newPath, string launchArgs)
{
string argument = "/C Choice /C Y /N /D Y /T 4 & Del /F /Q \"{0}\" & Choice /C Y /N /D Y /T 2 & Move /Y \"{1}\" \"{2}\" & Start \"\" /D \"{3}\" \"{4}\"{5}";
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = string.Format(argument, currentPath, tempFilePath, newPath, Path.GetDirectoryName(newPath), Path.GetFileName(newPath), launchArgs);
info.CreateNoWindow = true;
info.FileName = "cmd.exe";
Process.Start(info);
}
}
}
my XML Updater class
using System;
using System.Net;
using System.Xml;
namespace updater
{
public class UpdateXml
{
private Version version;
public Uri uri;
private string fileName;
private string md5;
private string description;
private string launchArgs;
internal Version Version {
get { return this.Version; }
}
internal Uri Uri {
get { return this.Uri; }
}
internal string FileName {
get { return this.fileName; }
}
internal string MD5 {
get { return this.md5; }
}
internal string Description {
get { return this.description; }
}
internal string LaunchArgs {
get { return this.launchArgs; }
}
internal UpdateXml(Version version, Uri uri, string fileName, string md5, string description, string launchArgs)
{
Console.WriteLine("run in1");
this.version = version;
this.uri = uri;
this.fileName = fileName;
this.md5 = md5;
this.description = description;
this.launchArgs = launchArgs;
Console.WriteLine("run out 1");
}
internal bool IsNewerThan(Version version)
{
Console.WriteLine("run in 2");
return this.version > version;
}
internal static bool ExistOnServer(Uri location)
{
try
{
Console.WriteLine("run in 3");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(location.AbsoluteUri);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Console.WriteLine("run out 3");
return res.StatusCode == HttpStatusCode.OK;
}
catch { return false; }
}
internal static UpdateXml Parse(Uri location, string appID)
{
Console.WriteLine("run in 4");
Version version = null;
string url = "", fileName = "", md5 = "", description = "", launchArgs = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(location.AbsoluteUri);
XmlNode node = doc.DocumentElement.SelectSingleNode("//update");
if(node == null)
{
return null;
}
version = Version.Parse(node["version"].InnerText);
url = node["url"].InnerText;
fileName = node["fileName"].InnerText;
md5 = node["md5"].InnerText;
description = node["description"].InnerText;
launchArgs = node["launchArgs"].InnerText;
Console.WriteLine("run out 4");
return new UpdateXml(version, new Uri(url), fileName, md5, description, launchArgs);
}
catch
{
return null;
}
}
}
}
My interfaces
using System;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
namespace updater
{
public interface Iupdater
{
string ApplicationName { get; }
string ApplicationID { get; }
Assembly ApplicationAssembly { get; }
Icon ApplicationIcon { get; }
Uri UpdateXmlLocation { get; }
Form Context { get; }
}
}
my update start form where it seems to go into a loop
using System;
using updater;
using System.Windows.Forms;
namespace updater
{
internal partial class updateInformation : Form
{
private Iupdater applicationInfo;
private UpdateXml updateInfo;
private UpdateInoForm updateInoForm;
public updateInformation(Iupdater applicationInfo, UpdateXml updateInfo)
{
InitializeComponent();
this.applicationInfo = applicationInfo;
this.updateInfo = updateInfo;
this.Text = this.applicationInfo.ApplicationName + " - Update in Process";
if (this.applicationInfo.ApplicationIcon != null)
this.Icon = this.applicationInfo.ApplicationIcon;
//this.lblNewVersion.Text = String.Format("New Version: {0}", this.updateInfo.Version.ToString());
Timer wait = new Timer();
wait.Interval = 5000;
wait.Tick += new EventHandler(wait_Tick);
wait.Start();
}
void wait_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Yes;
}
private void Details_Click(object sender, EventArgs e)
{
if (this.updateInfo == null)
this.updateInoForm = new UpdateInoForm(this.applicationInfo, this.updateInfo);
this.updateInoForm.ShowDialog(this);
}
}
}
You need to return the field values, instead of the properties returning the property value. Do not write properties like this:
/// THIS PROPERTY TRIES TO RETURN ITSELF!!
internal Version Version {
get {
return this.Version; // you wanted to return 'this.version'
}
}
You can use auto-properties like this:
// No more private fields
public class UpdateXml
{
public Version Version { get; set; }
public string FileName { get; } // Only expose a getter, can be set in the ctor
public Uri Uri { get; set; }
// add more properties here...
public UpdateXml(string filename)
{
FileName = filename;
}
}
Or learn to use a convention seen allot in C#. Prefix the private variable names:
public class UpdateXml
{
private Version _version;
private string _fileName;
private Uri _uri;
public Version Version => _version;
public string FileName => _filename;
public Uri Uri {
get => _uri;
set => _uri = value;
}
// add more properties here...
// use the ctor to set some field values
public UpdateXml(string filename)
{
_filename = filename;
}
// fields allow setting values later on
public void SetLatestVersion(Version v)
{
if (_version == null || v > _version)
_version = v;
}
}
All in all, take care when writing code. Case does matter ;-)
I have created a signalR hub which is solving purpose to show timer at UI of grouped person. If one person starts a play with another one, timer runs very nice.
But the problem is when another group start a new play and previous one is still in progress, I mean Timer is already running, then it overlaps the timer and hold one group. Timer runs once at a time. But I want it to have threading and run for every group.
here is my code:
ConnectionMapping.cs
public class ConnectionMapping<T>
{
private readonly Dictionary<T, HashSet<string>> _connections = new Dictionary<T, HashSet<string>>();
public int Count { get { return _connections.Count; } }
public void Add(T key, string connectionId)
{
lock (_connections)
{
HashSet<string> connections;
if (!_connections.TryGetValue(key, out connections))
{
connections = new HashSet<string>();
_connections.Add(key, connections);
}
lock (connections)
{
connections.Add(connectionId);
}
}
}
public IEnumerable<string> GetConnections(T key)
{
HashSet<string> connections;
if (_connections.TryGetValue(key, out connections))
{
return connections;
}
return Enumerable.Empty<string>();
}
public void Remove(T key, string connectionId)
{
lock (_connections)
{
HashSet<string> connections;
if (!_connections.TryGetValue(key, out connections))
{
return;
}
lock (connections)
{
connections.Remove(connectionId);
if (connections.Count == 0)
{
_connections.Remove(key);
}
}
}
}
}
Below is a Hub class:
public class TimeSyncingHub : Hub
{
private readonly static ConnectionMapping<int> _connections = new ConnectionMapping<int>();
private readonly PlayerPickTicker _playerPickTicker;
private readonly object _PickStateLock = new object();
private IUserBusiness userBusiness;
public TimeSyncingHub() :
this(PlayerPickTicker.Instance)
{
}
public TimeSyncingHub(PlayerPickTicker playerPickTicker)
{
_playerPickTicker = playerPickTicker;
}
public Task LeaveRoom(string roomName)
{
return Groups.Remove(Context.ConnectionId, roomName);
}
public async Task JoinRoom(string roomName)
{
await Groups.Add(Context.ConnectionId, roomName);
Clients.Group(roomName).JoinedRoom("Enter into group - " + roomName);
}
}
Another class where I have written time code is PlayerPickTicker class which is depends in same and implemented in constructor of hub class as shown in class above.
Below is PlayerPickTicker class:
public class PlayerPickTicker : IDisposable
{
private readonly static Lazy<PlayerPickTicker> _instance = new Lazy<PlayerPickTicker>(
() => new PlayerPickTicker(GlobalHost.ConnectionManager.GetHubContext<TimeSyncingHub>()));
private IHubContext _context;
public UserPlayerPickModel UserPlayerPick { get; set; }
public static PlayerPickTicker Instance { get { return _instance.Value; } }
private Timer TickTimer;
private Timer InitialTickTimer;
public int StartSeconds { get; set; }
public int StopSeconds { get; set; }
public bool IsTimerOver { get; set; }
private PlayerPickTicker(IHubContext context)
{
_context = context;
}
public void StartInitialTimer(Model.QueryModel queryModel)
{
try
{
MyDraftBusiness myDraft = new MyDraftBusiness();
myDraft.UdpatePlaysOneMinuteTimerPending(queryModel);
//lock (_InitialTimerStateLock)
//{
//StartSeconds = 60;
//if (InitialTickTimer != null)
//{
// InitialTickTimer.Dispose();
//}
States = new Dictionary<string, bool>() {
{ "IsInitialTimerOver", false},
{ "CallInitialTimerOverMethod", true},
};
PickPlayer = new Dictionary<string, long>() { { "LastPlayerPickId", 0 } };
//lock (States)
//{
StartSeconds = 60;
StopSeconds = 0;
IsInitialTimerOver = false;
CallInitialTimerOverMethod = true;
//}
InitialTickTimer = new Timer();// OnTimerElapsedInitialTimer, queryModel, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
InitialTickTimer.Interval = 1000;
InitialTickTimer.Enabled = true;
InitialTickTimer.Elapsed += (sender, e) => OnTimerElapsedInitialTimer(queryModel, e);
InitialTickTimer.AutoReset = false;
//}
//}
}
catch (Exception ex)
{
Helpers.ExceptionsLog.LogFile(" -( At StartInitialTimer Mehtod )- " + ex.Message);
}
}
private void OnTimerElapsedInitialTimer(object sender, ElapsedEventArgs e)
{
Model.QueryModel queryModel = (Model.QueryModel)sender;
try
{
//lock (_InitialtickStateLock)
//{
var states = (Dictionary<string, bool>)States;
var IsInitialTimerOver = states.FirstOrDefault(x => x.Key == "IsInitialTimerOver").Value;
//Clients.
if (StartSeconds <= StopSeconds && !IsInitialTimerOver)
{
if (InitialTickTimer != null)
{
InitialTickTimer.Dispose();
}
//IsInitialTimerOver = true;
states["IsInitialTimerOver"] = true;
//lock (States)
//{
//************** from client to server
var JsonResult = new JObject();
JsonResult.Add("data", JToken.FromObject(queryModel));
string GroupName = "Group" + queryModel.playId.ToString();
_context.Clients.Group(GroupName).initialTimerCompleted(JsonResult);
//_context.Clients.Group(GroupName, _context.Clients.All).initialTimerCompleted(JsonResult);
//_context.Clients.All.initialTimerCompleted(JsonResult);
MyDraftBusiness myDraft = new MyDraftBusiness();
try
{
myDraft.UdpatePlaysOneMinuteTimerRunning(queryModel);
DraftModel draftModel = myDraft.GetDraftById(queryModel);
if (draftModel != null)
{
var userPlayerPicks = myDraft.GetUserPlayerPickByPlayId(draftModel.PlayId);
if (draftModel.IsDraftFilled)
{
if (draftModel.Play != null && draftModel.Play.IsOneMinuteTimerPending == true)
{
myDraft.UdpatePlaysOneMinuteTimerPending(queryModel);
}
else
{
var activeUserPick = userPlayerPicks.Where(x => !x.Picked).FirstOrDefault();
if (activeUserPick != null)
{
StartTimer(activeUserPick);
}
}
}
}
}
catch (Exception ex)
{
Helpers.ExceptionsLog.LogFile(" -( At OnTimerElapsedInitialTimer Mehtod inner catch )- " + ex.Message);
var userPlayerPicks = myDraft.GetUserPlayerPickByPlayId(queryModel.playId);
var activeUserPick = userPlayerPicks.Where(x => !x.Picked).FirstOrDefault();
if (activeUserPick != null)
{
StartTimer(activeUserPick);
}
}
//}
}
else if (StartSeconds > 0)
{
//IsInitialTimerOver = false;
states["IsInitialTimerOver"] = false;
//lock (States)
//{
StartSeconds -= 1;
var GroupName = "Group" + Convert.ToString(queryModel.playId);
_context.Clients.Group(GroupName).intialTimerElapsed(StartSeconds);
_context.Clients.All.pickRunning(queryModel.playId);
InitialTickTimer.Stop();
InitialTickTimer.Start();
//StartInitialTimer(queryModel);
//}
}
//}
}
catch (Exception ex)
{
Helpers.ExceptionsLog.LogFile(" -( At OnTimerElapsedInitialTimer Mehtod )- " + ex.Message);
}
}
I know it's big code and hard to understand. But I hope for Any Good Developer here, Much appreciation for helper from core of my heart!
In my case, while calling API (GET Method), I should send the below query as part of the body.
URI : GET XXX/_search
Body:
{
"query":
{
"match_all":
{
}
}
}
As of now, I'm hard-coding the string and appending to the resource body.
hopefully below code snippet can help you.
Consumer:
JsonField jsn1 = new JsonField() { Name = "_id"};
jsn1.AddValue("1234");
JsonField jsn2 = new JsonField() { Name = "_name" };
jsn2.AddValue("CDE");
JsonField jsn0 = new JsonField(true) { Name = "match" };
JsonField jsn = new JsonField(true) { Name = "query" };
JsonConverter.MapField(jsn0, jsn1);
JsonConverter.MapField(jsn, jsn0);
JsonConverter jsonconverter = new JsonConverter();
jsonconverter.Add(jsn);
JsonField jsn3 = new JsonField() { Name = "name" };
jsn3.AddValue("temp");
jsonconverter.Add(jsn3);
Console.WriteLine(jsonconverter.GetJsonFormat());
Check logic below.
public class JsonConverter
{
LinkedList<JsonField> jsonlinking = new LinkedList<JsonField>();
public void Add(JsonField jsonfield)
{
LinkedListNode<JsonField> jsonelement = jsonlinking.Last;
if (jsonelement != null)
{
jsonelement.Value.SpecialChar = ",";
}
jsonlinking.AddLast(jsonfield);
}
public string GetJsonFormat()
{
jsonformat = string.Empty;
jsonformat += "{" + Environment.NewLine;
foreach (var item in jsonlinking)
{
ReadJson(item);
}
jsonformat += Environment.NewLine + "}" + Environment.NewLine;
return jsonformat;
}
public static void MapField(JsonField primary, JsonField relative)
{
primary.Next = relative;
}
public static void MapField(Queue<JsonField> linksource)
{
JsonField primary = null;
JsonField relative = null;
foreach (var item in linksource)
{
if (primary == null)
{
primary = item;
}
else if (relative == null)
{
relative = null;
}
else
{
JsonConverter.MapField(primary, relative);
primary = null;
relative = null;
}
}
}
private string jsonformat = string.Empty;
private void ReadJson(JsonField field)
{
if (field != null)
{
jsonformat += #"""";
jsonformat += field.Name;
jsonformat += #"""";
if (field.isContainer)
{
jsonformat += #":{" + Environment.NewLine;
int count = field.ChildNodes.Count();
if (count > 0)
{
foreach (var item in field.ChildNodes)
{
ReadJson(item);
}
}
jsonformat = (jsonformat.Substring(jsonformat.Length - 1, 1) == ",") ? jsonformat.Substring(0, jsonformat.Length - 1) : jsonformat;
jsonformat += #"}" + field.SpecialChar + Environment.NewLine;
}
else
{
jsonformat += #":";
jsonformat += field.GetValues();
}
}
}
}
public class JsonField
{
private int Size = 1;
private int CurrentIndex = 0;
public string Name { get; set; }
private string[] _value;
public string[] Value { get { return _value; } private set { _value = value; } }
public bool isContainer{get;set;}
public JsonField()
{
this.Value = new string[this.Size];
}
private Queue<JsonField> _next = new Queue<JsonField>();
public JsonField Next {
set
{
if (this._next.Count>0)
{
foreach (var item in this._next)
{
item.SpecialChar = ",";
}
}
this._next.Enqueue(value);
}
}
public IEnumerable<JsonField> ChildNodes { get { return this._next; } }
public JsonField(int valuesize)
{
this.Size = valuesize;
this.Value = new string[this.Size];
}
public JsonField(bool iscontainer)
{
this.isContainer = iscontainer;
}
public void AddValue(string value)
{
if (CurrentIndex >= this.Value.Length)
{
throw new ArgumentException("Index Out of Range over Value Array");
return;
}
this.Value[CurrentIndex] = value;
CurrentIndex++;
}
public void ClearValue()
{
this.Value = null;
this.Value = new string[this.Size];
}
public string GetValues()
{
if (this.Value.Length == 1)
{
return #"""" + this.Value[0] + #"""";
}
string returns=string.Empty;
for (int index = 0; index < this.Value.Length; index++)
{
returns += #"""" + this.Value[index] + #"""";
if ((index +1) < this.Value.Length)
{
returns += ",";
}
}
return "[" + returns+ "]";
}
public string SpecialChar { get; set; }
}
after a long search about reconnecting a client to a server (with a timer) I did find a way that works. well almost works.
Lets say I run 3 clients, when I shutdown the server and re-run it, only one client reconnect
seems like the problem is on the server's side, but can't figure it out.
here's my code (Base class only implements the INotifyPropertyChanged interface):
Client:
class RFClient : Base
{
public enum RFClientType { Sender, Receiver };
#region Properties
System.Threading.Timer _stTimer;
private int serverPort;
private string serverIP;
private string _name;
public string Name
{
get { return _name; }
set { _name = value; OnPropertyChanged("Name"); }
}
private string cType { get; set; }
private RFClientType _clientType;
public RFClientType ClientType
{
get { return _clientType; }
set
{
_clientType = value;
OnPropertyChanged("ClientType");
}
}
private TcpClient _tcpServer;
public TcpClient TcpServer
{
get { return _tcpServer; }
set { _tcpServer = value; OnPropertyChanged("TcpServer"); }
}
private string _msgRecieved;
public string MsgRecieved
{
get { return _msgRecieved; }
set { _msgRecieved = value; OnPropertyChanged("MsgRecieved"); }
}
private string _errorMSG;
public string ErrorMSG
{
get { return _errorMSG; }
set { _errorMSG = value; OnPropertyChanged("ErrorMSG"); }
}
#endregion
#region ctor
public RFClient(string IP, int Port, RFClientType clientType)
{
_name = Environment.UserName;
this._clientType = clientType;
if (_clientType == RFClientType.Receiver) { cType = "Receiver"; }
else if (_clientType == RFClientType.Sender) { cType = "Sender"; }
this._tcpServer = new TcpClient();
this.serverIP = IP;
this.serverPort = Port;
connectToserver(this._tcpServer, this.serverIP, this.serverPort);
}
#endregion
#region Methods
public void connectToserver(TcpClient tcpServer, string IP, int Port)
{
if (tcpServer != null)
{
try
{
_tcpServer.Connect(IP, Port);
SendMessage("onConnect");
ReadFromServer();
}
catch (Exception ex) { _errorMSG = ex.Message; }
}
}
public async void ReadFromServer()
{
string message;
if (_stTimer != null) { _stTimer.Dispose(); }
await Task.Run(() =>
{
while (true)
{
if (_tcpServer != null && !_tcpServer.Connected)
{
_stTimer = new System.Threading.Timer(Timer_Tick, null, 0, 15000);
break;
}
else
{
try
{
using (NetworkStream readStream = _tcpServer.GetStream())
{
byte[] bytes = new byte[256];
int i;
while ((i = readStream.Read(bytes, 0, bytes.Length)) != 0)
{
message = System.Text.Encoding.GetEncoding("Windows-1255").GetString(bytes, 0, i);
MsgRecieved = message;
}
}
}
catch (Exception ex) { _errorMSG = ex.Message; return; }
}
}
});
}
public async void SendMessage(string Message)
{
if (!_tcpServer.Connected)
{
_errorMSG = "No Connection";
return;
}
if (_tcpServer.Connected)
{
int msgSize;
await Task.Run(() =>
{
msgSize = Message.Length;
byte[] buffer = Encoding.GetEncoding("Windows-1255").GetBytes("$#1type" + cType + "$#2type" + "$#1name" + _name + "$#2name" + "$#1message" + Message + "$#2message");
NetworkStream serverStream = _tcpServer.GetStream();
serverStream.Write(buffer, 0, buffer.Length);
serverStream.Flush();
});
}
}
public void Close()
{
this._tcpServer.Close();
}
public void Timer_Tick(object sender)
{
_tcpServer.Close();
//_tcpServer = null;
_tcpServer = new TcpClient();
while (!_tcpServer.Connected)
{
try
{
connectToserver(_tcpServer, serverIP, serverPort);
break;
}
catch { ErrorMSG = "Cannot connect..."; }
}
}
#endregion
}
Server:
class RFServer : Base
{
#region Propertirs
private int myPort;
private TcpListener serverSocket;
public TcpListener ServerSocket
{
get { return serverSocket; }
set { serverSocket = value; OnPropertyChanged("ServerSocket"); }
}
private ObservableCollection<TcpClient> myclients;
public ObservableCollection<TcpClient> MyClients
{
get { return myclients; }
set { myclients = value; OnPropertyChanged("MyClients"); }
}
private string msgRecieved;
public string MsgRecieved
{
get { return msgRecieved; }
set { msgRecieved = value; OnPropertyChanged("MsgRecieved"); }
}
private string status;
public string Status
{
get { return status; }
set { status = value; OnPropertyChanged("Status"); }
}
private string errorStatus;
public string ErrorStatus
{
get { return errorStatus; }
set { errorStatus = value; OnPropertyChanged("ErrorStatus"); }
}
private string logPath = AppDomain.CurrentDomain.BaseDirectory + "serverRole.txt";
#endregion
#region ctor
public RFServer(IPAddress locallAddress, int Port)
{
this.myPort = Port;
try
{
if (!System.IO.File.Exists(logPath))
{
System.IO.File.Create(logPath).Close();
}
serverSocket = new TcpListener(locallAddress, this.myPort);
serverSocket.Start();
Status = "Server started";
writeLog(logPath, "Server started");
}
catch (Exception ex) { writeLog(logPath, ex.Message); ErrorStatus = ex.Message; }
}
#endregion
#region Methods
public async void ListenToClients()
{
await Task.Run(() =>
{
myclients = new ObservableCollection<TcpClient>();
TcpClient tcpClient = new TcpClient();
while (true)
{
try
{
tcpClient = serverSocket.AcceptTcpClient();
ReadClients(tcpClient);
}
catch (Exception ex) { tcpClient.Close(); writeLog(logPath, string.Format(ex.Message)); ErrorStatus = ex.Message; }
}
});
}
public async void ReadClients(TcpClient tcpClient)
{
string fullDataReceived, clientMessage, clientType, logStr;
string clientName = string.Empty;
await Task.Run(() =>
{
while (tcpClient.Connected)
{
try
{
NetworkStream networkStream = tcpClient.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)tcpClient.ReceiveBufferSize);
fullDataReceived = Encoding.GetEncoding("Windows-1255").GetString(bytesFrom);
clientMessage = GetSubstringByString("$#1message", "$#2message", fullDataReceived);
clientType = GetSubstringByString("$#1type", "$#2type", fullDataReceived);
clientName = GetSubstringByString("$#1name", "$#2name", fullDataReceived);
if (clientMessage == "onConnect")
{
logStr = string.Format("Client connected -- Name: {0}, Type: {1}, Message: {2}", clientName, clientType, clientMessage);
writeLog(logPath, logStr);
if (clientType == "Receiver")
{
myclients.Add(tcpClient);
}
networkStream.Flush();
}
else
{
logStr = string.Format("New message from {0}: {1}", clientName, clientMessage);
writeLog(logPath, logStr);
MsgRecieved = string.Format("{0}: {1}", clientName, clientMessage);
SendDataToClients(MsgRecieved);
}
}
catch (Exception ex)
{
if (string.IsNullOrWhiteSpace(clientName)) { clientName = "UnKnown"; }
ErrorStatus += ex.Message + Environment.NewLine;
tcpClient.Close();
writeLog(logPath, string.Format("cannot read from client: {0}{2}Error: {1}",clientName,ex.Message,Environment.NewLine));
}
}
});
}
public async void SendDataToClients(string message)
{
if (myclients.Count > 0)
{
byte[] buffer = Encoding.GetEncoding("Windows-1255").GetBytes(message);
NetworkStream clientStream;
string logStr;
for (int i = 0; i <= myclients.Count - 1; i++)
{
await Task.Run(() =>
{
if (((TcpClient)myclients[i]) != null && ((TcpClient)myclients[i]).Connected)
{
try
{
clientStream = ((TcpClient)myclients[i]).GetStream();
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
logStr = string.Format("Message Sent to {0} Clients. ({1})", myclients.Count, message);
writeLog(logPath, logStr);
Status += "Message Sent to " + myclients.Count + " Clients." + Environment.NewLine;
}
catch
{
((TcpClient)myclients[i]).Close();
myclients.RemoveAt(i);
writeLog(logPath, "client disconnected...");
ErrorStatus = "client disconnected..." + Environment.NewLine;
}
}
else
{
((TcpClient)myclients[i]).Close();
myclients.RemoveAt(i);
writeLog(logPath, "client disconnected...");
ErrorStatus += "client disconnected..." + Environment.NewLine;
}
});
}
}
}
/// <summary>
/// Trim a string between 2 delimiters (strings).
/// </summary>
/// <param name="startString">1st delimiter</param>
/// <param name="endString">2nd delimiter</param>
/// <param name="fullString">Full string</param>
/// <returns></returns>
public string GetSubstringByString(string startString, string endString, string fullString)
{
return fullString.Substring((fullString.IndexOf(startString) + startString.Length), (fullString.IndexOf(endString) - fullString.IndexOf(startString) - startString.Length));
}
public void writeLog(string filePath, string Logcontent)
{
File.AppendAllText(filePath, DateTime.Now + " :::: " + Logcontent + Environment.NewLine);
}
public void CloseServer()
{
writeLog(logPath, "Server Stopped");
this.serverSocket.Stop();
this.myclients.Clear();
}
#endregion
}
ReadClients() is working on a 3rd Thread, I think it should be on the same Thread as ListenToClients().
I have two task which work as producer and consumer, but they don't run in parallel. Second one waits for the first one to compelete. Can you explain why and how can I correct this? I want both of them run at the same time. (I try many varians, but all of them don't work well)
class DirectoryReader
{
private readonly string _dir ;
private Processor[] _processors;
private string[] _files;
private readonly Regex _rx = new Regex(#"([^.\\]+)\.cs");
private Queue<Processor> queue = new Queue<Processor>();
private bool isOff;
private AutoResetEvent isAvailable = new AutoResetEvent(false);
private StreamWriter log = new StreamWriter("Log.txt");
public DirectoryReader(string dir)
{
_dir = dir;
}
public Container[] ProcessAllFiles()
{
_files = Directory.GetFiles(_dir, "*.cs", SearchOption.AllDirectories);
_processors = new Processor[_files.Length];
var thread = Task.Run(() => Compute());
var thread2 = Task.Run(() => Read());
thread.Wait();
}
public void Read()
{
for (var i = 0; i < _files.Length; i++)
{
try
{
var matches = _rx.Matches(_files[i]);
foreach (var match in matches)
{
//Console.WriteLine(match);
lock (log)
{
log.WriteLine(match);
}
}
_processors[i] = new Processor(matches[matches.Count - 1].ToString(), File.ReadAllText(_files[i]));
lock (queue)
{
queue.Enqueue(_processors[i]);
isAvailable.Set();
}
}
catch (IOException ex)
{
Console.WriteLine(ex.Message + _files[i]);
}
}
isOff = true;
}
public void Compute()
{
Processor proc = null;
int ccount = 0;
while (true)
{
lock (queue)
{
if ((ccount = queue.Count) > 0)
{
proc = queue.Dequeue();
}
}
if (ccount == 0)
{
if (isOff)
{
return;
}
else
{
isAvailable.WaitOne();
}
}
if (proc != null)
{
//Some calculations on proc
lock (log)
{
log.WriteLine("+++++" + proc.FileName);
}
}
}
}
}
UPD1: I rewrite this code with using BlockingCollection, but is still doesn't work correct
class DirectoryReader
{
private readonly string _dir ;
private Processor[] _processors;
private string[] _files;
private readonly Regex _rx = new Regex(#"([^.\\]+)\.cs");
private List<Container> answer = new List<Container>();
BlockingCollection<FileContainer> dataItems = new BlockingCollection<FileContainer>();
public DirectoryReader(string dir)
{
_dir = dir;
}
public void ProcessAllFiles()
{
_files = Directory.GetFiles(_dir, "*.cs", SearchOption.AllDirectories);
_processors = new Processor[_files.Length];
var task = Task.Factory.StartNew(Compute);
var task2 = Task.Factory.StartNew(Read);
Task.WaitAll(task, task2);
}
public void Read()
{
for (var i = 0; i < _files.Length; i++)
{
try
{
var matches = _rx.Matches(_files[i]);
dataItems.Add(new FileContainer{
Name = matches[matches.Count - 1].ToString(),
Content = File.ReadAllText(_files[i])});
}
catch (IOException ex)
{
Console.WriteLine(ex.Message + _files[i]);
}
}
dataItems.CompleteAdding();
}
public void Compute()
{
FileContainer proc = null;
while (!dataItems.IsCompleted)
{
try
{
proc = dataItems.Take();
}
catch (InvalidOperationException) { }
if (proc != null)
{
//Some calculations
}
}
}
}