使用参数调用方法

时间:2014-04-25 09:46:29

标签: c# task invoke

我正在使用C#和.NET Framework 4.0开发Windows窗体应用程序。

我使用Task来运行长时间运行的任务,每次我的任务处理代码时,我都需要使用一些日志消息更新UI。

有一个Queue处理代码,我需要证明代码已被处理。

private Task taskReadCodeAuto;
private delegate void RefreshTextBox();

private Queue CodesReceived;


public MainForm()
{
    InitializeComponent();

    logMessages = new List<string>();

    CodesReceived = new Queue();

    taskReadCodeAuto = new Task(() => ProcessCodesReceived());
}

private void ProcessCodesReceived()
{
    int result;
    try
    {
        while (CodesReceived.Count > 0)
        {
            string code = CodesReceived.Dequeue().ToString();
            InsertProfileMessage(DateTime.Now.ToString("HH:mm:ss.fff"), string.Format("Sending code {0} to ReadCodeAuto...", code));
            if (trzic == null)
            {
                result =
                    TRZIC.ReadCodeAuto(
                        ConnStringTextBox.Text,
                        byte.Parse(AggregationNumeric.Value.ToString()),
                        code);
            }
            else
            {
                result =
                    trzic.ReadCodeAuto(
                        byte.Parse(AggregationNumeric.Value.ToString()),
                        code);
            }

            InsertProfileMessage(DateTime.Now.ToString("HH:mm:ss.fff"), string.Format("Code sent {0}. Result: {1}", code, result));
        }
    }
    catch (Exception ex)
    {
        InsertProfileMessage(DateTime.Now.ToString("HH:mm:ss.fff"), "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        InsertProfileMessage(DateTime.Now.ToString("HH:mm:ss.fff"), "Error: " + ex.Message);
        InsertProfileMessage(DateTime.Now.ToString("HH:mm:ss.fff"), "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    }
    finally
    {
        InsertProfileMessage(DateTime.Now.ToString("HH:mm:ss.fff"), "END BG-WORKER");
    }
}

private void InsertProfileMessage(string time, string message)
{
    string profileString =
            string.Format("{0} - {1}", time, message);
    logMessages.Add(profileString);

    if (this.InvokeRequired)
    {
        RefreshTextBox d = new RefreshTextBox(RefreshTextBoxResults);
        Invoke(d);
    }
    else
    {
        RefreshTextBoxResults(profileString + "\n");
    }
}

private void RefreshTextBoxResults(string text)
{
    LogTextBox.AppendText(text);
}

我的问题是,我不知道如何使用LogTextBox传递要在Invoke上显示的文字。

我该怎么做?

4 个答案:

答案 0 :(得分:2)

使用Invoke的重载,它将Object[]作为参数提供给您的方法。

答案 1 :(得分:0)

您可以在调用后添加参数:

        Action<string> d = RefreshTextBoxResults;
        this.Invoke(d, profileString + "\n");

调用已包含参数的操作(这种情况适用于可重用性)

Action d= () =>RefreshTextBoxResults(profileString + "\n");
if (this.InvokeRequired)
{
    Invoke(d);
}
else
{
    d();   
}

PS,如果要使用RefreshTextBox委托而不是Action,则应更改RefreshTextBox委托以包含字符串参数

答案 2 :(得分:0)

您必须使用使用数组的Invoke的重载:

MSDN documentation

答案 3 :(得分:0)

传递如下的文字值。

RefreshTextBox d = new RefreshTextBox(RefreshTextBoxResults);
Invoke(d,new object[] {“Pass value here”});