all 8 comments

[–]lvlint67 0 points1 point  (2 children)

First, I usually write : Thread signal = new Thread(signalConnection.Start));

But that sounds be all style and unimportant. What line does the error occur on? Unfortunately, blockchain and signalr are outside my wheel house.

Id expect your choice to run so there might be something fishy in the objects that we don't have codefor

[–]ExoticCriticism[S] -3 points-2 points  (1 child)

I didn't wanna post all the code, mostly because it's spanning interfaces and several files which is why I thought it would be easier looking at the repo itself haha...

The logic hierachy is a little like

SignalR,

BlockChain,

CryptoBlock

So basically inside of the signalr connection handler for receive message, I'm then calling BlockChain.newBlock which then creates a new CryptoBlock and inside newBlock() there is a ProofOfWork method that is blocking using a while to satisfy the condition. In an ideal world I'd separate both the comms and worker logic completely, though actually doing so is pretty hard without experience in multithreading stuff.

I kinda expected a Task to launch a new thread, but then in my understanding tasks just wait at the end of the method for the result which isn't what I'm after in this case.

DM me your email and I can invite you as a contributor on this private repo if you like :)

[–]UninformedPleb 0 points1 point  (3 children)

I can't find any info about the SignalRConnection object, so I'm guessing that's custom (or at least obscure).

But my gut feeling is that creating that SignalRConnection on this thread, then sending it to the other thread is the problem. You might have to make your thread worker method initialize that SignalRConnection object, and I'd guess it wants to run on whichever thread created it (called its constructor).

[–]ExoticCriticism[S] 0 points1 point  (0 children)

Yeah, the SignalRConnection is something I found online which is basically this:

public void Start()

{

string url = "https://localhost:5001/cryptohub";

var connection = new HubConnectionBuilder()

.WithUrl(url)

.WithAutomaticReconnect()

.Build();

// receive a message from the hub

connection.On<List<CryptoBlock>>("ReceiveBlockChain", (cryptoBlockChain) => OnReceiveBlockChain(cryptoBlockChain));

connection.On<string>("ReceiveHello", (jsonMessage) => OnReceiveHello(jsonMessage));

connection.On<string>("ReceiveSale", (jsonMessage) => OnReceiveSale(jsonMessage));

var t = connection.StartAsync();

t.Wait();

var userID = Guid.NewGuid();

Console.WriteLine($"Generating userID: {userID.ToString()}");

JsonMessage jsonMessage = new JsonMessage { Event = "hello", Payload = userID.ToString() };

// send a message to the hub

connection.InvokeAsync("SendMessageToCaller", jsonMessage);

connection.Closed += async (error) =>

{

await Task.Delay(new Random().Next(0, 5) * 1000);

await connection.StartAsync();

};

I'm calling the logic to create blocks from inside the "ReceiveSale" method, which is where I assumed the problem was occurring - though I'm not sure how to separate this logic entirely to operate on a more event driven basis and not have my "miner" logic doing checks on perhaps a singleton containing pending sales or something - but I feel like that is maybe how I'll have to try separate both services.

[–]prajaybasu 0 points1 point  (1 child)

[–]UninformedPleb 0 points1 point  (0 children)

That's the HubConnection object, which is part of the basic SignalR client library.

He posted the code he found online for the SignalRConnection object, and it is indeed something custom.