Application continues to be executed even after closure

Asked

Viewed 83 times

1

I’m having a problem with my application. The same continues in the list of processes even after its completion, besides it, also appears in the list the application Clickonce.

Setting

My application communicates with a turnstile through a dll, and this communication is managed by a thread in the state while(true).

It turns out that eventually the thread is terminated without explanation/Exception. I then have another thread whether the thread original .isAlive, if it is false the same as a new call in thread. But at this time an error occurs, stating that you cannot access dll why it is being used by another thread.

Follow an example of the code:

public static Thread th;
public static Thread thVerify;

void btnIniciar_Click(...)
{
    th = new Thread(() => IniciarCatraca());
    thVerify = new Thread(() => IniciarVerificacaoThread(th));
    th.Start();
    thVerify.Start();
}

public void IniciarVerificacaoThread(Thread t)
{
    try
    {
        while(true)
            if (t == null || !t.isAlive())
                th = new Thread(() => IniciarCatraca());
    }
    catch(Exception ex)
    {
        CallCatch(ex);
    }
 }

It checks correctly if the thread th is active, but still gives exception when trying to start the turnstile again, informing that the dll is already in use by another thread, whereas the thread was closed!

Then, I finish the application and open the process manager. There are two processes ClickOnce and ProjetoCatraca. If I open the application again and try to start the communication with the ratchet without before finalizing the processes is returned me the same error, the dll is being used by another thread.

Could you tell me why the application remains on the list of cases even after its closure?

This can be caused by the use of thread?

Is there any way to verify the existence of a thread using methods other than those expressed in my code? Any suggestions?

  • 2

    In which part of the code the thread is terminated?

  • o Hometurnstile(); When the turnstile takes too long to respond or when there is a failure to communicate with it in any of the calls made by th to the turnstile, such as releasing turn, checking card and etc.. Communication with the ratchet is via the network. .

1 answer

0

Has to finish the thread when closing the program, otherwise it is always running, alias, to do better, simply create a bool static call stopThread, and within the while of the thread, check if stopThread is active, if it is, simply from a break

  • 1

    This was the alternative I found, in the "Form_closing" event, I set its value to False, and then the thread leaves the while and ends.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.