简体   繁体   中英

How to run same method in separate thread without end

I've a project which have a static web browser in the method. I wanted the run this method(which including the web browser)in same time, without ending. There is:

1 method 2 button 2 textbox

static webbrowser wb;

public static look(string address)
{
    wb = new webbrowser;
    wb.navigate(address);
}

button1.click()
{
    for(int i = 0;i <= converttoint32(textbox1.text);i++)
        look(textbox2.text);
}

button2.click()
{
    //close the threads.
}

< I want to use separate threads to create new webbrowser. They should continue until the button2.click.

eg. when clicked to button1 method will create web browsers(count from textbox1.text) and they will stay connected. When clicked to button2 the tasks will closed.

Based on this , you can try.

public static object locker =  new object();

    public static void InitBrowser(int browser)
    {
        var thread = new Thread(() =>
        {
            // Create Browser Here

            Monitor.Wait(locker);
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }

button1.click()
{       
   Monitor.Enter(locker);
   for(int i = 0;i <= converttoint32(textbox1.text);i++)
    InitBrowser(textbox2.text);
}

button2.click()
{
    Monitor.PulseAll(locker);
}

You can try this. This solution is not tested, but maybe you could use it like a base to build waht you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM