System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://vol.stock.hexun.com/idio_06.aspx?stockcode=600000#bb"); 
for (int i = 600000; i < 600003; i++)
            {
                    int j = i + 1;
                    System.Threading.Thread.Sleep(8000);
                    SendKeys.Send("^(a)");
                    SendKeys.Send("^(c)");                  
                    IDataObject sg = Clipboard.GetDataObject();
                    String nsg = (String)sg.GetData(DataFormats.Text);
                    File.WriteAllText("D:\\上海\\" + i + ".txt", nsg);
                    SendKeys.Send("%(d)");
                    SendKeys.Send("http://vol.stock.hexun.com/idio_06.aspx?stockcode=" + j + "#bb");
                    SendKeys.Send("{Enter}");
                
            }
当for (int i = 600000; i < 600001; i++)时循环只运行一次程序可以正常运行,但当for (int i = 600000; i < 600003; i++)时循环多于一次程序运行到第一个SendKeys.Send就会死掉。

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Runtime.InteropServices.ComTypes;
    using System.IO;
    using System.Windows.Forms;
    namespace ConsoleApplication1
    {
        class Test
        {
            static void Main()
            {
                System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://vol.stock.hexun.com/idio_06.aspx?stockcode=600000#bb");
                for (int i = 600000; i < 600001; i++)
                {
                    int j = i + 1;
                    System.Threading.Thread.Sleep(8000);
                    SendKeys.Send("^(a)");
                    SendKeys.Send("^(c)");
                    System.Windows.Forms.IDataObject sg = Clipboard.GetDataObject();
                    String nsg = (String)sg.GetData(DataFormats.Text);
                    File.WriteAllText("D:\\上海\\" + i + ".txt", nsg);
                    SendKeys.Send("%(d)");
                    SendKeys.Send("http://vol.stock.hexun.com/idio_06.aspx?stockcode=" + j + "#bb");
                    SendKeys.Send("{Enter}");
                } 
            }
        }
    }Unhandled Exception: System.InvalidOperationException: SendKeys cannot run insid
    e this application because the application is not handling Windows messages.  Ei
    ther change the application to handle messages, or use the SendKeys.SendWait met
    hod.
       at System.Windows.Forms.SendKeys.Send(String keys, Control control, Boolean w
    ait)
       at System.Windows.Forms.SendKeys.Send(String keys)
       at ConsoleApplication1.Test.Main() in Visual Stud
    io 2008\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 20
    Press any key to continue . . .怎么回事?
      

  2.   

    不知道,我用vc2005行的啊,只不过我的这段代码是放在Button按钮里的