C# 更新主表单';外部类的进度条?

C# 更新主表单';外部类的进度条?,c#,class,progress-bar,C#,Class,Progress Bar,我有一个主窗体,上面有一个进度条,我想从一个名为“Logic”的外部类更新进度条。。。但是,在主窗体上已经引用了逻辑。如果我试图引用逻辑中的主窗体来更新进度条,我只会得到堆栈溢出 在四处搜索的过程中,我遇到了很多关于后台工作人员的话题。。。但这不是我想用的。我在我的逻辑类中有一些特定的位置,我想使用progressbar.PerformStep()更新主窗体上的进度条。我已经尝试在主窗体上创建一个方法来更新进度条,并从Logic类调用它,但它再次缺少引用。。。我不能只使用MainForm frm

我有一个主窗体,上面有一个进度条,我想从一个名为“Logic”的外部类更新进度条。。。但是,在主窗体上已经引用了逻辑。如果我试图引用逻辑中的主窗体来更新进度条,我只会得到堆栈溢出

在四处搜索的过程中,我遇到了很多关于后台工作人员的话题。。。但这不是我想用的。我在我的逻辑类中有一些特定的位置,我想使用progressbar.PerformStep()更新主窗体上的进度条。我已经尝试在主窗体上创建一个方法来更新进度条,并从Logic类调用它,但它再次缺少引用。。。我不能只使用MainForm frm1=newmainform(),而不在其他地方引起错误。我在这里感到很困惑

[编辑]

下面是解决方案的代码(感谢大家)----

