Solved

Threading Process Has Multiple Openings?

Posted on 2007-03-19
10
277 Views
Last Modified: 2010-04-16
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.ThreadState.Unstarted | System.Threading.ThreadState.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.ToString();
            proc.Start();
        }
0
Comment
Question by:kvnsdr
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 14

Expert Comment

by:dfu23
ID: 18750902
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?
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 18751474
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.ToString();
            proc.WaitForExit();
            proc.Start();
     
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18751790
It looks strange. What happends with this code?
Process proc = new Process();
proc.StartInfo.FileName = txtBackupBatPath.Text.ToString();
proc.WaitForExit();
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18751791
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.ToString();
            proc.Start();
            proc.WaitForExit();
        }
}
This way only one thread will touch the .bat file at one time.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:AlexNek
ID: 18752256
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.

0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 18752740
well that code is backwards try this ...

            Process proc = new Process();
            proc.StartInfo.FileName = txtBackupBatPath.Text.ToString();
            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();
}
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 18752755
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
0
 
LVL 1

Accepted Solution

by:
kvnsdr earned 0 total points
ID: 18775075
Sorry I didn't get back sooner. I've been gone, just got back.

Anyway, the proc.WaitForExit(); works very well.......

            Process proc = new Process();
            proc.StartInfo.FileName = txtBackupBatPath.Text.ToString();
            proc.WaitForExit();
            proc.Start();
     

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now