Hello,
I am trying to run this program, but I have no idea where the problem is. I get these 3 errors in my error list when I try to run it:
Error 2 The best overloaded method match for 'Project_1.Client.Client(Project_1.SharedClick, Project_1.SharedMsg, System.Windows.Forms.TextBox, System.Windows.Forms.Button, System.Windows.Forms.Button)' has some invalid arguments
Error 3 Argument '1': cannot convert from 'Project_1.SharedMsg' to 'Project_1.SharedClick'
Error 4 Argument '2': cannot convert from 'Project_1.SharedClick' to 'Project_1.SharedMsg'
The error happens in the Form1 class at:
"client = new Client(sm, sc, txtThread2, btnStart, btnStop);"
Here is the complete source code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Project_1
{
public partial class Form1 : Form
{
// Declare Variables
private Server server = null;
private Client client = null;
private Thread serverThread = null, clientThread = null;
private ThreadStart ts1;
private ThreadStart ts2;
public Form1()
{
InitializeComponent();
btnStart.Focus();
}
//protected override void Dispose{ bool disposing }
//{
// if ( disposing )
// {
// if {components != null}
// {
// components.Dispose();
// }
// }
// base.Dispose{ disposing };
//}
private void btnStart_Click(object sender, EventArgs e)
{
// Form
btnStart.Enabled = true;
btnStop.Enabled = true;
// Create SharedMsg and SharedClick Objects
SharedMsg sm = new SharedMsg();
SharedClick sc = new SharedClick();
// Create server and client Objects
server = new Server(sm, sc, txtThread1);
client = new Client(sm, sc, txtThread2, btnStart, btnStop);
// Create Thread Delegates
ts1 = new ThreadStart(server.Produce);
ts2 = new ThreadStart(client.Consume);
// Create the Threads
serverThread = new Thread(ts1);
clientThread = new Thread(ts2);
// Start the Threads
serverThread.Start();
clientThread.Start();
// Reset
btnStart.Enabled = false;
//serverThread = new Thread( new ThreadStart( server.Produce));
}
// Stop Button Reset
private void btnStop_Click(object sender, EventArgs e)
{
serverThread.Abort();
clientThread.Abort();
txtThread1.Clear();
txtThread2.Clear();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
}
public class SharedClick
{
// Shared by Producer and Consumer Threads
private int clickNumber = 0;
// Check if Msg is Full
private bool clickNumberReading = false;
// Set Method
public void SetClickNumber(int cn)
{
// Lock Acquiring
Monitor.Enter(this);
// If Msg Full
if (clickNumberReading == true);
{
// Wait on this object
Monitor.Wait(this);
}
// If Msg Empty, Write New Value
clickNumber = cn;
// New Value Available
clickNumberReading = true;
// Waiting thread signal
Monitor.Pulse(this);
// Release lock on object
Monitor.Exit(this);
}// end SetClickNumber
// Get Method
public int GetClickNumber()
{
// Lock Acquiring
Monitor.Enter(this);
if (clickNumberReading == false)
{
// Wait on this object
Monitor.Wait(this);
}
// New Value Un
clickNumberReading = false;
// Waiting thread signal
Monitor.Pulse(this);
// Release lock on object
Monitor.Exit(this);
// Return the click amount
return clickNumber;
} //end get method
}
public class SharedMsg
{
// Shared by Producer and Consumer Threads
private string sharedMsg = "";
// Check if Msg is Full
private bool sharedMsgReading = false;
// Set Method
public void SetMsg(string sm)
{
// Lock Acquiring
Monitor.Enter(this);
// If Msg Full
if (sharedMsgReading == true)
{
// Wait on this object
Monitor.Wait(this);
}
// If Msg Empty, Write New Value
sharedMsg = sm;
// New Value Available
sharedMsgReading = true;
// Waiting thread signal
Monitor.Pulse(this);
// Release lock on object
Monitor.Exit(this);
}
public string GetMsg()
{
// Lock Acquiring
Monitor.Enter(this);
// If Msg Full
if (sharedMsgReading == false)
{
// Wait on this object
Monitor.Wait(this);
}
// New Value Available
sharedMsgReading = false;
// Waiting thread signal
Monitor.Pulse(this);
// Release lock on object
Monitor.Exit(this);
// Return Msg
return sharedMsg;
}
}
public class Server
{
// Declare Variables
private SharedMsg sm;
private SharedClick sc;
private TextBox txtThread1;
delegate void SetTextCallback(string text);
// Constructor
public Server(SharedMsg m, SharedClick s, TextBox aBox)
{
txtThread1 = aBox;
sm = m;
sc = s;
}
// Produce Method
public void Produce()
{
// Variables
int count = 0; int numClick=0; int bannerIndex=-1;
string msg;
// Create String with Banners
String[] banner = { "Help?",
"Success",
"Love",
"New",
"Test"};
// Loop up to 30 times
while (count<30)
{
count++;
if (numClick < 500)
{
// Banner Display
SetText("(Click: " + numClick + ") Move to next banner!");
bannerIndex = (bannerIndex + 1) % 5;
}
else
{
SetText("{Click: " + numClick + ") Keep the banner!");
}
// Place in SharedMsg
msg = banner[bannerIndex];
sm.SetMsg(msg);
// Passes Number Of Clicks
numClick = sc.GetClickNumber();
}
SetText("Done!");
} // end of Produce Method
// SetText Display
private void SetText(string text)
{
if (this.txtThread1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.txtThread1.Invoke(d, new object[] { text });
}
else
{
this.txtThread1.Text = text;
}
}
} // end of class Server
public class Client
{
// Declare Variables
private SharedClick sharedclick;
private Random r = new Random();
private SharedMsg sharedMsg;
private TextBox txtThread2 = new TextBox();
private Button btnStop = new Button();
private Button btnStart = new Button();
delegate void SetTextCallback(string text);
// Contructor
public Client(SharedClick sc, SharedMsg sm, TextBox box, Button start, Button stop)
{
sharedclick = sc;
sharedMsg = sm;
txtThread2 = box;
btnStop = stop;
btnStart = start;
}
// Consume Method
public void Consume()
{
int count = 0;
// Loop up to 30 times
while (count < 30)
{
// Click between 1 and 1000
int clicks = r.Next(1, 1000);
count++;
Thread.Sleep(clicks);
sharedclick.SetClickNumber(clicks);
// Display amount of clicks
SetText((sharedMsg.GetMsg() + " (Clicks:" + clicks + ")"));
}
btnStart.Enabled = true;
btnStop.Enabled = false;
SetText("Done!");
} // end method Consume
// SetText Display
private void SetText(string text)
{
if (this.txtThread2.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.txtThread2.Invoke(d, new object[] { text });
}
else
{
this.txtThread2.Text = text;
}
}
} // end of the Client class
} // end of namespace