5 Replies - 325 Views - Last Post: 26 September 2012 - 11:29 PM Rate Topic: -----

#1 imgregduh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 15-May 11

Cant get my list to appear in the listbox via thread

Posted 26 September 2012 - 05:34 PM

How do i continuously update a list on the UI from a process in a Thread? So far im attempting to use a threadpool and i have two list and they both have a process that add items to the list. the two processes that add to the list are being used in a thread of their own.
im not sure to use bindinglist nor how to use it either.
On the UI i have 2 textboxes, one button, 2 listboxes. Also im having an issue with the output going to the listboxes in the UI, i dont know what is wrong.

so far i have this :
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.Threading;

namespace MultiThreadingReaderWriter
{
    public partial class ReaderWriterfrm : Form
    {
        ReadWriter ReadWrite = new ReadWriter();
        public ReaderWriterfrm()
        {
            InitializeComponent();
            
        }
        private void Start_Click(object sender, EventArgs e)
        {
            int Read = 0;       
            int Write = 0; 
            temp.Items.Add("Opening");
            // Attempting to convert text into int 
            // if fail exception thrown + message box error

            try
            { Read = Convert.ToInt32(reader.Text); }
            catch (FormatException)//Checking if format is correct
            {
                MessageBox.Show("Reader Input must be a numerical digit");
                return;
            }
            catch (OverflowException)//Checking if number is too large or small
            {
                MessageBox.Show("Reader Input must be larger than -2,147,483,648 and smaller than +2,147,483,647 ");
                return;
            }
                if (Read <= 0)
                {
                    MessageBox.Show("Reader Input must be greater than 0");
                    return;
                }
                else if (Read >= 100)
                {
                    MessageBox.Show("Reader Input is too large");
                    return;
                }

            temp.Items.Add("Passed Read Exceptions");
            try
            {  Write = Convert.ToInt32(writer.Text); }
            catch (FormatException)//Checking if format is correct
            {
                MessageBox.Show("Writer Input must be a numerical digit");
                return;
            }
            catch (OverflowException)//Checking if number is too large or small
            {
                MessageBox.Show("Writer Input must be larger than -2,147,483,648 and smaller than +2,147,483,647 ");
                return;
            }
                if (Write <= 0)
                {
                    MessageBox.Show("Writer Input must be greater than 0");
                    return;
                }
                else if (Write >= 100)
                {
                    MessageBox.Show("Writer Input is too large");
                    return;
                }
            temp.Items.Add("Passed Write Exceptions");
            ManualResetEvent[] doneReadEvents = new ManualResetEvent[Read];
            ManualResetEvent[] doneWriteEvents = new ManualResetEvent[Write];
            ReadWriter[] ReadArray = new ReadWriter[Read];
            ReadWriter[] WriteArray = new ReadWriter[Write];
            for (int i = 0; i < Read; i++)
            {
                doneReadEvents[i] = new ManualResetEvent(false);
                ReadWriter Rw = new ReadWriter();
                Rw._Rw = Read;
                Rw._doneReadEvents = doneReadEvents[i];
                ReadArray[i] = Rw;
                ThreadPool.QueueUserWorkItem(Rw.ThreadPoolCallBackRead, i);
                temp.Items.Add("Thread Read: " + i);
            }
            for (int i = 0; i < Write; i++)
            {
                doneWriteEvents[i] = new ManualResetEvent(false);
                ReadWriter rW = new ReadWriter();
                rW._rW = Write;
                rW._doneWriteEvents = doneWriteEvents[i];
                WriteArray[i] = rW;
                ThreadPool.QueueUserWorkItem(rW.ThreadPoolCallBackWrite, i);
                temp.Items.Add("Thread Write: " + i);
            }
            WaitHandle.WaitAny(doneReadEvents);
            WaitHandle.WaitAny(doneWriteEvents);
            temp.Items.Add("Complete");
            temp.Items.Add("Closing");
            Output.DataSource = ReadWrite.MyList;
            //Output.Items.Add("something");
            //Work.DataSource = ReadWrite.MyList2;
            //ReadWrite.ReadData(Read);
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            reader.Clear();
            writer.Clear();
            temp.Items.Clear();
            Output.DataSource = null;
            Output.Items.Clear();
            Work.DataSource = null;
            Work.Items.Clear();            
        }
    }
}

