Tim
asked on
C# Multithreading Question
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:
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 = "";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window