Only one program at a time - c#

I have a situation where I need to have only one instance of a program running at the same time.
This would be trivial like this:
class OneAtATimePlease
{
static void Main()
{
// Naming a Mutex makes it available computer-wide. Use a name that's
// unique to your company and application (e.g., include your URL).
using (var mutex = new Mutex (false, "oreilly.com OneAtATimeDemo"))
{
// Wait a few seconds if contended, in case another instance
// of the program is still in the process of shutting down.
if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))
{
Console.WriteLine ("Another app instance is running. Bye!");
return;
}
RunProgram();
}
}
static void RunProgram()
{
Console.WriteLine ("Running. Press Enter to exit");
Console.ReadLine();
}
}
Except for the small detail, that I need the EXISTING process to terminate, not the new one.
I tried making a semaphore the existing process could listen to after grabbing the above mutex, but because I want it to wait for the semaphore then I can end up in a situation where the semaphore is always signaled and thus it doesn't work.
Anyone have a good idea on how to solve this problem?

You need inter-process communication, to send a signal to the existing application. For C#, see IPC Mechanisms in C# - Usage and Best Practices.

Cine, I've written a two-Mutex logic. The first one is the "execution lock", while the second is the "monitor lock".
When the first process can't acquire the monitor lock, it will exit and release the execution lock for the new process.
I'm not sure if this is the best solution, and any feedback will be welcome.
C#:
class Program
{
private static string processName = DateTime.Now.ToString("hh.mm.ss");
private static bool exitProcess;
private static Mutex firstLock
{
get
{
return new Mutex(false, "stackoverflow.com/questions/11304052/");
}
}
private static Mutex secondLock
{
get
{
return new Mutex(false, "stackoverflow.com/questions/11304052/ #2");
}
}
static void Main(string[] args)
{
Console.WriteLine(string.Format("Process {0} starting", processName));
exitProcess = false;
while (true)
{
using (firstLock)
{
Console.WriteLine(string.Format("Process {0} trying to get #1 mutex", processName));
if (!firstLock.WaitOne(TimeSpan.FromSeconds(1), false))
{
Console.WriteLine(string.Format("Process {0} #1 mutex in use, waiting for release", processName));
bool killFirstApp = false;
while (!killFirstApp)
{
killFirstApp = LockSecondMutex();
}
continue;
}
new Thread(MonitorSecondMutex).Start();
RunProgram();
firstLock.ReleaseMutex();
break;
}
}
}
static void RunProgram()
{
while (!exitProcess)
{
Console.WriteLine(string.Format("Process {0} running", processName));
Thread.Sleep(1000);
}
}
static void MonitorSecondMutex()
{
while (true)
{
using (secondLock)
{
if (!secondLock.WaitOne(TimeSpan.FromSeconds(2), false))
{
Console.WriteLine(string.Format("Process {0} lost second mutex. Will now exit.", processName));
exitProcess = true;
break;
}
secondLock.ReleaseMutex();
}
Thread.Sleep(500);
}
}
static bool LockSecondMutex()
{
while (true)
{
using (secondLock)
{
if (!secondLock.WaitOne(TimeSpan.FromSeconds(2), false))
{
continue;
}
Thread.Sleep(5000);
secondLock.ReleaseMutex();
}
return true;
}
}
}

You could possibly get this done by requesting access to some limited system-wide resource, such as port. Your application could bind a socket to a specific port on launch. If it fails to bind, send a termination signal to the running instance and try again.

Related

Start the same program many times but only one should work

