2012-01-10 53 views
1

我得到这个错误如何在threadstart中输出字符串?

预期的方法名称。

此代码:

thSystem[index] = new Thread(new ThreadStart(cls.StartProcess(out string))); 

如何做产值过载?

我想从StartProcess中取出字符串。

下面是我的代码:

//声明功能

 clsCallProcess cls = new clsCallProcess(index, url, name, timer); 
     thSystem[index] = new Thread(new ThreadStart(cls.StartProcess(out string))); 
     thSystem[index].Start(); 

//功能

 public class clsCallProcess 
    { 
     private int mindex; 
     private string murl; 
     private string mname; 
     private int mtimer; 
     bool IsRunning = true; 

     // The constructor obtains the state information. 
     public clsCallProcess(int index, string url, string name, int timer) 
     { 
      mindex = index; 
      murl = url; 
      mname = name; 
      mtimer = timer; 
     } 

     public void StartProcess(out string result) 
     { 
      //First run checking 
      result = "Connection Success"; 
      while (IsRunning) 
      { 
       Thread.Sleep(mtimer); 
       IsRunning = false; 

       try 
       { 
        //FileManager.WriteActivityLog("Process Start", mname); 
        DateTime start = DateTime.Now; 
        string html = Utility.RequestWebpage(murl, string.Empty, true); 
        TimeSpan minisecond = DateTime.Now - start; 

        FileManager.WriteActivityLog("time:" + minisecond.Milliseconds.ToString() + ",html length:" + html.Length.ToString(), mname); 
        //FileManager.WriteActivityLog("html:" + html, mname); 
        //FileManager.WriteActivityLog("Process End", mname); 
       } 
       catch (Exception ex) 
       { 
        ExceptionManager.WriteErrorLog(ex.Message, true, mname); 
        result = ex.Message; 
       } 
       IsRunning = true; 
      } 


     } 
    } 

回答

1

显然,你在一个循环中运行的东西永远(现在你的线程循环将永远结束为IsRunning将始终为真),以便定期检查某个网页。

获取定期结果的一种方法是事件处理程序,每次尝试连接时都会引发该事件处理程序。它看起来是这样的:

public class PageWatcher 
{ 
    private int mindex; 
    private string murl; 
    private string mname; 
    private int mtimer; 
    bool IsRunning = true; 

    public event EventHandler<ConnectionAttemptEventArgs> ConnectionAttempt; 

    // The constructor obtains the state information. 
    public PageWatcher(int index, string url, string name, int timer) 
    { 
     mindex = index; 
     murl = url; 
     mname = name; 
     mtimer = timer; 
    } 

    public void StartProcess() 
    { 
     //First run checking 
     string lastResult = "Connection Success"; 

     while (IsRunning) 
     { 
      Thread.Sleep(mtimer); 
      IsRunning = false; 

      try 
      { 
       //FileManager.WriteActivityLog("Process Start", mname); 
       DateTime start = DateTime.Now; 
       string html = Utility.RequestWebpage(murl, string.Empty, true); 
       TimeSpan minisecond = DateTime.Now - start; 

       FileManager.WriteActivityLog("time:" + minisecond.Milliseconds.ToString() + ", html length:" + html.Length.ToString(), mname); 
       //FileManager.WriteActivityLog("html:" + html, mname); 
       //FileManager.WriteActivityLog("Process End", mname); 
       lastResult = "Connection Success"; 
      } 
      catch (Exception ex) 
      { 
       ExceptionManager.WriteErrorLog(ex.Message, true, mname); 
       lastResult = ex.Message; 
      } 
      IsRunning = true; 
      OnConnectionAttempt(result); 
     } 
    } 

    private void OnConnectionAttempt(string result) 
    { 
     var handler = ConnectionAttempt; 
     if (handler != null) 
     { 
      handler(this, new ConnectionAttemptEventArgs(mindex, result)); 
     } 
    } 
} 


public class ConnectionAttemptEventArgs : EventArgs 
{ 
    public readonly int Index; 
    public readonly string Result; 

    public ConnectionAttemptEventArgs(int index, string result) 
    { 
     Index = index; 
     Result = result; 
    } 
} 

然后使用它是这样的:

我也改名为你的类作为clsCallProcess是不是一个真正的好名字。用cls作为前缀以表明它是一个类是相当毫无意义的,并且CallProcess是它正在做的事情,但不是它的目的是什么 - 这是观看与网页的连接。

+0

我想这代码是不是为我工作,有其他的方式? – user983738 2012-01-10 05:27:16

+0

@ user983738:你是什么意思的“不工作”? – ChrisWue 2012-01-10 05:31:57

+0

它的内容很不对我错误 公共ConnectionResultEventArgs,这1个不放的返回方法 – user983738 2012-01-10 05:33:27

1

有一个“继续”委托,取得结果。代表将在方法结束时被调用。

clsCallProcess cls = new clsCallProcess(index, url, name, timer); 
thSystem[index] = new Thread(new ThreadStart(
    () => 
    cls.StartProcess(result => Console.WriteLine (result)))); 
// ---------------------------^ 
// If you want to do something other than write it to the console, do that here. 
thSystem[index].Start(); 

这里的StartProcess(

public void StartProcess(Action<string> continueWith) 
    { 
     string result; 
     while (IsRunning) 
     { 
      // do stuff 
      result = "success"; 
     } 

     // all done... call continuation delegate. 
     continueWith (result); 
    } 

的框架的替代,也许这样做(如果你有.NET 4中),使用更加友好的方式:Continuations with Task Parallel Library

+0

如果我通过正常的方式调用这个函数,像这样 cls.StartProcess(); 我应该放回什么类型? – user983738 2012-01-11 08:42:41

+0

mean,cls.StartProcess(result => stringVariable = result)));? – user983738 2012-01-11 09:14:48