C# 调用方法以使程序不';不要被阻挡

C# 调用方法以使程序不';不要被阻挡,c#,multithreading,task-parallel-library,C#,Multithreading,Task Parallel Library,我需要在下面的C#代码中调用SendEmail(),这样我的程序就不会因为SendEmail()方法花费大量时间或失败而被阻止 这是我的C代码:(我使用的是.NET4.5) 我可以用下面的方法实现同样的效果吗?或者还有其他更好的方法吗?使用async/await是更好的方法吗 public void MyMethod() { DoSomething(); try { s

我需要在下面的C#代码中调用SendEmail(),这样我的程序就不会因为SendEmail()方法花费大量时间或失败而被阻止

这是我的C代码:(我使用的是.NET4.5)

我可以用下面的方法实现同样的效果吗?或者还有其他更好的方法吗?使用async/await是更好的方法吗

public void MyMethod()
        {
            DoSomething();

            try
            {   
                string emailBody = "TestBody";
                string emailSubject = "TestSubject";

                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SendEmailAlert), arrEmailInfo);
            }
            catch (Exception ex)
            {
                //Log error message
            }

        }

        private void SendEmailAlert(object state)
        {
            string[] arrEmailnfo = state as string[];
            MyClassX.SendAlert(arrEmailnfo[0], arrEmailnfo[1]);
        }
如果我需要将SendEmailAlert()方法设为fire And forget,我可以使用这样的代码,这样对吗?----->


谢谢。

Async await绝对可以帮助您。当有CPU绑定的工作要异步执行时,可以使用
Task.Run()
。可以“等待”此方法,以便在任务完成后恢复代码

在你的情况下,我会这样做:

public async Task MyMethod()
{
    DoSomething();

    try
    {   
        string emailBody = "TestBody";
        string emailSubject = "TestSubject";

        await Task.Run(()=> SendEmailAlert(arrEmailInfo));

        //Insert code to execute when SendEmailAlert is completed.
        //Be aware that the SynchronizationContext is not the same once you have resumed. You might not be on the main thread here
    }
    catch (Exception ex)
    {
        //Log error message
    }

}

private void SendEmailAlert(string[] arrEmailInfo)
{
    MyClassX.SendAlert(arrEmailnfo[0], arrEmailnfo[1]);
}

Async Wait绝对可以帮助您。当有CPU绑定的工作要异步执行时,可以使用
Task.Run()
。可以“等待”此方法,以便在任务完成后恢复代码

在你的情况下,我会这样做:

public async Task MyMethod()
{
    DoSomething();

    try
    {   
        string emailBody = "TestBody";
        string emailSubject = "TestSubject";

        await Task.Run(()=> SendEmailAlert(arrEmailInfo));

        //Insert code to execute when SendEmailAlert is completed.
        //Be aware that the SynchronizationContext is not the same once you have resumed. You might not be on the main thread here
    }
    catch (Exception ex)
    {
        //Log error message
    }

}

private void SendEmailAlert(string[] arrEmailInfo)
{
    MyClassX.SendAlert(arrEmailnfo[0], arrEmailnfo[1]);
}

查看async/await您可以使用线程,但使用async/await可能更好。查看async/await您可以使用线程,但使用async/await可能更好。非常感谢。如果我需要将SendEmailAlert()方法设为fire And forget,我可以删除wait关键字,然后像下面那样使用它。这样正确吗?----->运行(()=>sendmailalert(arrEmailInfo))@VineTV是的,在这种情况下,您可以删除等待!非常感谢。如果我需要将SendEmailAlert()方法设置为fire And forget,我可以删除Wait关键字并按如下方式使用它。这样做正确吗?----->运行(()=>sendmailalert(arrEmailInfo))@VineTV是的,在这种情况下,您可以删除等待!
public async Task MyMethod()
{
    DoSomething();

    try
    {   
        string emailBody = "TestBody";
        string emailSubject = "TestSubject";

        await Task.Run(()=> SendEmailAlert(arrEmailInfo));

        //Insert code to execute when SendEmailAlert is completed.
        //Be aware that the SynchronizationContext is not the same once you have resumed. You might not be on the main thread here
    }
    catch (Exception ex)
    {
        //Log error message
    }

}

private void SendEmailAlert(string[] arrEmailInfo)
{
    MyClassX.SendAlert(arrEmailnfo[0], arrEmailnfo[1]);
}