主要形式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Natural_Language_Processor
{
public partial class frm_Main : Form

{
    Logic logic = new Logic();

    public frm_Main()
    {
        InitializeComponent();
    }

    private void frm_Main_Load(object sender, EventArgs e)
    {
        Timer.Start();
    }

    private void btn_Enter_Click(object sender, EventArgs e)
    {
        logic.Progress += new Logic.ProgressDelegate(DisplayProgess);
        logic.RaiseProgress(0);

        logic.str_Input = txt_Input.Text;
        logic.Prep_Input();

        txt_Input.Text = "";
        logic.RaiseProgress(100);
        System.Threading.Thread.Sleep(100);
        logic.RaiseProgress(0);
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void eraseToolStripMenuItem_Click(object sender, EventArgs e)
    {
        logic.EraseMemory();
    }

    public void DisplayProgess(int percent)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new Logic.ProgressDelegate(DisplayProgess), new Object[] { percent });
        }
        else
        {
            this.progbar.Value = percent;
        }
    }
}
逻辑:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Natural_Language_Processor
{
class Logic
{
    Data oData = new Data();
    public List<string> Words = new List<string>();

    private System.Threading.Thread T = null;

    public delegate void ProgressDelegate(int percent);
    public event ProgressDelegate Progress;

    #region Variables
        public string str_Input;
        public string[] WordArray;
    #endregion

    public void RaiseProgress(int percent)
    {
        if (Progress != null)
        {
            Progress(percent);
        }
    }

    public void Prep_Input()
    {
        //Check for Input
        if (String.IsNullOrEmpty(str_Input))
        {

        }
        else
        {
            //Set everything to lower-case
            str_Input = str_Input.ToLower();
            RaiseProgress(10);

            //Remove all punctuation
            if (str_Input.Contains(","))
            {
                while (str_Input.Contains(","))
                {
                    int int_index = str_Input.IndexOf(",");
                    str_Input = str_Input.Remove(int_index, 1);
                }
            }
            if (str_Input.EndsWith("."))
            {
                str_Input = str_Input.Trim('.');
            }
            else if (str_Input.EndsWith("?"))
            {
                str_Input = str_Input.Trim('?');
            }
            RaiseProgress(20);

            //Split the sentence into an array of individual words
            WordArray = str_Input.Split(' ');
            RaiseProgress(30);

            //Get current words (and max ID) from the database
            int max_index = 0;
            oData.GetWords();
            Words.Clear();

            if (oData.WordDataSet.Count > 0)
            {
                for (int i = 0; i < oData.WordDataSet.Count; i++)
                {
                    max_index = oData.WordDataSet[i].ID;
                    Words.Add(oData.WordDataSet[i].Word);
                }
            }
            RaiseProgress(40);

            //Check each word in the sentence
            for (int i = 0; i < WordArray.Length; i++)
            {
                //Update the frequency of an existing word in the database
                if (Words.Contains(WordArray[i].ToString()))
                {
                    oData.UpdateWords(WordArray[i].ToString());
                }
                else
                {
                    //Or add the word
                    max_index = max_index + 1;
                    oData.InsertWordsTable(max_index, WordArray[i].ToString(), 1);

                    //And create its pre/pro word tables
                    oData.NewPreWordTable(WordArray[i].ToString());
                    oData.NewProWordTable(WordArray[i].ToString());
                }
            }
            RaiseProgress(50);

            //Check each word in the sentence after we have possibly created new pre/pro word tables in the previous code
            for (int i = 1; i < WordArray.Length; i++)
            {
                oData.GetPreWords(WordArray[i].ToString());
                Words.Clear();

                //Get current pre_words from the database
                for (int a = 0; a < oData.WordDataSet.Count; a++)
                {
                    Words.Add(oData.WordDataSet[a].Word);
                }

                //Update the frequency of an existing word in the database
                if (Words.Contains(WordArray[i - 1].ToString()))
                {
                    oData.UpdatePreWords(WordArray[i].ToString(), WordArray[i - 1].ToString());
                }
                else
                {
                    //Or add the word
                    oData.InsertPreWord(WordArray[i].ToString(), oData.GetPreWordIndex(WordArray[i].ToString()), WordArray[i - 1].ToString(), 1);
                }

                if (i == WordArray.Length - 1)
                {

                }
                else
                {
                    oData.GetProWords(WordArray[i].ToString());
                    Words.Clear();

                    //Get current pro_words from the database
                    for (int b = 0; b < oData.WordDataSet.Count; b++)
                    {
                        Words.Add(oData.WordDataSet[b].Word);
                    }

                    //Update the frequency of an existing word in the database
                    if (Words.Contains(WordArray[i + 1].ToString()))
                    {
                        oData.UpdateProWords(WordArray[i].ToString(), WordArray[i + 1].ToString());
                    }
                    else
                    {
                        //Or add the word
                        oData.InsertProWord(WordArray[i].ToString(), oData.GetProWordIndex(WordArray[i].ToString()), WordArray[i + 1].ToString(), 1);
                    }
                }
            }
            RaiseProgress(60);
        }
    }

    public void Respond()
    {
        RaiseProgress(70);
    }

    public void EraseMemory()
    {
        oData.GetWords();
        Words.Clear();
        for (int i = 0; i < oData.WordDataSet.Count; i++)
        {
            oData.DeletePreTable(oData.WordDataSet[i].Word);
            oData.DeleteProTable(oData.WordDataSet[i].Word);
        }
        oData.DeleteWordsTable();
        MessageBox.Show("Memory has been erased.");
    }
}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统数据;
使用System.Data.SqlClient;
使用System.Windows.Forms;
名称空间自然语言处理器
{
类逻辑
{
数据oData=新数据();
公共列表词=新列表();
private System.Threading.Thread T=null;
公共委托无效进度委托(整数百分比);
公共活动进度代表进度;
#区域变量
公共字符串stru_输入;
公共字符串[]字数组;
#端区
公共无效提升进度(整数百分比)
{
如果(进度!=null)
{
进度(百分比);
}
}
公共void Prep_输入()
{
//检查输入
if(String.IsNullOrEmpty(str_输入))
{
}
其他的
{
//将所有内容设置为小写
str_Input=str_Input.ToLower();
提高进度(10);
//删除所有标点符号
if(str_Input.Contains(“,”))
{
while(str_Input.Contains(“,”))
{
int_index=str_Input.IndexOf(“,”);
str_Input=str_Input.Remove(int_索引,1);
}
}
if(str_Input.EndsWith(“.”)
{
str_Input=str_Input.Trim('.');
}
else if(str_Input.EndsWith(“?”))
{
str_Input=str_Input.Trim(“?”);
}
提高进度(20);
//把句子分成一组单词
WordArray=str_Input.Split(“”);
提高进度(30);
//从数据库中获取当前单词(和最大ID)
int max_指数=0;
oData.GetWords();
单词。清除();
如果(oData.WordDataSet.Count>0)
{
对于(int i=0;ipublic partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Logic logic = new Logic();
        logic.Progress += new Logic.ProgressDelegate(DisplayProgess);
        logic.Start();
    }

    public void DisplayProgess(string message, int percent)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new Logic.ProgressDelegate(DisplayProgess), new Object[] { message, percent });
        }
        else
        {
            this.label1.Text = message;
            this.progressBar1.Value = percent;
        }
    }

}

public class Logic
{

    private System.Threading.Thread T = null;

    public delegate void ProgressDelegate(string message, int percent);
    public event ProgressDelegate Progress;

    public void Start()
    {
        if (T == null)
        {
            T = new System.Threading.Thread(new System.Threading.ThreadStart(Worker));
            T.Start();
        }
    }

    private void Worker()
    {
        RaiseProgress("Initializing...", 0);
        System.Threading.Thread.Sleep(1000); // simulated work

        RaiseProgress("Loading Map...", 25);
        System.Threading.Thread.Sleep(1500); // simulated work

        RaiseProgress("Loading Sprites...", 50);
        System.Threading.Thread.Sleep(1200); // simulated work

        RaiseProgress("Loading Sound Effects...", 75);
        System.Threading.Thread.Sleep(1700);

        RaiseProgress("Loading Music...", 85);
        System.Threading.Thread.Sleep(1100); // simulated work

        RaiseProgress("Done!", 100);
    }

    private void RaiseProgress(string message, int percent)
    {
        if (Progress != null)
        {
            Progress(message, percent);
        }
    }

}