Home

How to cancel all threads

I am adding a button in linqpad with an onclick action, like this

controls.Add(new Button("Customers", onClick: async btn => await StartClientAsync("Customers")));

when I click the button it runs fine and ends successfully but I get the message in Linqpad that I need to click CTRL-SHIFT F5 to cancel all threads.

I don't understand which thread is running, is there a way to stop all the threads in Code?
before I click the button there are no threads running.

I am using c# program , the main method is not async as I have no await.

thanks

Comments

  • Include the following code in your query:

    Util.NewProcess = true;
    

    This will kill the previous process each time you re-run the query.

  • I tried to add Util.NewProcess = true to all calls but could not get it to work
    My code look something like this:

    JsonSerializerSettings jsonSetting;
    HttpClient client;
    List<Control> controls;
    string RadioRet = "";
    
    
    void Main()
    {
        controls = new List<Control>();
        foreach (var ps in cfg)
        {
            controls.Add(new RadioButton("group1", ps.CustName, onClick: r => RadioRet = ps.CustNo));
        }
        controls.Add(new Button("Customers", onClick: async btn => await StartClientAsync("Customers")));
        controls.Dump();
    
    }
    
    async Task StartClientAsync(string what)
    {
        // initialize api
    
        // hidden
    
        // call the endpoints
        switch(what){
            case "Customers" : 
                await GetCustomers();
                break;
    
        }
    
    }
    
    async Task GetCustomers()
    {
        var json = await client.GetStringAsync("controller/api/v1/customer?pagesize=10");
        List<Customer> customer = Customer.FromJson(json);
        Util.Pivot(customer).Dump("10 First Customers", 1);
    }
    
  • I'm not sure what the problem is. What are you trying to achieve?

  • my question is why LinqPad have threads running when the script has ended

  • Your script is technically still running since you're using Control components that need to function after the script has completed (e.g., Button). To be able to run the component actions, something has to be running to execute them.

    If you want to run it and have it completely stop, don't use UI controls like you are, just run the action code directly.

  • yes ok, but the strange thing is that before I click the button the first time there are no threads running. so why do they need to be running the second time and not the first time?

  • I suspect the execution thread is spun up on first interaction. No need to run it if nothing needs to be run.

Sign In or Register to comment.