Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error messages in a multithreaded application.

Status
Not open for further replies.

Sanjeet1

Programmer
Sep 4, 2003
26
US
I have a windows service. The windows service has 2 files. Winsowsservice.cs and Thread.cs.

The windowsservice receives a byte array converts it to a string and passes it to a thread instance in the thread.cs file. Here is the code:

byte[] b=new byte[1024];
int k=s.Receive(b);
Console.WriteLine("Recieved...");
ASCIIEncoding encoding = new ASCIIEncoding( );
string cs = encoding.GetString(b);

MyThread thr1 = new MyThread(cs);
MyThread thr2 = new MyThread(cs);
// I get an error message here: No overload for method 'MyThread' takes '1' argument
I am trying to pass the cs string how do I pass multiple messages?

Thread tid1 = new Thread(new ThreadStart(thr1.Thread1(cs)));
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1(cs)));

// I get an error message saying: Method name expected


In the thread.cs file I declare a thread1 function and try to process the cs string that's passed from the serviceexample.cs file:

public void Thread1(string x)
{
for (int i = 0; i < 10; i++)
{
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
string[] array = cs.Replace("\\n", "\n").Replace("\0", "").TrimEnd().Split('\n');

// I get an error message at the last line: The type or namespace name 'cs' could not be found (are you missing a using directive or an assembly reference?)

Thank you


 
Thread as a classname is a BAD IDEA. There is already a "Thread" class that .Net uses. Try changing the name of your Thread class to CustomThreadClass with the constructor also being

public CustomThreadClass(string cs)
{

}


Then in your Thread1 function (rename this to something more meaningful too) you don't have cs, you have string x. To make this really simple, and consistent, I would change that method to:

public void SetStringInThread(string cs)
{
//The code you already have goes in here
}
 
I don' have a threadclass I just call the the thread functionality in the onstart section of the windows service:
protected override void OnStart(string[] args)
{
Console.WriteLine("Connection accepted from "+s.RemoteEndPoint);
byte[] b=new byte[1024];
int k=s.Receive(b);
Console.WriteLine("Recieved...");
ASCIIEncoding encoding = new ASCIIEncoding( );
string cs = encoding.GetString(b);

MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1));
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
tid1.IsBackground = true;
tid2.IsBackground = true;
try
{
tid1.Start();
tid2.Start();
}
catch (ThreadStateException te)
{
Console.WriteLine(te.ToString() );
}
Thread.Sleep(10);
Console.WriteLine("End of Main");

then in the thread.cs file I have mythread class that has a thread1 function:

public class MyThread
{
public void Thread1(string x)
{
for (int i = 0; i < 10; i++)
{
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
string[] array = x.Replace("\\n", "\n").Replace("\0", "").TrimEnd().Split('\n');


 
JurkMonkey saying you need a constructor for your MyThread class among other good points. JM please correct me if I misspoke. Here is a code snippet that might help.
Code:
using System;
using System.Threading;

public class Sleeper
{ 
	public Int32 sleepDelay; 
	string sleepMessage;
	public string retVal; 

	public Sleeper(Int32 sleepDelay, string sleepMessage) 
	{ 
		this.sleepDelay = sleepDelay; 
		this.sleepMessage = sleepMessage; 
	} 

	public void ThreadSleeper() 
	{ 
		System.Threading.Thread.Sleep(sleepDelay * 1000); 
		retVal = sleepMessage + " slept for " + sleepDelay.ToString() + " seconds." + Environment.NewLine; 
	} 
}

class TestSleeper
{
    static void Main() 
	{
		Sleeper s1 = new Sleeper(2, "sleeper1");
		Thread t1 = new Thread(new ThreadStart(s1.ThreadSleeper));
		t1.Start();
		t1.Join();
		Sleeper s2 = new Sleeper(4, "sleeper2");
		Thread t2 = new Thread(new ThreadStart(s2.ThreadSleeper));
		t2.Start();
		t2.Join();
		Console.WriteLine(s1.retVal + s2.retVal);
	}
}
Marty
 
Also:
Code:
     string[] array = cs.Replace("\\n", "\n").Replace("\0", "").TrimEnd().Split('\n');
This is a bad idea, as you're accessing the cs variable (which is declared outside of your running thread) multiple times, which raises the risk of it getting mangled. You should make a local copy of it in your thread one time, and protect that copy operation with the lock() operator. That will reduce/eliminate the chance of another thread modifying it.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top