Tråde og forms

Tags:    c#

Spørgsmål: hvorfor kommer der ikke noget som helst i min listbox i Form1.cs
der er 3 klasser som vist nedenfor.



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Threading;


namespace Weather
{
/// <summary>
/// Summary description for main.
/// </summary>
public class main : Form1
{
public main()
{
//
// TODO: Add constructor logic here
//
}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Threading;

namespace Weather
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;


public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//addListBoxItem("!tezt");

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}



#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.listBox1.Location = new System.Drawing.Point(648, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 420);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(768, 429);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
// [STAThread]
// static void Main()
// {
//
// //Application.Run(new Form1());
//
// }

private void button1_Click(object sender, System.EventArgs e)
{ Work w = new Work();
Thread th = new Thread(new ThreadStart(w.doWork));
th.Start();
}

public void addListBoxItem(string a)
{
listBox1.Items.Add(a);
}




}




}


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Threading;

namespace Weather
{
public class Work : Form1
{
public Work()
{

addlist("list");
}


public void doWork()
{
base.listBox1.Items.Add("adgafdgdsafgfdsa");
int w = 0;
int x = 0;
com.capescience.live.StationInfo si = new Weather.com.capescience.live.StationInfo();
com.capescience.live.Station[] st = new Weather.com.capescience.live.Station[1000];

ArrayList arrayCountry = new ArrayList();

String[] strArray = new String[1000];
String[] strArray2 = new String[1000];

strArray = si.listCountries();

//Form1 f = new Form1();



while(w < si.listCountries().Length)
{

st = si.searchByCountry(strArray[w]);
Debug.WriteLine(strArray[w]);
addlist(strArray[w].ToString());
base.listBox1.Items.Add("asdsada");


while(x < st.Length)
{
Debug.WriteLine(st[x].longitude);

//base.addListBoxItem(st[x].longitude.ToString());
x++;

}
w++;
}


}

public void addlist(string a)
{
base.addListBoxItem(a);
base.addListBoxItem("dfgfdgfd");

}

}
}



Hvorfor gøre det nemt?

[Redigeret d. 02/02-05 14:00:24 af test testesen]



2 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 1 karma
Sorter efter stemmer Sorter efter dato
En vigtig detajle når man opdatere en Form er at huske man ikke må opdatere GUI fra andre tråde end selve GUI tråden!!
En nem måde at sikre det på er at i sin eventhandler og have test på this.InvokeRequired
Jeg ville gøre sådan her:
Fold kodeboks ind/udKode 

Mvh
Henrik

[Redigeret d. 05/04-05 12:37:48 af Henrik]

[Redigeret d. 05/04-05 12:43:16 af Henrik]



Kommer der heller ikke noget i din debug ?



t