Is it somehow possible that if I start my program like 10 times fast in a row, but only one at a time should do something. The other keep waiting that the working program is finished or stopped.
So in the end, if I open my program 10 times, all 10 programs should be working in a row, not simultaneously.
Is this possible in c#?
You can use a named EventWaitHandle to do this, for example:
using System;
using System.Threading;
namespace Demo
{
static class Program
{
static void Main()
{
using (var waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, "MyHandleName"))
{
Console.WriteLine("Waiting for handle");
waitHandle.WaitOne();
try
{
// Body of program goes here.
Console.WriteLine("Waited for handle; press RETURN to exit program.");
Console.ReadLine();
}
finally
{
waitHandle.Set();
}
Console.WriteLine("Exiting program");
}
}
}
}
Try running a few instances of this console app and watch the output.
You can use system wide Mutex or system wide Semaphore. If you create Mutex or Semaphore with name it become visible for whole system - in other words it can be visible from other processes.
Mutex syncMutex = new Mutex(false, "NAME OF MUTEX");
try
{
if(!syncMutex.WaitOne(MUTEX_TIMEOUT))
{
//fail to get mutex
return;
}
//mutex obtained do something....
}
catch(Exception ex)
{
//handle error
}
finally
{
//release mutex
syncMutex.ReleaseMutex();
}

execute a C# method asynchronously using Threading in windows service

