How do i start the block chain using c# code

0 votes
I am trying to open cmd and through C# code and execute the APIs provided in the documentation. I am able to create a block chain using the 'multichain-util create chain9' but not able to initiate it using the 'multichaind chainName -daemon' command. It shows me the following exception:

IO error: C:\Users\Kishan\AppData\Roaming\MultiChain\chain10\permissions.db\MANIFEST-000002: The handle is invalid.

Below is my source code:

   Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.WorkingDirectory = @"D:\Work\Blockchain";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            //p.StandardInput.WriteLine("multichain-util create chain10");
            p.StandardInput.WriteLine("multichaind chain10 -daemon");
            //p.StandardInput.WriteLine("cd..");
            p.StandardInput.Flush();
            p.StandardInput.Close();
            p.WaitForExit();
            Console.WriteLine(p.StandardOutput.ReadToEnd());
 

Could you please check on this and provide me with and answer would be really greatful !

Thanks !
asked Aug 6, 2017 by kishanvikani
edited Aug 6, 2017 by kishanvikani
can u send full code for this project because i want to create one simple multichain UI so i need one simple example
can u send full code for this project because i want to create one simple multichain UI so i need one simple example
i created node but i dont know how to connect multichain to C# UI application
Sure, I will have it on my github you can clone it from there itself. It would take me a week i guess. So keep trying till then, Will update you once i have it on github !

1 Answer

+2 votes
 
Best answer
I think this is what you are seeking.

static void Main(string[] args)
        {

            Process p = new Process();

            p.StartInfo.WorkingDirectory = @"c:\multichain\1.0\";
            p.StartInfo.FileName = @"c:\multichain\1.0\multichaind.exe";
            p.StartInfo.Arguments = "LOTest5 -daemon";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            
            p.StartInfo.CreateNoWindow = true;
         
            p.OutputDataReceived += P_OutputDataReceived;
          
            p.Start();
            p.BeginOutputReadLine();
            p.WaitForExit();
        }
        
       
        private static void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                Console.WriteLine(e.Data);
            }
        }
answered Aug 7, 2017 by MaSsv
selected Aug 9, 2017 by kishanvikani
This was helpful, could you think of as why this is happening ?
This worked when I started the chain once but after i terminate the program and again run it. It shows the same error ! This happens with every new chain I create, It starts only once also it terminates it's hosting on its own with the following console message.

Other nodes can connect to this node using:
multichaind chain9@192.168.0.6:9245

Node started
The thread 0x1a28 has exited with code 259 (0x103).
The thread 0x18b0 has exited with code 259 (0x103).
The thread 0x1f08 has exited with code 259 (0x103).
With p.StartInfo.CreateNoWindow = true; you are not seeing that the process is still running. MultiChaind executable will not exit. It runs and stays running. - You will need to get the Process identifier from the variable in your code and end the process. Even better call Stop method
Thank You ! This was very helpful.
I have successfully created a chain and then executed it with code. But till the chain is running i am not able to click on any other button where by i can perform and other operation further like giving permission or any other command that uses multichain-cli. It would be great if you point out the issue i missed !
disclaimer: the correct means of accesing a node and its json rpc api is NOT executing the multichaincli exe within a process..
Could you guide how to do it ? Do I need to use any library, which would have methods using which we can access the JSON RPC API. Could you help a little ?
...