线程和GUI在C#中的元素 [英] Threading and GUI elements in C#

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

问题描述

我试图做一个基本的IRC客户端...但我的问题越来越离不开它滞后



$ B $在RTF框中显示的文本BI解决使用线程,我要更新一个线程RTF中,但我不能,因为它给了我错误的约不是静态的RTF框元素?




任何见解?我将粘贴代码,如果你们希望它

i am trying to make a basic IRC client...but my problem is getting the text to display in a RTF box without it lagging

i settled on using threading, and i want to update the RTF box in a thread, but i cant because it gives me errors about the RTF box element not being static?

any insight? i will paste code if you guys want it



确定这里是代码(做编辑撞?)


ok here is the code (do edits bump?)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net;

namespace IrcClient
{
    public partial class mainWindow : Form
    {
        static IRC client;
        static string newLine, oldLine;

        public mainWindow()
        {
            InitializeComponent();
        }

        private void main()
        {

        }

        private void mainWindow_Load(object sender, EventArgs e)
        {
            client = new IRC("irc.freenode.net" ,6667, "jimi__hendrix");
            new Thread(new ThreadStart(update)).Start();

        }

        private static void update()
        {
            newLine = client.sr.ReadLine();

            Thread.Sleep(50);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            client.privmsg(inputBox.Text);
            messageBox.AppendText(inputBox.Text + "\n");
            inputBox.Text = "";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (newLine != oldLine)
            {
                messageBox.AppendText(newLine + "\n");
                oldLine = newLine;
            }
        }
    }

    class IRC
    {
        TcpClient connection;
        NetworkStream stream;
        public StreamWriter sw;
        public StreamReader sr;
        string nick;

        public IRC(string server, int port, string Nick)
        {
            try
            {
                connection = new TcpClient(server, port);
                stream = connection.GetStream();
                sr = new StreamReader(stream);
                sw = new StreamWriter(stream);
                nick = Nick;
                sw.WriteLine("PASS " + "caruso11");
                sw.Flush();
                sw.WriteLine("NICK " + nick);
                sw.Flush();
                sw.WriteLine("USER Jimi__Hendrix 8 irc.freenode.net :Jimi__Hendrix");
                sw.Flush();
            }

            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void privmsg(string msg)
        {
            sw.WriteLine(msg);
            sw.Flush();
        }

        public void parse(string msg)
        {

        }
    }
}



的一些方法是空白的,有些代码可以清理,但我希望得到这第一个完成了...还,那里有,设置了窗口


some of the methods are blank and some code could be cleaned up, but i want to get this done first...also, theres the code generated by visual studios that sets up the window

推荐答案

在一般的视觉工作室生成的代码,试图更新从其他线程控件比主线程将无法正常工作,由于Windows的限制。如果你需要调用一个方法或从您的工作线程设置的RTF框的属性,你可能需要使用的调用或的 BeginInvoke的。这可能如下所示:

In general, trying to update a Control from a thread other than the main thread will not work, due to limitations of Windows. If you need to call a method or set a property on the RTF box from your worker thread, you'll probably need to use Invoke or BeginInvoke. This might look like the following:

void MyThreadMethod()
{
    // stuff
    this.rtfBox.BeginInvoke( (MethodInvoker)(()=> this.rtfBox.Text = "foo") );
    // stuff
}

修改:正如其他人所指出的,如果这实际上是不能的编译的因担忧不是静态控制错误,你可能想从静态函数,这是违法的引用一个成员变量。张贴代码将有助于缩小问题。

Edit: As others point out, if this is actually failing to compile due to an error about the control not being static, you're probably trying to reference a member variable from a static function, which is illegal. Posting code will help narrow down the problem.

这篇关于线程和GUI在C#中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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