-2

I'm working on my multi-threading password cracker, only numbers. It must show how much time has passed to find the password. I used Stopwatch to find it, but in functions Stopwatch doesn't work. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;


namespace ConsoleApplication4
{
  class Program
  {
    static void Main(string[] args)
    {
        int psw = 14995399;

        Stopwatch time = new Stopwatch();

        Thread Thread1 = new Thread(islem1);
        Thread Thread2 = new Thread(islem2);
        Thread Thread3 = new Thread(islem3);
        Thread Thread4 = new Thread(islem4);

        time.Start();

        Thread1.Start();
        Thread2.Start();
        Thread3.Start();
        Thread4.Start();

        Thread.Sleep(1000);

        time.Stop();
        System.Console.WriteLine("Time elapsed: {0}", time.Elapsed);
        Console.ReadKey();
    }

    static void islem1()
    {
        for (int i = 00000000; i < 25000000; i++)
        {
            int psw = 14995399;
            if (i == psw)
            {
                System.Console.WriteLine("Şifre=" + i);

                time.Stop();
                Console.WriteLine("Time elapsed: {0}", time.Elapsed);
                Console.ReadKey();
            }
        }
    }

    static void islem2()
    {
        for (int i = 25000000; i < 50000000; i++)
        {
            int psw = 14995399;
            if (i == psw)
            {
                System.Console.WriteLine("Şifre=" + i);

                time.Stop();
                Console.WriteLine("Time elapsed: {0}", time.Elapsed);
                Console.ReadKey();
            }
        }
    }


    static void islem3()
    {
        for (int i = 50000000; i < 75000000; i++)
        {
            int psw = 14995399;
            if (i == psw)
            {
                System.Console.WriteLine("Şifre=" + i);

                time.Stop();
                Console.WriteLine("Time elapsed: {0}", time.Elapsed);
                Console.ReadKey();
            }
        }
    }

    static void islem4()
    {
        for (int i = 75000000; i < 100000000; i++)
        {
            int psw = 14995399;
            if (i == psw)
            {
                System.Console.WriteLine("Şifre=" + i);

                time.Stop();
                Console.WriteLine("Time elapsed: {0}", time.Elapsed);
                Console.ReadKey();
            }
        }
    }
  }
}
honk
  • 9,137
  • 11
  • 75
  • 83

3 Answers3

4

It's because your variable

Stopwatch time = new Stopwatch();

declared outside the visibility functions. The scope of visibility the var is your function Main. You can pass Stopwatch as a parameter to your functions:

Thread1.Start(time);

Or declare it as a class field:

class Program
{
    private static Stopwatch time = new Stopwatch();
    ...
}

Note that you have just one Stopwatch instance then if you stop it in one thread it'll stopped in all application and elapsed time will not changed after that.

Then you should delete time.Stop(); from your Main method because it can caused the result in cased when your threads works longet then 1 second.

Also it's no reason to call Thread.Sleep(). Just delete this lines and your code continues work as expected.

Finally you can delete Console.ReadKey() from your thread functions because your main-thread already waits for user input.

The whole solution with configurable threads count can illustrate interesting results for different number of threads. Try the code below which can illustrate work with thread parameters and reduce lines of code:

using System;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication4
{
    internal class Program
    {
        private class BruteforceParams
        {
            public int StartNumber { get; set; }
            public int EndNumber { get; set; }
        }

        private const int password = 14995399;
        private static readonly Stopwatch time = new Stopwatch();

        private static void Main(string[] args)
        {
            const int maxPassword = 100000000;

            Console.WriteLine("Enter number of threads: ");
            var threadsCountString = Console.ReadLine();
            var threadsCount = int.Parse(threadsCountString);

            var threads = new Thread[threadsCount];
            for (int i = 0; i < threadsCount; i++)
            {
                var thread = new Thread(Bruteforce);
                threads[i] = thread;
            }

            time.Start();
            for (int i = 0; i < threadsCount; i++)
            {
                threads[i].Start(new BruteforceParams { StartNumber = i * maxPassword / threadsCount, EndNumber = (i + 1) * maxPassword / threadsCount });
            }

            Console.ReadKey();
        }

        private static void Bruteforce(object param)
        {
            var bp = (BruteforceParams) param;
            for (int i = bp.StartNumber; i < bp.EndNumber; i++)
            {
                if (i == password)
                {
                    Console.WriteLine("Şifre=" + i);
                    time.Stop();
                    Console.WriteLine("Time elapsed: {0}", time.Elapsed);
                }
            }
        }
    }
}
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • I'm not sure I understand your scenario but I think that you no need to get ReadKey in every thread function. Try to delete it and it will continues work as expected I think. Also, delete time.Stop(); line from Main function. – Vadim Martynov Jan 01 '16 at 20:51
  • This isn't going to be all that useful to OP because they call `Stop` on the single Stopwatch instance several times. This will have unexpected results. – spender Jan 01 '16 at 20:51
  • this is a homework for me to understand the time difference between brute force password crack and multi threading password crack. – Ahmet Özdemir Jan 01 '16 at 21:01
  • OK. Then, you also no need to declare `int psw = 14995399;` in each thread function, you can just declare it as the class field like a `StopWatch` in my answer. Also it can be interesting to create configurable by threads count system. You can read N from console, creates N treads and pass to each thread start search number `i * 100000000 / N` where `i` is your thread index. It's not your homework but it can be interesting and it'll not take long ;) – Vadim Martynov Jan 01 '16 at 21:12
  • that's a good idea, but now i'm gonna do the password cracker for parallel task thing, i don't know how to use that, and what is the idea of that, this is the last night for the homework :) – Ahmet Özdemir Jan 01 '16 at 21:41
  • @AhmetÖzdemir I updated my answer with whole solution. – Vadim Martynov Jan 01 '16 at 21:43
  • your code is so complicated for me, because i dont know c# codding, i'm learning it for homework, and as i see we define the thread number, but it must be 4 threads to use. i have another question – Ahmet Özdemir Jan 02 '16 at 21:21
  • static void islem1() { Stopwatch time = new Stopwatch(); time.Start(); for (int i = 00000000; i < 25000000; i++) { int psw = 14995399; if (i == psw) { System.Console.WriteLine("Şifre=" + i); time.Stop(); Console.WriteLine("Time elapsed: {0}", time.Elapsed); Console.ReadKey(); } } } – Ahmet Özdemir Jan 02 '16 at 21:25
  • this is my first thread, other threads are same like this one. when i start the stopwatch, and stop it, i think it starts timer from 0 on other functions. i need to have the total time to find the password – Ahmet Özdemir Jan 02 '16 at 21:25
1

How do you think time.Stop(); going to work inside your function body islem1() or any other since you have defined the stopwatch inside Main() function body. You are bound to get compilation error saying time doesn't exist in current context.

static void islem1()
{
        .............
            time.Stop();  // this line of code
            Console.WriteLine("Time elapsed: {0}", time.Elapsed);
            Console.ReadKey();
        }
    }
}

Rather, you can create a separate watch per method and report that

static void islem1()
{
      StopWatch time = Stopwatch.StartNew();
      time.Stop();  // this line of code
      Console.WriteLine("Time elapsed: {0}", time.Elapsed);
      Console.ReadKey();
 }
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

It's going to be difficult to extract meaningful timings using a single Stopwatch instance.

You might chose to make your timing measurements using a different pattern that uses a new Stopwatch for each measurement.

void Main()
{
    var t1 = new Thread(_ => {
        var sw = Stopwatch.StartNew();
        DoSomething();
        Console.WriteLine("took {0}ms", sw.ElapsedMilliseconds);
    });
    var t2 = new Thread(_ => {
        var sw = Stopwatch.StartNew();
        DoSomethingElse();
        Console.WriteLine("took {0}ms", sw.ElapsedMilliseconds);
    });
    t1.Start();
    t2.Start();
    t1.Join();
    t2.Join();
    Console.ReadKey();
}

void DoSomething()
{
    //do something
}

void DoSomethingElse()
{
    //do something
}
spender
  • 117,338
  • 33
  • 229
  • 351