ReaderWriter class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;

namespace MultiThreadingReaderWriter
{
    class ReadWriter
    {
        public List<string> myList = new List<string>();
        public List<string> myList2 = new List<string>();
        public List<string> MyList { get { return myList; } }
        public List<string> MyList2 { get { return myList2; } }
        public int RW { get { return _Rw; } }
        public int rW { get { return _rW; } }
        public int _Rw;
        public int _rW;
        public ManualResetEvent _doneReadEvents;
        public ManualResetEvent _doneWriteEvents;

        public void ThreadPoolCallBackRead(Object threadContext)
        { 
            int threadindex = (int) threadContext;
            myList2.Add("Thread Read " + threadindex+ " started");
            ReadData(_Rw);
            myList2.Add("Thread Read " + threadindex + " done");
            _doneReadEvents.Set();
        }
        public void ThreadPoolCallBackWrite(Object threadContext)
        {
            int threadindex = (int)threadContext;
            myList2.Add("Thread Write " + threadindex + " started");
            WriteData(_rW);
            myList2.Add("Thread Write " + threadindex + " done");
            _doneWriteEvents.Set();
        }
        public void ReadData(int reader)
        {
            myList.Add("Reader " + reader + " has entered Critical Section");
            myList.Add("Reader " + reader + " is Reading");
            myList.Add("Reader " + reader + " is leaving Critical Section");
        }
        public void WriteData(int writer)
        {
            myList.Add("Writer " + writer + " has entered Critical Section");
            myList.Add("Writer " + writer + " is writing");
            myList.Add("Writer " + writer + " is leaving Critical Section");
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Cant get my list to appear in the listbox via thread

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Cant get my list to appear in the listbox via thread

Posted 26 September 2012 - 05:46 PM

View Postimgregduh, on 26 September 2012 - 05:34 PM, said:

Also im having an issue with the output going to the listboxes in the UI, i dont know what is wrong.


Can't really help you there since you decided not to show us how temp is created or initialized, but based on the code you posted, it looks like it's your various calls to add items to temp that is your output.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Cant get my list to appear in the listbox via thread

Posted 26 September 2012 - 06:21 PM

View Postimgregduh, on 26 September 2012 - 05:34 PM, said:

im not sure to use bindinglist nor how to use it either.

You really should use the BindingList<> since it'll take care of a lot of other extra things for you, but that is for later.

If you set a breakpoint at line 104 and inspect the ReadWrite.MyList, how many items are in the list?
Was This Post Helpful? 0
  • +
  • -

#4 imgregduh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 15-May 11

Re: Cant get my list to appear in the listbox via thread

Posted 26 September 2012 - 07:59 PM

View PostSkydiver, on 26 September 2012 - 05:46 PM, said:

View Postimgregduh, on 26 September 2012 - 05:34 PM, said:

Also im having an issue with the output going to the listboxes in the UI, i dont know what is wrong.


Can't really help you there since you decided not to show us how temp is created or initialized, but based on the code you posted, it looks like it's your various calls to add items to temp that is your output.

temp is just a listbox on the UI. it is just there just to show me the checkpoints the program is passing as it runs

the One im concern with is the listbox named "Output" which is connected to a list called myList.

Also, that is all the code that is written if you see something that seems like its missing code or has no real connection to anything it probably is useless or something on the UI


i set the breakpoint and the count = 0

This post has been edited by imgregduh: 26 September 2012 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Cant get my list to appear in the listbox via thread

Posted 26 September 2012 - 11:21 PM

View Postimgregduh, on 26 September 2012 - 07:59 PM, said:

i set the breakpoint and the count = 0

Then that is why no data is being shown in your list box. There is no data to show.
Was This Post Helpful? 0
  • +
  • -

#6 imgregduh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 15-May 11

Re: Cant get my list to appear in the listbox via thread

Posted 26 September 2012 - 11:29 PM

View PostSkydiver, on 26 September 2012 - 11:21 PM, said:

View Postimgregduh, on 26 September 2012 - 07:59 PM, said:

i set the breakpoint and the count = 0

Then that is why no data is being shown in your list box. There is no data to show.

thanks for your assistance but i kinda figured it out and i cant edit my original post to show it though. i rewrote it all entirely and just through the class under the main in one cs file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1