kvnsdr
asked on
Threading Process Has Multiple Openings?
I have a thread that starts a backup.bat process.
Problem: Multiple .bat are opened instead of just ONE.
Help............ here's the code...........
private void startBackupThread()
{
if ((thread1 == null) || ((thread1.ThreadState & (System.Threading.ThreadSt ate.Unstar ted | System.Threading.ThreadSta te.Stopped )) != 0))
{
thread1 = new Thread(new ThreadStart(backupBat));
thread1.IsBackground = true;
thread1.Priority = ThreadPriority.Normal;
thread1.Start();
}
}
private void backupBat()
{
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt ring();
proc.Start();
}
Problem: Multiple .bat are opened instead of just ONE.
Help............ here's the code...........
private void startBackupThread()
{
if ((thread1 == null) || ((thread1.ThreadState & (System.Threading.ThreadSt
{
thread1 = new Thread(new ThreadStart(backupBat));
thread1.IsBackground = true;
thread1.Priority = ThreadPriority.Normal;
thread1.Start();
}
}
private void backupBat()
{
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt
proc.Start();
}
can you use a static boolean variable or some other flag (in a DB or whatever) to indicate that the backup batch process has already been kicked off ... and maybe add a date/time so you can conditionally know when to run it again?
ASKER
I've since added ''proc.WaitForExit();'' which helps some, but not all. Still two .bat DOS cmd widows open consecutively..... It's getting better though......
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt ring();
proc.WaitForExit();
proc.Start();
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt
proc.WaitForExit();
proc.Start();
It looks strange. What happends with this code?
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt ring();
proc.WaitForExit();
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt
proc.WaitForExit();
you have to syncronize the block of code:
private static object syncLock = new object();
private void backupBat()
{
lock(syncLock)
{
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt ring();
proc.Start();
proc.WaitForExit();
}
}
This way only one thread will touch the .bat file at one time.
private static object syncLock = new object();
private void backupBat()
{
lock(syncLock)
{
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt
proc.Start();
proc.WaitForExit();
}
}
This way only one thread will touch the .bat file at one time.
I would suggest in addition rewrite you code
if (thread1 == null)
{
thread1 = new Thread(new ThreadStart(backupBat));
thread1.IsBackground = true;
thread1.Priority = ThreadPriority.Normal;
thread1.Start();
}
and test if startBackupThread() called more that once.
2 Kate12
If thread started more that once we can't prevent to start the second copy when the first is finished.
if (thread1 == null)
{
thread1 = new Thread(new ThreadStart(backupBat));
thread1.IsBackground = true;
thread1.Priority = ThreadPriority.Normal;
thread1.Start();
}
and test if startBackupThread() called more that once.
2 Kate12
If thread started more that once we can't prevent to start the second copy when the first is finished.
well that code is backwards try this ...
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt ring();
proc.Start();
proc.WaitForExit();
thread1 = null;
then in your starting code ...
lock(SomeLockObject) {
if ((thread1 == null))
{
thread1 = new Thread(new ThreadStart(backupBat));
thread1.IsBackground = true;
thread1.Priority = ThreadPriority.Normal;
thread1.Start();
}
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToSt
proc.Start();
proc.WaitForExit();
thread1 = null;
then in your starting code ...
lock(SomeLockObject) {
if ((thread1 == null))
{
thread1 = new Thread(new ThreadStart(backupBat));
thread1.IsBackground = true;
thread1.Priority = ThreadPriority.Normal;
thread1.Start();
}
sry didnt see other posts on the first bit ... the main thing you need to protect is the *creation* of the thread ... not the execution portion of the thread ... I might also make the code set thread1 = null when it is complete and only check thread1==null ...
Cheers,
Greg
Cheers,
Greg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.