如何从创建文本框的线程以外的线程读取文本框

本文关键字:线程 读取 取文本 文本 创建 | 更新日期: 2023-09-27 18:00:46

我对C#还很陌生,我已经编写了几个正常运行的程序,但它们都是单线程应用程序。这是我的第一个多线程应用程序,我正在努力解决"跨线程操作无效:从创建它的线程以外的线程访问控件'cbLogType'"错误。我的应用程序在Windows事件查看器中搜索用户定义事件日志源(cbLogType)中的用户定义事件ID。我正在使用后台工作进程来完成所有工作,并且我正在使用worker.reportprogress来更新标签,但是,我在调试时收到了上述错误。我尝试了几个Invoke方法,但似乎没有一个能解决我的错误。我还尝试过删除组合框并直接在代码中设置日志源,这在一定程度上有效,但仍然失败。我已经包含了我的代码,任何帮助都将不胜感激。我怀疑我可能没有正确使用Invoke方法。提前感谢!代码:

    private void bgWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        {
            if (File.Exists(@"C:'Events.log"))
                MessageBox.Show("File 'Events.log' already exists. All new data will be appended to the log file!", "Warning!");
            string message = string.Empty;
            string eventID = (tbEventID.Text);
            string text;
            EventLog eLog = new EventLog();
            Invoke((MethodInvoker)delegate() { text = cbLogType.Text; });
            eLog.Source = (this.cbLogType.Text); // I am receiving the error here
            eLog.MachineName = ".";
            int EventID = 0;
            string strValue = string.Empty;
            strValue = tbEventID.Text.Trim();
            //string message = string.Empty;
            EventID = Convert.ToInt32(strValue); // Convert string to integer
            foreach (EventLogEntry entry in eLog.Entries)
            {
                int entryCount = 1;
                if (cbDateFilter.Checked == true)
                {
                    if (entry.TimeWritten > dtPicker1.Value && entry.TimeWritten < dtPicker2.Value)
                        if (entry.InstanceId == EventID)
                            message = "Event entry matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
                    using (StreamWriter writer = new StreamWriter(@"C:'Events.log", true))
                        writer.WriteLine("EventID: " + entry.InstanceId +
                            "'r'nDate Created: " + entry.TimeWritten +
                            "'r'nEntry Type: " + entry.EntryType +
                            "'r'nMachinename: " + entry.MachineName +
                            "'r'n" +
                            "'r'nMessage: " + entry.Message +
                            "'r'n");
                    if (entry.InstanceId != EventID)
                        message = "No event ids matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
                }
                else
                {
                    if (cbDateFilter.Checked == false)
                    {
                        if (entry.InstanceId == EventID)
                            using (StreamWriter writer = new StreamWriter(@"C:'Events.log", true))
                                writer.WriteLine("EventID: " + entry.InstanceId +
                                    "'r'nDate Created: " + entry.TimeWritten +
                                    "'r'nEntry Type: " + entry.EntryType +
                                    "'r'nMachinename: " + entry.MachineName +
                                    "'r'n" +
                                    "'r'nMessage: " + entry.Message +
                                    "'r'n");
                        else if (entry.InstanceId != EventID)
                            message = "No event ids matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
                    }
                    bgWorker1.ReportProgress((entryCount) * 10, message);
                    entryCount++;
                }
            }
        }
    }
    private void bgWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lblStat.Text = e.UserState.ToString();
    }

如何从创建文本框的线程以外的线程读取文本框

您正在非UI线程中访问cbLogType。

更改为

eLog.Source = text;