Include a dll as a reference and call objects of your class

0

I want to call the main function of the following class:

class Program
{
    static Pool _pool = null;
    static Work _work = null;
    static uint _nonce = 0;
    static long _maxAgeTicks = 20000 * TimeSpan.TicksPerMillisecond;
    static uint _batchSize = 100000;

    static void Main(string[] args)
    {
        while (true)
        {
            try
            {
                _pool = SelectPool();
                _work = GetWork();
                while (true)
                {
                    if (_work == null || _work.Age > _maxAgeTicks)
                        _work = GetWork();

                    if (_work.FindShare(ref _nonce, _batchSize))
                    {
                        SendShare(_work.Current);
                        _work = null;
                    }
                    else
                        PrintCurrentState();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Console.Write("ERROR: ");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine();
            Console.Write("Hit 'Enter' to try again...");
            Console.ReadLine();
        }
    }


    private static void ClearConsole()
    {
        Console.Clear();
        Console.WriteLine("*****************************");
        Console.WriteLine("*** Minimal Bitcoin Miner ***");
        Console.WriteLine("*****************************");
        Console.WriteLine();
    }

    private static Pool SelectPool()
    {
        ClearConsole();
        Print("Chose a Mining Pool 'user:password@url:port' or leave empty to skip.");
        Console.Write("Select Pool: ");
        string login = ReadLineDefault("lithander_2:[email protected]:8332");
        return new Pool(login);
    }

    private static Work GetWork()
    {
        ClearConsole();
        Print("Requesting Work from Pool...");
        Print("Server URL: " + _pool.Url.ToString());
        Print("User: " + _pool.User);
        Print("Password: " + _pool.Password);
        return _pool.GetWork();
    }

    private static void SendShare(byte[] share)
    {
        ClearConsole();
        Print("*** Found Valid Share ***");
        Print("Share: " + Utils.ToString(_work.Current));
        Print("Nonce: " + Utils.ToString(_nonce));
        Print("Hash: " + Utils.ToString(_work.Hash));
        Print("Sending Share to Pool...");
        if (_pool.SendShare(share))
            Print("Server accepted the Share!");
        else
            Print("Server declined the Share!");

        Console.Write("Hit 'Enter' to continue...");
        Console.ReadLine();
    }

    private static DateTime _lastPrint = DateTime.Now;
    private static void PrintCurrentState()
    {
        ClearConsole();
        Print("Data: " + Utils.ToString(_work.Data));
        string current = Utils.ToString(_nonce);
        string max = Utils.ToString(uint.MaxValue);
        double progress = ((double)_nonce / uint.MaxValue) * 100;
        Print("Nonce: " + current + "/" + max + " " + progress.ToString("F2") + "%");
        Print("Hash: " + Utils.ToString(_work.Hash));
        TimeSpan span = DateTime.Now - _lastPrint;
        Print("Speed: " + (int)(((_batchSize) / 1000) / span.TotalSeconds) + "Kh/s"); 
        _lastPrint = DateTime.Now;
    }

    private static void Print(string msg)
    {
        Console.WriteLine(msg);
        Console.WriteLine();
    }

    private static string ReadLineDefault(string defaultValue)
    {
        //Allow Console.ReadLine with a default value
        string userInput = Console.ReadLine();
        Console.WriteLine();
        if (userInput == "")
            return defaultValue;
        else
            return userInput;
    }
}

I create a new console application, add the dll, I put the class and the call to the class method:

using Example;

namespace ConsoleApplication1
{
    class Prueba
    {
        static void Main(string[] args)
        {
            metodo();
        }

        private static void metodo()
        {
            Program.Main(); // aqui no puedo llamarlo
        }


    }
}

When I call main, it gives me the following errors:

  

Severity Code Description Project File Line Suppression State   Error CS0122 'Program' is inaccessible due to its protection   level ConsoleApplication1 c: \ users \ moh \ documents \ visual studio   2015 \ Projects \ ConsoleApplication1 \ ConsoleApplication1 \ Test.cs 18 Activate

     

Severity Code Description Project File Line Suppression State   Error CS0103 The name 'args' does not exist in the current   context ConsoleApplication1 c: \ users \ moh \ documents \ visual studio   2015 \ Projects \ ConsoleApplication1 \ ConsoleApplication1 \ Test.cs 18 Activate

What would be the correct method to call my main function from my .dll that I have added as a reference and use a different spaces name?

Solved: private static string [] args;

If not, the array is defined as q no. total that I am delayed by that.

    
asked by Perl 09.11.2016 в 14:10
source

2 answers

2

Without analyzing the code a lot, it's not Prueba.Main(); is Program.Main(); , and you should pass some array to string as a parameter (even if it's an empty one), that this is mandatory in Main .

    
answered by 09.11.2016 / 15:17
source
1

First, I think you asked another question about this topic and you were told that to generate a dll you must create a project of type "Class Library". I guess you have not done it, since a class library has no "main" method

On the other hand, the class you want to call must be defined as public.

    
answered by 09.11.2016 в 15:25