Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Threading Process Has Multiple Openings?

Posted on 2007-03-19
10
283 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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