Quantcast
Channel: WPF thread and progressbar
Viewing all articles
Browse latest Browse all 2

WPF thread and progressbar

0
0

Hey,

If I make a class like this to do a thread but want to let the progress know to a progressbar on the main window what am I missing here:

This is a class that makes use of a thread. I didn't place all the code inside because it's not relevant. (I think it's just a question of structure and knowing what to do) I know this is working code that uses a thread. Now I want to have a progressbar that runs accordingly with this thread on the main window:

namespace PE3.NetworkTools.Services
{
   public delegate void PortScanServiceEvent(PortScanService sender, PortScanServiceEventArgs e);

   public class PortScanServiceEventArgs
   {
       public bool completed;
       public string result;

       public PortScanServiceEventArgs(string resultaat, bool completed)
       {
           this.completed = completed;
           this.resultaat = result;
       }
   }

        public class PortScanService
        {
            
            public event PortScanServiceEvent scanDone;

            private Thread portScanServiceThread;


            //Attributes: Properties


            //Constructor:
            public PortScanService()
            {
               //Some code
            }

            public void startScan()
            {
                portScanServiceThread = new Thread(new ThreadStart(PortScanServiceThreadProc));
                portScanServiceThread.Start();
            }

            public void PortScanServiceThreadProc()
            {
                scan();
                scanDone.Invoke(this, new PortScanServiceEventArgs(this.result, true));
            }

            //Methods:

            //Public Methoden:
            public void scan()
            {
               //some code
                try
                {
                    scanDone.Invoke(this,new
PortScanServiceEventArgs(result,false));
                }
                catch
                {

                }

            }

            public void AbortThread()
            {
                try
                {
                    if (portScanServiceThread != null)
                        portScanServiceThread.Abort();
                    scanDone.Invoke(this, new PortScanServiceEventArgs(resultaat, true));
                }
                catch (ThreadAbortException)
                {
                    //catch and we don't do anything.
                }
            }
        }
    }

This is the code from the main window:

  public partial class MainWindow : Window
    {
        private PortScanService pss = null;
        private bool busy = false;


        private void btnPortScan_Click(object sender, RoutedEventArgs e)
        {
            pss = new PortScanService();
            if (busy)
            {
                pss.AbortThread();
            }
            else
            {
                busy = true;
               
//some code

           pgrStatus.IsEnabled = true;
           pss.scanDone += new PortScanServiceEvent(pss_scanGeDone);
                            pss.startScan();
                            pgrStatus.Visibility = Visibility.Visible;
                            lblStatus.Content = "Thread is busy";
                            btnStopThread.Content = "Stop PortScanService";
                        }
         }



















}

Now I have missing link on how I can pass the progress of the threat to the progressbar in WPF, so that you can view the progress on the progressbar as long as the thread is busy.




Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images