Is there a safer/ more readable way of using this code? Is there a safer/ more readable way of using this code?
I gotta translate this code into a better , safer and more readable sample of code and get my onclick to work.(its currently called from another thread then what it is created from.)

public class TemperaturePresenter
{
    MainView _view;

    // TODO ojoj det blev lite stökigt här
    public TemperaturePresenter(){
        _view = new MainView();
        ThreadPool.QueueUserWorkItem( 
             delegate{
        XmlDocument serverDoc = new XmlDocument();
        serverDoc.Load("ServerUrls.xml");
        var location = serverDoc.SelectSingleNode("//servers/server/url");
        string serverUrl = location.InnerText;
        var searchNode = serverDoc.SelectSingleNode("//servers/server/xpath");
        var searchPath = searchNode.InnerText;
        WebRequest req = WebRequest.Create(serverUrl);
        var response = req.GetResponse();
        var a = response;
        Stream stream = response.GetResponseStream();
        XmlDocument responseDoc = new XmlDocument();
        responseDoc.Load(stream);
        var temperatureNode = responseDoc.SelectSingleNode(searchPath);
        string temperature = temperatureNode.Attributes["value"].Value;

        SetTemperature(temperature);
            });
    }

    void SetTemperature(string temperature) {
        //if ( _view.InvokeRequired ){
        //    _view.Invoke( (Action&lt;string&gt;) SetTemperature, temperature);
        //    return;
        //}
        _view.Temperature = temperature;
    }

    public Form Form { get{ return _view; } }


Next page:

public partial class MainView : Form
{
    public MainView()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //fyll

        throw new NotImplementedException("cant press on this button, yet!");
    }

    public string Temperature {
        set { 
            tempLbl.Text = String.Format("{0} grader celcius", value);
            Refresh();
        } 


Im thinking i need to do some "this.thread" stuff, but i dont know how.
Well, at first glance, i think it should be:


Constructor shouldn't have any logic other than initialization. Any I/O, Threads .... operations would be better to start in custom method. So, in your case i will write:

public TemperaturePresenter(){
    _view = new MainView();
}
delegate in your example very large, my opinion - it should be extracted as a new method
Webrequest in Presenter? It depends, but I think it no so good. May be it would be better if you extract web-request logic as external dependency like ITemperatureProvider. Because now, it pull dependency on webserver file and file working into the presenter - it has some smell.


At first glance that's all.