Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Threading Process Has Multiple Openings?

Posted on 2007-03-19
10
Medium Priority
?
294 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

604 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