Solved

Threading Process Has Multiple Openings?

Posted on 2007-03-19
10
288 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

688 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