如何将回调字符串用于mainform

时间:2014-11-12 11:24:15

标签: c# .net

我有一个用c#

编写的方法
class TorrentDownload
{
    private readonly Thread _getstateThread;        
    public Sohadownload() {
        _getstateThread = new Thread(GetState);
        _getstateThread.Start();            
    }

    TorrentHandle handle=null;
    public void Start(AddTorrentParams param)
    {
        var _session = new Session();            
        if (_session == null) throw new ArgumentNullException("sesion");
        _session.ListenOn(6881, 6889);
        handle = _session.AddTorrent(param);                                 
    }

        public void GetState(){
        while (true)
        {
            if (handle == null)
            {
                Thread.Sleep(1000);
                continue;
            }
            var status = handle.GetStatus();
            if (status.IsSeeding)
            {
                break;
            }                
            string _state = status.State.ToString();
            double _up = status.UploadRate;
            double _down = status.DownloadRate;
            double _process = status.Progress;
            Thread.Sleep(1000);
            //Console.WriteLine("State {0} Process {1}", _state, _process);            
       }
    }        
}

现在在Main()计划中我只想在_state_up_dơn_process变量中调用值以显示在Console.Writeline中 我正在尝试使用委托,但它没有工作。

请帮帮我,谢谢。

3 个答案:

答案 0 :(得分:2)

根据您的要求,这里有一个想法

构造一个类,它将管理状态并创建公共属性,以使您能够访问变量值

您可以在主要功能中拨打GetState,然后就可以访问_state_up_down_process

如果需要,您可以将您的属性和方法以及类作为static,但这里是示例代码:

public class ManageStatus
{
    private string _state;
    private double _up,_down,_process;

    public string State
    {
        get{ return _state;}
        set{ _state=value;}
    }
    public string State
    {
        get{ return _state;}
        set{ _state=value;}
    }
    public double Up
    {
        get{ return _up;}
        set{ _up=value;}
    }
    public double Down
    {
        get{ return _down;}
        set{ _down=value;}
    }
    public double Process
    {
        get{ return _process;}
        set{ _process=value;}
    }
    public void GetState()
    {
        while (true)
        {
            if (handle == null)
            {
                Thread.Sleep(1000);
                continue;
            }
            var status = handle.GetStatus();
            if (status.IsSeeding)
            {
                break;
            }                
            _state = status.State.ToString();
            _up = status.UploadRate;
            _down = status.DownloadRate;
            _process = status.Progress;
            Thread.Sleep(1000);
         }  
    }
}

在您的主要代码中,您可以执行此操作

public static void Main()
{
     var status=new ManageStatus();
     status.GetState();
     Console.Writline("State:{0}",status.State);
     Console.Writline("Up:{0}",status.Up);
     Console.Writline("Down:{0}",status.Down);
     Console.Writline("Process:{0}",status.Process);
}

希望这会对你有所帮助

答案 1 :(得分:2)

您可以在GetState()内而不是Main打印该行:

public void GetState() 
{ 
    while (true)
    {

        if (handle == null)
        {
            Thread.Sleep(1000);
            continue;
        }

        var status = handle.GetStatus();

        if (status.IsSeeding)
        {
            break;
        }

        Console.WriteLine("State: {0}, Up: {1}, Down: {2}, Process: {3}",
                          status.State.ToString(),
                          status.UploadRate,
                          status.DownloadRate,
                          status.Progress);

        Thread.Sleep(1000);
    }  
}

答案 2 :(得分:0)

除了我的其他答案,您可以修改GetState并将循环移至Main

// N.B: I don't know where you get handle from,
//      I'll just adapt the code you've posted for simplicity
public WhateverTypeStatusIs GetState()
{
    if (handle == null)
    {
        Thread.Sleep(1000);
        continue;
    }
    var status = handle.GetStatus();
    if (status.IsSeeding)
    {
        break;
    }                

    return status;
}

public static void Main()
{
    while (true)
    {
        var status = GetState();

        if (status.IsSeeding)
        {
            break;
        }

        Console.WriteLine("State: {0}, Up: {1}, Down: {2}, Process: {3}",
                          status.State.ToString(),
                          status.UploadRate,
                          status.DownloadRate,
                          status.Progress);

        Thread.Sleep(1000);
    }
}

当然,Main并没有做太多事情并且阻止了GetState()个单独的回复,但鉴于我们对您的计划了解不多,我认为它可以作为一个体面的例子。

我还假设handle.GetStatus()没有返回null