-6

I have a C# winform application which needs to run multiple instance in synchronous way. The goal is to:

If the exe runs 3 times, it runs the first instance of the exe and the rest will wait until the first instance finishes the processing. Then, a next waiting exe intance will run and process and so on.

The exe will run one by one until it finish processing then the exe will terminates automatically af.

Any idea how to do this?

I already tried below:

    private void CheckInstance()
            {
                bool _returnValue = true;
                string _lockFile = string.Empty;
                Random _rnd = new Random();

                int _randomValue = _rnd.Next(100, 200);
                int _rndmiliSec = 0;

                _rndmiliSec = DateTime.Now.Millisecond * _rnd.Next(2, 6);

                _lockFile = string.Concat(AppDomain.CurrentDomain.BaseDirectory, string.Format("/{0}", instanceFileName));

                while (_returnValue)
                {
                    _returnValue = File.Exists(_lockFile);

                    if (_returnValue)
                    {
                        Thread.Sleep(1000);
                        this.Hide();
                    }
                    else
                    {
                        try
                        {
                            Thread.Sleep((_rnd.Next(1000) + _rndmiliSec) + _rnd.Next(1000, 1500));
                            Functions.WriteLog(_lockFile, "Starting the process...");
                            Functions.WriteLog(_lockFile, string.Format("Start Time : {0}", paramPrintTime));
                            File.SetAttributes(_lockFile, FileAttributes.ReadOnly);
                            this.Show();
                            break;
                        }
                        catch (Exception)
                        {
                            _returnValue = false;
                        }
                    }
                }
            }

            private void DeleteInstance()
            {
                try
                {
                    File.SetAttributes(string.Concat(AppDomain.CurrentDomain.BaseDirectory, string.Format("/{0}", instanceFileName)), FileAttributes.Normal);
                    File.Delete(string.Concat(AppDomain.CurrentDomain.BaseDirectory, string.Format("/{0}", instanceFileName)));
                }
                catch (Exception)
                {

                }

            }

private void Form_Shown(Object sender, EventArgs e)
        {
            _backWorker.RunWorkerAsync();
        }
private void FormClosed(object sender, FormClosedEventArgs e)
        {
            DeleteInstance();
        }

        private void Form_Load(object sender, System.EventArgs e)
        {
            CheckInstance();
        }

        BackgroundWorker _backWorker = new BackgroundWorker();
public Form()
        {
            InitializeComponent();
            _backWorker.WorkerReportsProgress = true;
            _backWorker.ProgressChanged += _backWorker_ProgressChanged;
            _backWorker.RunWorkerCompleted += _backWorker_RunWorkerCompleted;
            _backWorker.DoWork += _backWorker_DoWork;
        }
 private void _backWorker_DoWork(object sender, DoWorkEventArgs e)
        {
Do some work processing...
}
  private void _backWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }
  private void _backWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pg.Value = e.ProgressPercentage;
            lblIndicator.Text = e.UserState.ToString();
        }

When the exe run 3 instance, the first instance will run while 2nd and third hides for a while awaiting the 1st instance to be finisih. However, after the 1st instance finish the process, The 2nd and 3rd instance are running simultaneously.

Any Ideas? Thanks.

jeff
  • 448
  • 1
  • 15
  • 34
  • 3
    What have you tried yourself? What didn't work for you and do you have any understanding why? This is a Q&A website, not just a source for getting copy, paste solutions. – Gerald Versluis Oct 07 '13 at 15:00
  • What exe are you running ? a console app ? – Kurubaran Oct 07 '13 at 15:50
  • I'm sorry guys I forgot to post my code on what I already done. Please see the edited one. Thanks. – jeff Oct 08 '13 at 00:51
  • possible duplicate of [Reliably detecting that another of my applications is running](http://stackoverflow.com/questions/975250/reliably-detecting-that-another-of-my-applications-is-running) – Mitch Mar 01 '14 at 17:34

1 Answers1

-2

Maybe this can work:

public static bool IsProgramRunning(string TitleOfYourForm)
{
    bool result = false;
    Process[] processes = Process.GetProcesses();
    foreach (Process p in processes)
    {
         if (p.MainWindowTitle.Contains(TitleOfYourForm))
         {
             result = true;
             break;
         }
    }
    return result;
}

Call this function in the Main function(before opening the mainForm), if it is false Application.Exit() else show your form..

General Grievance
  • 4,555
  • 31
  • 31
  • 45
r.mirzojonov
  • 1,209
  • 10
  • 18
  • Thanks but this one doesn't work at all, the forms are running simultaneously it should be one by one. – jeff Oct 08 '13 at 00:51