在C#Winforms中调用类 [英] Invoke in Class in c# winforms

查看:69
本文介绍了在C#Winforms中调用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在类中运行的线程,该线程需要更新文本框的值,但调用不会出现在类中.

I have a thread running in a class that needs to update value of textbox but invoke does not appear in class.

有什么想法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using CheckedBoxSerpCrawler;
namespace SERP_Crawler
{   
    class Crawl
    {
        public Crawl()
        {              

        var t = new Thread(() =>
              {
                 for (int i = 2; i < (pagesToScroll / 10); i++)
                  {
                        //Here i need to invoke CheckUrlList
                  } 
              });
        t.SetApartmentState(ApartmentState.STA);
        t.Start();          

        }//End Constructor
    private void CheckUrlList(object sender, EventArgs e)
    {
        //Here update textbox
    }
}
}

推荐答案

IntelliSense可以毫无问题地显示此内容.我不知道你有什么问题?

IntelliSense shows this without any problems. I don't know what problem you have?

        TextBox test = new TextBox();
        if (test.InvokeRequired)
        {
            test.BeginInvoke(...);
        }

上述内容应该可以使用,也许您应该看看 http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx

The above mentioned should work, maybe you should have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx

为了澄清一些问题- test 在这里是您的TextBox,应更改其值.如果您没有访问类中的TextBox的权限,那么我喜欢使用事件来启动gui-control-class中的方法.但这是另一个故事;)

To clarify a few things - testis here your TextBox which value is ought to be changed. If you don't have access to the TextBox in your class, I like using events to start a method in your gui-control-class. But that's another story ;)

这篇关于在C#Winforms中调用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