C# 每次调用特定方法时创建一个新的后台线程

C# 每次调用特定方法时创建一个新的后台线程,c#,winforms,multithreading,C#,Winforms,Multithreading,WinForm: 在我的主应用程序中有一个名为check_news的方法。 我如何创建并运行一个线程,在每次调用该方法时(通过按下按钮或在程序开始时),该线程将在后台对该方法进行处理 我知道如何创建线程,但如何在每次调用函数时创建新线程(新线程是因为旧线程已死亡) 我应该创建一个新类并使用新类对象和所需的方法运行线程吗? 我应该在哪里定义线程?您可以通过编写新线程(…) …可以是方法名、委托实例或匿名方法 每次执行此代码时,它都将创建一个新的线程实例 请注意,使用线程池将更有效。您可以通过编写新

WinForm: 在我的主应用程序中有一个名为check_news的方法。 我如何创建并运行一个线程,在每次调用该方法时(通过按下按钮或在程序开始时),该线程将在后台对该方法进行处理

我知道如何创建线程,但如何在每次调用函数时创建新线程(新线程是因为旧线程已死亡)

我应该创建一个新类并使用新类对象和所需的方法运行线程吗?
我应该在哪里定义线程?

您可以通过编写
新线程(…)

可以是方法名、委托实例或匿名方法

每次执行此代码时,它都将创建一个新的
线程
实例


请注意,使用
线程池将更有效。

您可以通过编写
新线程(…)
在方法内部创建一个线程。
可以是方法名、委托实例或匿名方法

每次执行此代码时,它都将创建一个新的
线程
实例


请注意,使用
线程池将更有效。

我认为实现这一点的最佳方法是将其添加到线程池中,这样做既简单又快速

例如:

public static void Main(string[] args)
{
    check_news();
}

private static void check_news()
{
    ThreadPool.QueueUserWorkItem((obj) =>
        {
            // Fetch the news here
            Thread.Sleep(1000); // Dummy
        });
}
或者,如果你真的想自己处理,这是你可以使用的:

public static void Main(string[] args)
{
    check_news();
    Console.ReadKey();
}

private static void check_news()
{
    Thread t = new Thread(() =>
        {
            // Check news here
            Thread.Sleep(1000); // Dummy
        });
    t.Priority = ThreadPriority.Lowest; // Priority of the thread
    t.IsBackground = true; // Set it as background (allows you to stop the application while news is fetching
    t.Name = "News checker"; // Make it recognizable
    t.Start(); // And start it
}
但您应该知道,这需要更长的时间来启动,它不会重用线程,并且没有真正的优势

或者,如果您需要更多控制,可以使用异步平台:

public static void Main(string[] args)
{
    check_news(); // You can add an argument 'false' to stop it from executing async
    Console.WriteLine("Done");
    Console.ReadKey();
}

public delegate void Func();

public static void check_news(bool async = true)
{
    Func checkNewsFunction = () =>
        {
            //Check news here
            Thread.Sleep(1000);
        };
    if (async)
    {
        AsyncCallback callbackFunction = ar =>
        {
            // Executed when the news is received

        };
        checkNewsFunction.BeginInvoke(callbackFunction, null);
    }
    else
        checkNewsFunction();
}

请注意,所有示例中的lambda表达式都可以用正则函数替换。但是我现在就使用它们,因为它看起来更像是一个例子。

我认为实现这一点的最好方法是将它添加到线程池中,这样做既简单又快速

    public void Example()
    {
        //call using a thread.
        ThreadPool.QueueUserWorkItem(p => check_news("title", "news message"));
    }

    private void check_news(string news, string newsMessage)
    {

    }
例如:

public static void Main(string[] args)
{
    check_news();
}

private static void check_news()
{
    ThreadPool.QueueUserWorkItem((obj) =>
        {
            // Fetch the news here
            Thread.Sleep(1000); // Dummy
        });
}
或者,如果你真的想自己处理,这是你可以使用的:

public static void Main(string[] args)
{
    check_news();
    Console.ReadKey();
}

private static void check_news()
{
    Thread t = new Thread(() =>
        {
            // Check news here
            Thread.Sleep(1000); // Dummy
        });
    t.Priority = ThreadPriority.Lowest; // Priority of the thread
    t.IsBackground = true; // Set it as background (allows you to stop the application while news is fetching
    t.Name = "News checker"; // Make it recognizable
    t.Start(); // And start it
}
但您应该知道,这需要更长的时间来启动,它不会重用线程,并且没有真正的优势

或者,如果您需要更多控制,可以使用异步平台:

public static void Main(string[] args)
{
    check_news(); // You can add an argument 'false' to stop it from executing async
    Console.WriteLine("Done");
    Console.ReadKey();
}

public delegate void Func();

public static void check_news(bool async = true)
{
    Func checkNewsFunction = () =>
        {
            //Check news here
            Thread.Sleep(1000);
        };
    if (async)
    {
        AsyncCallback callbackFunction = ar =>
        {
            // Executed when the news is received

        };
        checkNewsFunction.BeginInvoke(callbackFunction, null);
    }
    else
        checkNewsFunction();
}

请注意,所有示例中的lambda表达式都可以用正则函数替换。但是我现在就用它们,因为它看起来更像一个例子。

你会想在收到新闻后显示它。首先使用BackgroundWorker类,它可以让您避免麻烦。您需要在获得新闻后显示新闻。首先使用BackgroundWorker类,这样可以避免麻烦。这样做时,我会得到一个异常“Cross thread operation not valid”:这是我第一次调用该方法,你知道我为什么会得到异常吗?是的。您只能从GUI线程更改GUI控件。检查这个问题的解决方案:我读的链接,但仍然在空白,WHT是GUI线程?这是主线吗?如何使用其他线程更改GUI控件?GUI线程=主线程。下面是对
InvocationRequired
的更详细解释:执行此操作时,我会得到一个异常“Cross thread operation not valid”:这是我第一次调用该方法,知道我为什么会得到异常吗?是的。您只能从GUI线程更改GUI控件。检查这个问题的解决方案:我读的链接,但仍然在空白,WHT是GUI线程?这是主线吗?如何使用其他线程更改GUI控件?GUI线程=主线程。下面是对
调用的更详细解释,需要
    public void Example()
    {
        //call using a thread.
        ThreadPool.QueueUserWorkItem(p => check_news("title", "news message"));
    }

    private void check_news(string news, string newsMessage)
    {

    }