0

This is my first time with C# (So far I've been using it for about a week) and I have a few small issues and would like some pointers on tiding up my code. I have decided to make this section of my code multithreaded as before it was cause the program to near lockup while it ran this, however now it doesn't seem to run correctly at all.

The program should return saying a update is available however it does nothing but run the update Window twice and leave it there.

Here is the code and thanks in advance!

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;
using System.Threading;

namespace ProxySwitch
{
    public partial class UpdateCheckWindow : Form
    {
        private ParameterizedThreadStart process;
        public UpdateCheckWindow()
        {
            InitializeComponent();
        }

        private void UpdateCheckWindow_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void UpdateCheckWindow_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(process);
            thread.Start();
            {
                string VersionURL = "http://proxyswitch.co.uk/downloads/version.txt";
                string result = null;

                try
                {
                    WebClient client = new WebClient();
                    result = client.DownloadString(VersionURL);

                    if (result.Contains("1"))
                    {
                        this.Close();
                    }
                    else
                    {
                        DialogResult UpdateResult = MessageBox.Show("A update is avaliable! Would you like to download the latest version?", "Update Avaliable!", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                        if (UpdateResult == DialogResult.Yes)
                        {
                            System.Diagnostics.Process.Start("http://proxyswitch.co.uk/downloads/current/setup.exe");
                            this.Close();
                        }
                        else if (UpdateResult == DialogResult.No)
                        {
                            this.Close();
                        }
                        thread.Abort();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Close();
                }
            }
        }
    }
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by powerbox
0

First of all your application is a window app, you can't just fire a form method with having an issue of cross-thread exception. If you really wanted to have your app using thread then you should create your own class using a thread inside of it or use background worker class provided by .net.

I don't know what your process method does but I guess I can provide you a sample code;

public class SampleThread
    {
        private object syncRoot;
        private bool running;
        private System.Threading.Thread thread;
        private System.Threading.ParameterizedThreadStart threadStart;

        public SampleThread()
        {
            threadStart = new System.Threading.ParameterizedThreadStart(Run);
            thread = new System.Threading.Thread(threadStart);
            syncRoot = new object();
            running = false;
        }
        private void Run()
        {
            while (running)
            {
                //some stuff here , 
               //I never used Thread.Sleep, I used  thread signalling to  
              // to wake my threads up...
            }
        }
        public void StartService()
        {
            lock (syncRoot)
            {
                running = true;
                thread.Start();
            }
        }

        public void StopService()
        {
            lock (syncRoot)
            {
                running = false;
            }
        }
    }

For your web client code I rather have a method for that also checking if it needed InvokeRequired

private void StartWebClient()
{
    MethodInvoker action = delegate 
    {
        WebClient client = new WebClient();
        result = client.DownloadString(VersionURL); 
        //.... do your stuff here
    };
    if ( this.InvokeRequired )
      this.BeginInvoke(action);
   else
     action.Invoke();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.