Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用参数调用方法_C#_Task_Invoke - Fatal编程技术网

C# 使用参数调用方法

C# 使用参数调用方法,c#,task,invoke,C#,Task,Invoke,我正在用C#和.NET Framework 4.0开发一个Windows窗体应用程序 我正在使用Task运行一个长时间运行的任务,每次任务处理代码时,我都需要用一些日志消息更新UI 有一个处理该代码的队列,我需要显示代码已被处理 private Task taskReadCodeAuto; private delegate void RefreshTextBox(); private Queue CodesReceived; public MainForm() { Initiali

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

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

有一个处理该代码的队列,我需要显示代码已被处理

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);
}
私有任务taskReadCodeAuto;
私有委托无效刷新文本框();
接收到私有队列代码;
公共表格(
{
初始化组件();
logMessages=新列表();
CodesReceived=新队列();
taskReadCodeAuto=新任务(()=>ProcessCodesReceived());
}
私有void ProcessCodesReceived()
{
int结果;
尝试
{
而(CodesReceived.Count>0)
{
字符串代码=CodesReceived.Dequeue().ToString();
InsertProfileMessage(DateTime.Now.ToString(“HH:mm:ss.fff”)、string.Format(“将代码{0}发送到ReadCodeAuto…”,code));
如果(trzic==null)
{
结果=
TRZIC.ReadCodeAuto(
ConnStringTextBox.Text,
byte.Parse(AggregationNumeric.Value.ToString()),
代码);
}
其他的
{
结果=
trzic.ReadCodeAuto(
byte.Parse(AggregationNumeric.Value.ToString()),
代码);
}
InsertProfileMessage(DateTime.Now.ToString(“HH:mm:ss.fff”)、string.Format(“代码发送{0}。结果:{1}”,代码、结果));
}
}
捕获(例外情况除外)
{
插入个人资料信息(DateTime.Now.ToString(“HH:mm:ss.fff”),“;
InsertProfileMessage(DateTime.Now.ToString(“HH:mm:ss.fff”),“Error:+ex.Message”);
插入个人资料信息(DateTime.Now.ToString(“HH:mm:ss.fff”),“;
}
最后
{
InsertProfileMessage(DateTime.Now.ToString(“HH:mm:ss.fff”),“END BG-WORKER”);
}
}
私有void InsertProfileMessage(字符串时间,字符串消息)
{
字符串配置文件字符串=
格式(“{0}-{1}”,时间,消息);
添加(profileString);
if(this.invokererequired)
{
RefreshTextBox d=新的RefreshTextBox(RefreshTextBox结果);
调用(d);
}
其他的
{
RefreshTextBoxResults(profileString+“\n”);
}
}
私有void RefreshTextBoxResults(字符串文本)
{
LogTextBox.AppendText(文本);
}
我的问题是,我不知道如何使用
Invoke
传递要在
LogTextBox
上显示的文本


我该怎么做?

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

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

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

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

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


按如下方式传递文本值

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

你是否删除了我为你提供的另一个问题??你的代码将崩溃。您忘记传递要显示的文本。使用Invoke(d,profileString);
RefreshTextBox d = new RefreshTextBox(RefreshTextBoxResults);
Invoke(d,new object[] {“Pass value here”});