Solved

Threading Process Has Multiple Openings?

Posted on 2007-03-19
10
274 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows phone Development 11 27
Using Simpleject with Class Library 11 24
Default parameter problem in C# 3 25
String manipulation 15 49
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

10 Experts available now in Live!

Get 1:1 Help Now