如何在threadstart中给出输出字符串

本文关键字:输出 字符串 threadstart | 更新日期: 2023-09-27 18:19:48

我收到这个错误

应为方法名称。

对于该代码:

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;
            }

        }
    }

如何在threadstart中给出输出字符串

显然,你永远在循环中运行一些东西(现在你的线程循环永远不会结束,因为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;
    }
}

然后这样使用:

PageWatcher watcher = new PageWatcher(index, url, name, timer);
watcher.ConnectionAttempt += HandleConnectionAttempt;
thSystem[index] = new Thread(new ThreadStart(watcher.StartProcess));
thSystem[index].Start();
...
void HandleConnectionAttempt(object sender, ConnectionAttemptEventArgs e)
{
     Console.WriteLine("Connection attempt watcher {0}, result {1}", e.Index, e.Result);
}

我还把你的课改名为clsCallProcess,这不是一个好名字。用cls作为前缀来表示它是一个类是毫无意义的,而CallProcess是它正在做的事情,而不是它的意图——即观看与网页的连接。

有一个获取结果的"continuation"委托。委托将在方法结束时调用。

 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