Solved

C# Multithreading Question

Posted on 2014-03-24
2
266 Views
Last Modified: 2014-05-21
Hi ALl,

Here is my basis of completion:

1). run through text file filled with servers.
2). Connect to each server and check for shares
3). Use 10 threads to run through the list. I know 10 is an arbitrary number, but it was one I picked based on the # of servers I have.

When I run the code for the same share one of three results occur:

I will get the correct output
I will get duplicate output
I will get no output

It changes each time I run the code, so I am assuming that at least two threads are accessing the same variable at the sametime, just not sure how to get this fixed outside of using the lock() function. I read a bunch of C# Multithreading How-to's but for some reason its not happening for my code. Below is the code I am using:

public class ShareCheck
    {
        static string share;
        static void Main(string[] args)
        {
            String server = "";
            share = args[0];

            StreamReader file = new StreamReader("servers.txt");

            int n = 0;
            while ((server = file.ReadLine()) != null)
            {
                if (n == 0)
                {
                    Thread a = new Thread(() => CheckShare(server));
                    a.Start();
                }
                else if (n == 1)
                {
                    Thread b = new Thread(() => CheckShare(server));
                    b.Start();
                }
                else if (n == 2)
                {
                    Thread c = new Thread(() => CheckShare(server));
                    c.Start();
                }
                else if (n == 3)
                {
                    Thread d = new Thread(() => CheckShare(server));
                    d.Start();
                }
                else if (n == 4)
                {
                    Thread e = new Thread(() => CheckShare(server));
                    e.Start();
                }
                else if (n == 5)
                {
                    Thread f = new Thread(() => CheckShare(server));
                    f.Start();
                }
                else if (n == 6)
                {
                    Thread g = new Thread(() => CheckShare(server));
                    g.Start();
                }
                else if (n == 7)
                {
                    Thread h = new Thread(() => CheckShare(server));
                    h.Start();
                }
                else if (n == 8)
                {
                    Thread i = new Thread(() => CheckShare(server));
                    i.Start();
                }
                else if (n == 9)
                {
                    Thread j = new Thread(() => CheckShare(server));
                    j.Start();
                    n = 0;
                }
                else
                    n++;

            }
        }

        static void CheckShare(String server)
        {
            Process p = new Process();
            p.StartInfo.FileName = "share.exe";
            p.StartInfo.Arguments = "\\\\" + server + "\\" + share + "$";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();

            lock (server)
            {
                p.WaitForExit();
                if (p.StandardOutput.ReadToEnd().IndexOf("completed successfully") > -1)
                {
                    Console.WriteLine("\\\\" + server + "\\" + share + "$");
                }
            }
            server = "";
        }

Open in new window

0
Comment
Question by:tr57285
  • 2
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39951643
Why even have the share static variable? It's just a string, so the cost of passing it is minimal. I suggest passing what you normally store in share as a parameter to each thread. In this way, you don't have any global variable access, and thus there will be no need for locking.

e.g.

public class ShareCheck
{
    static void Main(string[] args)
    {
        String server = "";
        String share = args[0];

        StreamReader file = new StreamReader("servers.txt");

        int n = 0;
        while ((server = file.ReadLine()) != null)
        {
            if (n == 0)
            {
                Thread a = new Thread(() => CheckShare(server, share));
                a.Start();
            }
            else if (n == 1)
            {
                Thread b = new Thread(() => CheckShare(server, share));
                b.Start();
            }
            else if (n == 2)
            {
                Thread c = new Thread(() => CheckShare(server, share));
                c.Start();
            }
            else if (n == 3)
            {
                Thread d = new Thread(() => CheckShare(server, share));
                d.Start();
            }
            else if (n == 4)
            {
                Thread e = new Thread(() => CheckShare(server, share));
                e.Start();
            }
            else if (n == 5)
            {
                Thread f = new Thread(() => CheckShare(server, share));
                f.Start();
            }
            else if (n == 6)
            {
                Thread g = new Thread(() => CheckShare(server, share));
                g.Start();
            }
            else if (n == 7)
            {
                Thread h = new Thread(() => CheckShare(server, share));
                h.Start();
            }
            else if (n == 8)
            {
                Thread i = new Thread(() => CheckShare(server, share));
                i.Start();
            }
            else if (n == 9)
            {
                Thread j = new Thread(() => CheckShare(server, share));
                j.Start();
                n = 0;
            }
            else
                n++;

        }
    }

    static void CheckShare(String server, String share)
    {
        Process p = new Process();
        p.StartInfo.FileName = "share.exe";
        p.StartInfo.Arguments = "\\\\" + server + "\\" + share + "$";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();

        p.WaitForExit();

        if (p.StandardOutput.ReadToEnd().IndexOf("completed successfully") > -1)
        {
            Console.WriteLine("\\\\" + server + "\\" + share + "$");
        }
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39951659
You might prefer a LINQ-based approach also:

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace ConsoleApplication105
{
    public class ShareCheck
    {
        static void Main(string[] args)
        {
            String share = args[0];

            File.ReadLines("servers.txt")
                .AsParallel()
                .ForAll(server => CheckShare(server, share));
        }

        static void CheckShare(String server, String share)
        {
            Process p = new Process();
            p.StartInfo.FileName = "share.exe";
            p.StartInfo.Arguments = "\\\\" + server + "\\" + share + "$";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();

            p.WaitForExit();

            if (p.StandardOutput.ReadToEnd().IndexOf("completed successfully") > -1)
            {
                Console.WriteLine("\\\\" + server + "\\" + share + "$");
            }
        }
    }
}

Open in new window

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

Title # Comments Views Activity
rhino JavaScript import, load 25 67
Runtime Error 2 28
Turn on intranet settings 1 39
free scanner TWAIN can be operated with a Web application 9 39
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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!
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

24 Experts available now in Live!

Get 1:1 Help Now