'c# How to multi-use one class in winform

I created multiple rows through datagridview in parent form

I create a button in the datagridview and try to call the class for each row button

When calling class, the same class is called through the data set in the parent form, but the parameters entered into the class are different.

When I press the button,

if(button.text = "start")
{
    TestSocketService testSocketService= new SocketServerService();
    testSocketService.FormSendEvent += new TestSocketService.FormSendDataHandler(CallFunc);
    thread = new Thread(() => testSocketService.Start(xml, Row, Column));
    thread.Start();
}
else if(button.text = "stop")
{
    testSocketService.stop();
}

I wrote the code like this

But an error occurs

What's wrong with it?

I want to be able to play each role while running one class at the same time



Solution 1:[1]

You are declaring TestSocketService testSocketService= new SocketServerService(); in the scope of an if statement.

In the else statement you are trying to stop a service instance that doesn't exist (or if it exists its a different one).

testSocketService.stop();

Try declaring TestSocketService testSocketService instance outside your if statement and see if it works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikola Develops