i have a windows service that runs every 10 seconds to execute the read method. The Read method connects to remote server with the connection url provided in the constructor.
if the remote server fails to respond, it throws error and goes to catch. How do we make the thread to start again?
class PMTicketsService
{
private Timer _serviceTimer;
private TimerCallback _timerDelegate;
public PMTicketsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// Set the method to execute when the timer executes.
_timerDelegate = new TimerCallback(Receive);
// Create timer and attach our method delegate to it
_serviceTimer = new Timer(_timerDelegate, null, 1000, 10000);
}
public void Receive(object state)
{
ABC abc = new ABC(Url);
ABC abc1 = new ABC(Url1);
/* Create the thread object, passing in the abc.Read() method
via a ThreadStart delegate. This does not start the thread. */
Thread oThread = new Thread(new ThreadStart(abc.Read());
Thread oThread1 = new Thread(new ThreadStart(abc1.Read());
// Start the thread
oThread.Start();
oThread1.Start();
oThread.Join();
oThread1.Join();
}
}
class ABC
{
public string strUrl;
public ABC(string url)
{
strUrl = url;
}
public void Read()
{
try
{
// Code to use the connectionurl to access a remote server
}
catch (Exception ex)
{
// If the connection url fails to respond, how do we make thread start again?
}
}
}
In the future, you should submit sample code that actually compiles. I took what you had and cleaned it up, removed the unnecessary timer and structured it in a way that should give you what you need. In the code below, your Read method will continue running until you set done to true.
protected override void OnStart(string[] args)
{
try
{
ABC abc = new ABC("www.abc.com");
// Create the thread object, passing in the abc.Read() method
Thread oThread = new Thread(new ThreadStart(abc.Read));
// Start the thread
oThread.Start();
}
catch (Exception)
{
}
}
public class ABC
{
string strUrl = "";
public ABC(string url)
{
strUrl = url;
}
public void Read()
{
bool done = false;
while (!done)
{
try
{
//Code to use the connectionurl to access a remote server
//Use strUrl in here
}
catch (Exception)
{
//Wait 10 seconds before trying again
Thread.Sleep(10000);
}
//On success, set done to true
done = true;
}
}
}
Why do you want to start another thread? Starting/stopping threads is an expensive operation, you're far better off just keeping the existing thread open and continually trying to connect (possibly with a sleep in between). You already have the try/catch to keep the thread from crashing. Just wrap the try/catch in a while(!done) and set done to be true once you successfully connect.
You might also want to add some code so that if you can't connect X times in a row (maybe 5?) then you'll stop trying, or increase the timeout between connection attempts.

C# Making a Thread wait for a Timer

I'm writing a C# program that runs two IRC connections at once.
The connections are threaded, and each thread starts like so:
MainThread = new Thread(new ThreadStart(StartMainProcessor));
MainThread.IsBackground = false;
MainThread.Start();
private void StartMainProcessor() {
MainProcessor.Bot.Connect();
//while (true) { }
}
Bot.Connect() looks like this (somewhat abridged version):
public void Connect() {
try {
Client.Connect(IRCHelper.SERVER, IRCHelper.PORT);
}
catch (CouldNotConnectException e) {
Reconnect(true);
return;
}
try {
Client.Listen();
}
catch (Exception e) {
Reconnect(false);
return;
}
}
This works fine until the bot disconnects (which will always happen eventually, it's the nature of IRC).
When it disconnects, Reconnect() is called, which starts a timer. When that timer expires the bot is meant to then call Connect() again. The reason for the timer is that an IRC server will refuse an immediate reconnection sometimes.
However, once the Connect() method has ended, the Thread ends, and the program (console application) exits. (Client.Listen() is blocking)
I had previously overcome this problem by adding while (true) { } in StartMainProcessor()... But this eats up 100% CPU, and I'd really prefer a different solution.
Thank you for your help. :)
Sounds like you need a signaling construct. For example, you could use something like an AutoResetEvent to block the thread calling Reconnect, i.e. call Reconnect, start the timer and then block the thread. Then set the auto reset event in the timer expired event handler to allow the thread to continue (unblock) and call Connect.
I'm not a fan of spinning the processor - wastes huge amounts of CPU resources when you add infinite loops or sleeps in loops.
Why don't you just Thread.Sleep inside Bot.Reconnect? That would keep your thread alive and wake it up when ready to call Bot.Connect again.
You might want to try something like that
private bool canExitThread;
private void StartMainProcessor()
{
while (canExitThread)
{
//do the magic here
System.Threading.Thread.Sleep(1); //make sure you allow thread to do the job, otherwise you will get 100 cpu usage
//do the connecting, disconnecting, listening
}
}
Also can you check if Client is connected? if so then you should be checking that within the main loop and if it's disconnected - call the connect method.
Hope that gives you an idea how to do it.
Also have a look the the article below, which might explain things a little bit more.
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
how about something like this
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting server..");
foreach (var connection in new[] {new Connection(TimeSpan.FromSeconds(1)), new Connection(TimeSpan.FromSeconds(1))})
ThreadPool.QueueUserWorkItem(connection.Connect);
Console.WriteLine("Server running. Press Enter to quit.");
Console.ReadLine();
}
}
public class Connection //might be good to implement IDisposable and disconnect on Dipose()
{
private readonly TimeSpan _reConnectionPause;
public Connection(TimeSpan reConnectionPause)
{
_reConnectionPause = reConnectionPause;
}
//You probably need a Disconnect too
public void Connect(object state)
{
try
{
//for testing assume connection success Client.Connect(IRCHelper.SERVER, IRCHelper.PORT);
Debug.WriteLine("Open Connection");
}
catch (Exception)
{
//You might want a retry limit here
Connect(state);
}
try
{
//Client.Listen();
//Simulate sesison lifetime
Thread.Sleep(1000);
throw new Exception();
}
catch (Exception)
{
Debug.WriteLine("Session end");
Thread.Sleep(_reConnectionPause);
Connect(state);
}
}
}
}
I presume you have a Main method, so why don't we start there:
private static readonly MAX_NUM_BOTS = 2;
static void Main(string[] args)
{
List<Thread> ircBotThreads = new List<Thread>();
for(int numBots = 0; numBots < MAX_NUM_BOTS; numButs++)
{
Thread t = new Thread(()=>{StartMainProcessor();});
t.IsBackground = false;
t.Start();
ircBotThreads.Add(t);
}
// Block until all of your threads are done
foreach(Thread t in ircBotThreads)
{
t.Join();
}
Console.WriteLine("Goodbye!");
}
private static void StartMainProcessor()
{
MainProcessor.Bot.Connect();
}
Then you can do something like this:
// 30 second time out (or whatever you want)
private static readonly TimeSpan TIMEOUT = TimeSpan.FromSeconds(30.0);
// specify the maximum number of connection attempts
private static readonly int MAX_RECONNECTS = 10;
public void Connect()
{
bool shouldListen = false;
// This is your connect and re-connect loop
for(int i = 0; i < MAX_RECONNECTS; i++)
{
try
{
Client.Connect(IRCHelper.SERVER, IRCHelper.PORT);
shouldListen = true;
}
catch (CouldNotConnectException e)
{
// It's OK to sleep here, because you know exactly
// how long you need to wait before you try and
// reconnect
Thread.Sleep((long)TIMEOUT.TotalMilliseconds);
shouldListen = false;
}
}
while(shouldListen)
{
try
{
Client.Listen();
}
catch (Exception e)
{
// Handle the exception
}
}
}
This is a very rough draft, but the concept is that you keep trying to reconnect until you fail. Once you connect, then you listen (I presume you listen for something in IRC) and you process the data until you decide that you no longer need to be doing that work.

