Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have done some data moving from a DB to Another DB. I did in a Application and
put it in windows scheduler. It will be get executed each 2 mins.

when the system start execute the exe , already that application in execution Current execution has to get destroyed. we have to restrict running the application untill previous process gets over.

how to do this???


Thanks ,
Pal.
Posted
Comments
[no name] 13-Aug-12 8:23am    
Sounds like a poor design. Either make a single instance app or sleep until the previous instance exits.

do something thing like with Monitor.Enter,

C#
Monitor.Enter((object)dgDevices);
           try
           {
               Dg_Devices.Rows[RowIndex].Cells[ColumnName].Value = (object)DBNull.Value;
               Dg_Devices.Rows[RowIndex].Cells[ColumnName].Value = (object)Cellvalue;
               Dg_Devices.Refresh();
           }
           finally
           {
               Monitor.Exit((object)dgDevices);
           }




otherwise

Refer this link
http://www.yoda.arachsys.com/csharp/threads/deadlocks.shtml[^]

i hope this will help you

regards
sarva
 
Share this answer
 
If I understood correctly, you want to limit the execution of the application to one single instance.
1) If you want to prevent running a second one you can use this:

C#
static void Main()
  {
     string mutex_id = "__SincleInstanceApp";
     using (NamedMutex mutex = new NamedMutex(false, mutex_id))
     {
        if (!mutex.WaitOne(0, false))
        {           
           // Allready running
           return;
        }
        // Do stuff
     }
  }


See also: Single Instance Application in C#[^]
2) If you want to end the already running instance (might be the wrong approach), you have to search for the other instance, and kill[^] it like this:

C#
Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName (current.ProcessName);

    foreach (Process process in processes)
    {
        if (process.Id != current.Id)
        {
            process.Kill();
        }
    }

Please note, that simply killing an other process hold a lot of possible issues, it should be used only if it is not responding. Better trying to Close it with process.CloseMainWindow(), wait a while and only than killing it. But you also could communicate with your other application instance using an other mutex for example.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900