In C# (Net4.5, Win10) I wrote an email checker plugin. It handles Gmail/POP/IMAP. It works. But the plugin often freezes the app for a long time, if there's a connection problem etc. In C#, how can I "wrap" (delegate to a separate thread?) the GetEmails() function so that 1) it doesn't perceptibly "freeze" the rest of the app, 2) times out if it can't do its job within ~5 sec?
private static void GetEmails(int i) {
    // if Gmail
    cred = new NetworkCredential(user,password);
    request = (HttpWebRequest)WebRequest.Create(atomURL);
    try {
        response = (HttpWebResponse)request.GetResponse();
        ...
    // if POP
    try {
        pop.Connect();
        ...
    // if IMAP
    try {
        imap.Connect();
        ...
}
 
    