How to sync between a listening/sending tcp client thread and the main execution?

i have a simple windows service which runs and starts a thread which listen/receive heartbeat via tcp/ip. i'm having a hard time finding ways to sync between getting information from the tcp thread and using that value to update something in the main thread.
i try to use a thread.sleep method and keep on looping it for a few times while awaiting the answer back from the thread and then getting the value, but that method seems to be a bit volatile with the method sometimes working and sometimes not.
so what's a good way to sync between these two?
basically what i want to do is to start the listening tcp thread, get specific value and the update the main program.
attached are the receive function and the function which i used to start the thread.
p.s: i'm a totally noobie when it comes to tcp/ip and c# so any comments on any part of the code or the design is more than welcome :)
public virtual void Receive()
{
string eventMessage = string.Empty;
int bytesRcvd = 0;
int totalBytesRcvd = 0;
byte[] byteBuffer = new byte[maxBufferSize];
NetworkStream listenStream;
try
{
if (client.Connected)
{
listenStream = client.GetStream();
}
else
{
return;
}
while (true)
{
//message that is slot in from the object will get sent here.
if (!string.IsNullOrEmpty(MessageToSend))
{
Send(MessageToSend);
MessageToSend = string.Empty;
}
// must convert it back and look for the delimiter, cannot wait for the three heartbeat to pass
string leftoverMsg = string.Empty;
bytesRcvd = listenStream.Read(byteBuffer, totalBytesRcvd, maxBufferSize - totalBytesRcvd);
totalBytesRcvd += bytesRcvd;
//if more than heart beat size, can process to see if it's a heartbeat and proceed to send
if (totalBytesRcvd > msgHeartbeatSize)
{
eventMessage = Encoding.ASCII.GetString(byteBuffer, 0, totalBytesRcvd);
ProcessMessage(eventMessage, ref leftoverMsg, ref totalBytesRcvd, ref byteBuffer);
}
}
}
catch (ThreadAbortException thEx)
{
//do nothing as main thread has aborted and waiting to close
logger.Info(Thread.CurrentThread.Name + " is stopped. ");
}
catch (Exception exce)
{
bIsActive = false;
logger.Error(exce);
CleanUp();
}
finally
{
logger.Info(String.Format("Thread {0} Exiting. ", Thread.CurrentThread.Name));
}
}
public virtual void StartReceivingThread()
{
Thread thrReceive = new Thread(Receive);
try
{
if (!bIsActive && Connect())
{
//NOTE: exception thrown by a thread can only be captured by that thread itself
//start a listen thread
//wait until heartbeat message is accepted
thrReceive.Name = "thr" + serviceType.Name;
thrReceive.Start();
bIsActive = true;
//wait to get the heartbeat message
for (int i = 0; i < maxRetry; i++)
{
Thread.Sleep(maxTimeOutValue);
if (bIsReceivingHeartbeat)
break;
}
//if nothing happens close the connection and try again
if (!bIsReceivingHeartbeat)
{
bIsActive = false;
CleanUp();
logger.Info("Closing receiver thread - " + thrReceive.Name);
}
else
{
logger.Info("Starting receiver thread - " + thrReceive.Name);
}
}
}
catch(Exception ex)
{
logger.Error(ex);
}
//finally
//{
// logger.Info("Exiting receiver thread - " + thrReceive.Name);
//}
}
I assume bIsReceivingHeartbeat is a bool member variable of the class. If the value changed in one thread (receiver) is not visible in the other thread this is most likely due to memory barrier. I am saying this from my Java background but this is most likely true in .net as well.
Try declaring the variables volatile or use a property and make the getter and setter synchronized:
private bool bIsReceivingHeartbeat;
public bool IsReceivingHeartbeat
{
[MethodImpl(MethodImplOptions.Synchronized)]
get { return bIsReceivingHeartbeat; }
[MethodImpl(MethodImplOptions.Synchronized)]
set { bIsReceivingHeartbeat = value; }
}
And in the calling code:
if (!IsReceivingHeartbeat) ....
I am writing from Java background but the situation most likely similar
(Looks like you also posted this code in refactormycode.com.)
Anyway, instead of the loop with a sleep delay, I recommend using an Event object that pulsed by the code that sets IsReceivingHeartbeat. See the ManualResetEvent and AutoResetEvent classes in MSDN.

