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");
}
}
}

New Topic/Question
Reply



MultiQuote





|