如何将值从一个cs文件(void!)传递给另一个?

时间:2014-03-26 07:12:09

标签: c# xml json winforms

我的小程序工作得很好,读取带有3个配置值的xml文件,读取json-http页面等等。 现在有一件事我想做:在我的设置对话框中显示xml中的值。我得到了这个(Program.cs):

using ...

//*****************************************************************************
namespace t2bl
{
abstract class TANSS2BL
{



    public static NotifyIcon notico;


    //==========================================================================


    public static void Main(string[] astrArg)
    {
        ContextMenu cm;
        MenuItem miCurr;

        cm = new ContextMenu();

        miCurr = new MenuItem();
        miCurr.Index = 0;
        miCurr.Text = "&Settings";
        miCurr.Click += new System.EventHandler(SettingsClick);
        cm.MenuItems.Add(miCurr);

        miCurr = new MenuItem();
        miCurr.Index = 1;
        miCurr.Text = "Beenden";
        miCurr.Click += new System.EventHandler(ExitClick);
        cm.MenuItems.Add(miCurr);

        notico = new NotifyIcon();
        notico.Icon = new Icon("tanss.ico");
        notico.Text = "TANSS Busylight Connector";
        notico.Visible = true;
        notico.ContextMenu = cm;
        notico.DoubleClick += new EventHandler(NotifyIconDoubleClick);




        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();





        Application.Run();




    }

    public static void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("C://bll.config.xml");

        XmlNode xmlurl = doc.SelectSingleNode("/settings/url");
        string url = xmlurl.FirstChild.Value;
        XmlNode xmluid = doc.SelectSingleNode("/settings/userID");
        string userid_string = xmluid.FirstChild.Value;
        XmlNode xmlrefresh = doc.SelectSingleNode("/settings/refresh");
        string refresh_string = xmlrefresh.FirstChild.Value;

        int userid = Convert.ToInt32(userid_string);
        int refresh = 1000 * (Convert.ToInt32(refresh_string));

        var controller = new BusylightUcController();


        while (true)
        {
            WebClient client = new WebClient();
            var downloadString = client.DownloadString(url + "/module/busylight.php?    user=" + userid);

            JObject colors = JObject.Parse(downloadString);
            //Console.WriteLine(colors["color"]); //Debug
            var colorStatus = Convert.ToInt32(colors["color"]);

            switch (colorStatus)
            {
                check the colors and set the light
            }
            Thread.Sleep(refresh);
            GC.Collect();
        }
    }



    //==========================================================================
    protected static void ExitClick(Object sender, EventArgs e)
    {
        notico.Dispose();
        Application.Exit();
    }

    //==========================================================================
    protected static void SettingsClick(Object sender, EventArgs e)
    {

        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>

        // This should open the "Settings"-Popup, containing 3 textboxes and a button to    save them to xml.
        SettingsDiag form1 = new SettingsDiag();
        form1.Show();
    }

    //==========================================================================
    protected static void NotifyIconDoubleClick(Object sender, EventArgs e)
    {
        // ...
    }



}
}

另一方面(Form1.Designer.cs):

namespace Settingsdialog
{
    partial class SettingsDiag
    {

    /// <summary>
    /// Erforderliche Designervariable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Verwendete Ressourcen bereinigen.
    /// </summary>
    /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Vom Windows Form-Designer generierter Code

    /// <summary>
    /// Erforderliche Methode für die Designerunterstützung.
    /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    /// </summary>
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(50, 28);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(163, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "TANSS Busylight connector v1.0";
        this.label1.Click += new System.EventHandler(this.label1_Click);
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(53, 65);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(159, 20);
        this.textBox1.TabIndex = 1;
        this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(271, 117);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label1);
        this.Name = "Form1";
        this.Text = "TANSS Busylight connector";
        this.ResumeLayout(false);
        this.PerformLayout();

    }


    #endregion

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;

}

}

如何将值“url”,“userid_string”,“refresh_string”从backgroundworker void传递给“textbox1.Text =”输出?

谢谢!

2 个答案:

答案 0 :(得分:0)

如果你想与你的表单沟通后台工作者的结果,你可以将你的表单定义为TANSS2BL的成员(并在Main中初始化它)。 然后在bw_DoWork中,您可以使用要显示为参数的信息在表单上调用方法。

由于bw_DoWork在另一个线程上,所以在设置textBox值之前不要忘记在表单中测试InvokeRequired和BeginInvoke。

答案 1 :(得分:0)

有很多方法可以实现这一点,你在其他答案中有一个,我可以建议另一个: 因为您的worker被设置为运行异步,所以您可以使用worker事件报告更新进度,然后在处理程序中更新文本框

bw.WorkerReportsProgress = true;
bw.ProgressChanged += (o, args) => { textBox1.Text = args.UserState.ToString();}

很明显,在bw.DoWork事件处理程序中,您需要报告将UserState作为要报告和更新的字符串值传递的进度操作,因此在事件处理程序中需要调用

bw.ReportProgress(0, "you value to report");

希望这会有所帮助

相关问题