M
Mark Rae
Hi,
My R&D department has asked me to look at threading in a Web Service written
in C#, so I came up with the following code:
using System;
using System.ComponentModel;
using System.Threading;
using System.Web.Services;
namespace CWSThreading
{
public class CThreading : System.Web.Services.WebService
{
private double mdThread1;
private double mdThread2;
public CThreading()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
[WebMethod]
public string TestThreading()
{
string strOutput = "";
Thread thread1 = new Thread(new ThreadStart(RandomNumber1));
thread1.Start();
strOutput += "Result from 1st thread: " + mdThread1.ToString() + "\n";
Thread thread2 = new Thread(new ThreadStart(RandomNumber2));
thread2.Start();
strOutput += "Result from 2nd thread: " + mdThread2.ToString() + "\n";
return strOutput;
}
protected void RandomNumber1()
{
System.Random rndNumber = new Random((int)DateTime.Now.Ticks);
mdThread1 = rndNumber.NextDouble();
}
protected void RandomNumber2()
{
System.Random rndNumber = new Random((int)DateTime.Now.Ticks);
mdThread2 = rndNumber.NextDouble();
}
}
}
This is being called by a Windows app to which a web reference has been
added pointing to the above web service. A simple form with a button runs
the following code:
using TestWSThreading.wsThreading;
private void cmdTestWSThreading_Click(object sender, System.EventArgs e)
{
wsThreading.CThreading wsTestThreading = new CThreading();
string strOutput = wsTestThreading.TestThreading();
MessageBox.Show(strOutput);
wsTestThreading = null;
}
If I set a breakpoint anywhere in the above code which causes me to step
through it, it always returns different random numbers e.g.:
Result from 1st thread: 0.513379168935762
Result from 2nd thread: 0.202799929866008
However, if I remove the breakpoint so that the web service runs "normally",
I always get the following result:
Result from 1st thread: 0
Result from 2nd thread: 0
Any assistance gratefully received.
Best regards,
Mark Rae
My R&D department has asked me to look at threading in a Web Service written
in C#, so I came up with the following code:
using System;
using System.ComponentModel;
using System.Threading;
using System.Web.Services;
namespace CWSThreading
{
public class CThreading : System.Web.Services.WebService
{
private double mdThread1;
private double mdThread2;
public CThreading()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
[WebMethod]
public string TestThreading()
{
string strOutput = "";
Thread thread1 = new Thread(new ThreadStart(RandomNumber1));
thread1.Start();
strOutput += "Result from 1st thread: " + mdThread1.ToString() + "\n";
Thread thread2 = new Thread(new ThreadStart(RandomNumber2));
thread2.Start();
strOutput += "Result from 2nd thread: " + mdThread2.ToString() + "\n";
return strOutput;
}
protected void RandomNumber1()
{
System.Random rndNumber = new Random((int)DateTime.Now.Ticks);
mdThread1 = rndNumber.NextDouble();
}
protected void RandomNumber2()
{
System.Random rndNumber = new Random((int)DateTime.Now.Ticks);
mdThread2 = rndNumber.NextDouble();
}
}
}
This is being called by a Windows app to which a web reference has been
added pointing to the above web service. A simple form with a button runs
the following code:
using TestWSThreading.wsThreading;
private void cmdTestWSThreading_Click(object sender, System.EventArgs e)
{
wsThreading.CThreading wsTestThreading = new CThreading();
string strOutput = wsTestThreading.TestThreading();
MessageBox.Show(strOutput);
wsTestThreading = null;
}
If I set a breakpoint anywhere in the above code which causes me to step
through it, it always returns different random numbers e.g.:
Result from 1st thread: 0.513379168935762
Result from 2nd thread: 0.202799929866008
However, if I remove the breakpoint so that the web service runs "normally",
I always get the following result:
Result from 1st thread: 0
Result from 2nd thread: 0
Any assistance gratefully received.
Best regards,
Mark Rae