运行.exe时停止工作

时间:2012-11-01 08:37:07

标签: c# asynchronous exe

我在Visual Studio C#中有一个程序,当我从Visual Studio运行它时,它始终运行完美,但有时当我从.exe(双击)运行它时,它无法正常工作。有时它会停止工作并显示一个消息框:

  

问题导致程序无法正常工作。 Windows会   关闭程序并在解决方案可用时通知您

有时它不会进入我的异步功能。我认为它与我的异步功能有关,但我不知道该怎么做。

这是我班上的异步代码:

for (int i = 0; i < dtLP.Rows.Count; i++)
{
    WebClient client = new WebClient();
    client.Encoding = Encoding.UTF8;
    client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
    client.DownloadDataAsync(new Uri(dtLP.Rows[i]["lp_url"].ToString()), i);
    Log.Insert(i+"---"+dtLP.Rows[i]["lp_url"]);
}
while (end)
{
    Thread.Sleep(5);
}

void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    try
    {
        int h = (int)e.UserState;
        Encoding enc = Encoding.UTF8;
        string myString = "";
        int i;
        try
        {
            myString = enc.GetString(e.Result);
            if (myString.Contains('�') || myString.Contains('?'))
            {//UserId with special character: class-one, 14,(433: ascii, need to be iso-8859-1),
                Match m = new Regex(@"<meta\s+.*?charset\s*=\s*(?<charset>[A-Za-z0-9_-]+)", RegexOptions.Singleline | RegexOptions.IgnoreCase).Match(myString);
                if (m.Success)
                {
                    enc = Encoding.GetEncoding(m.Groups["charset"].Value.ToLower() ?? "iso-8859-1");
                    myString = enc.GetString(e.Result);
                }

                for (i = 0; i < Pharsing.myEncoding.Length && (myString.Contains('�') || myString.Contains("??")); i++)
                {
                    enc = Encoding.GetEncoding(Pharsing.myEncoding[i]);
                    myString = enc.GetString(e.Result);
                }

                if (i == Pharsing.myEncoding.Length)
                {//there is � or ? so encoding just without �.
                    for (i = 0; i < Pharsing.myEncoding.Length && myString.Contains('�'); i++)                
                    {
                        enc = Encoding.GetEncoding(Pharsing.myEncoding[i]);
                        myString = enc.GetString(e.Result);
                    }
                }
                myString = Pharsing.CreateCorrectHtmlDoc(myString);
           }
          catch
          {
              myString = "";
          }

       //lock (matUrls[h])
       //{
       matUrls[h] = myString;
       //}
       Log.Insert(h.ToString());
       pages--;
       if (pages == 0)
       {
           Log.Insert("pages == 0");
           end = false;
       }

    }
    catch
    {
        Log.Insert("Error: asynd url");
    }
}

P.S:trycatch块中的所有代码都不是问题。

0 个答案:

没有答案