Synchronisation of Two Threads

I have two threads in c#.. Now i need to wait for a particular statement to be executed before I can continue execution in the other thread which obviously is a case of synchronisation.
Is there any code that can carry this out as in using an in-built method?
This is the code example:
public void StartAccept()
{
try
{
newSock.BeginAccept(new AsyncCallback(Accepted), newSock);
}
catch (ArgumentException)
{
MessageBox.Show("Error in arguments while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (ObjectDisposedException)
{
MessageBox.Show("socket closed while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (SocketException)
{
MessageBox.Show("Error accessing socket while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (InvalidOperationException)
{
MessageBox.Show("Invalid operation while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (Exception)
{
MessageBox.Show("Exception occurred while using begin-accept", "Error", MessageBoxButtons.OK);
}
}
This receives data from the desired host which is selected by the code:
private void listBox1_Click(object sender, EventArgs e)
{
String data = (String)this.listBox1.SelectedItem;
ip = Dns.GetHostAddresses(data);
clientIP = new IPEndPoint(ip[0], 5555);
newSock.Bind(clientIP);
newSock.Listen(100);
}
So in order to start receiving data I need to initialise the socket to the particular remote host which is done when i click on one of the hosts shown in the listbox.
For this I need the synchronization.
Take a look at AutoResetEvent and ManualResetEvent. They are signals that makes synchronisation between threads possible.
The first thread that needs to wait for something to get done will do myEvent.WaitOne(), which blocks until the other thread calls myEvent.Set().
Let's say we have two threads, where one of them needs to do some kind of initialisation before the other thread can continue. You then share a AutoResetEvent between the two, let's call it myEvent.
// Signal example
using System;
using System.Threading;
class MySync
{
private readonly AutoResetEvent _myEvent;
public MySync(AutoResetEvent myEvent)
{
_myEvent = myEvent;
}
public void ThreadMain(object state)
{
Console.WriteLine("Starting thread MySync");
_myEvent.WaitOne();
Console.WriteLine("Finishing thread MySync");
}
}
class Program
{
static void Main(string[] args)
{
AutoResetEvent myEvent = new AutoResetEvent(false);
MySync mySync = new MySync(myEvent);
ThreadPool.QueueUserWorkItem(mySync.ThreadMain);
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
myEvent.Set();
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
Console.WriteLine("Finishing");
}
}
Don't confuse this with a shared resource where the access order doesn't matter. For example, if you have a shared list or a shared dictionary you need to wrap it in a mutex in order to guarantee that they execute correctly.
// Mutex example
object mySync = new object();
Dictionary<int, int> myDict = new Dictionary<int, int>();
void threadMainA()
{
lock(mySync)
{
mySync[foo] = bar;
}
}
void threadMainB()
{
lock(mySync)
{
mySync[bar] = foo;
}
}
You can use an AutoResetEvent.
In the following example two methods get called by different threads and DoSomethingA() will be executed and finish before DoSomethingB() starts:
AutoResetEvent resetEvent = new AutoResetEvent(false);
void ThreadWorkerA()
{
// perform some work
DoSomethingA();
// signal the other thread
resetEvent.Set();
}
void ThreadWorkerB()
{
// wait for the signal
resetEvent.WaitOne();
// perform the new work
DoSomethingB();
}
Note: remember to dispose the AutoResetEvent :)
Java has something called Join. I suspect there will be a predefined method in C# too.

Categories

Resources