I have created an application with a static variable (a random number generated on run time). copied .exe to three folder and ran it. the static value is seems to be shared (same) in all three .
IS that expected?
I have created an application with a static variable (a random number generated on run time). copied .exe to three folder and ran it. the static value is seems to be shared (same) in all three .
IS that expected?
I don't think the Memory is shared in this case, unless you somehow did that by accident.
However a new Radom() is the equivalent of saying new Random(DateTime.Now.Millisecond). If you run the 3 programs within the same millisecond (which believe me is not just possible, but likely if do via code), or if you have hard coded new Random(same int every time), your random rolls will be the same across all your programs if you are rolling within the same range ie. 0-5 like die or something.
Yes, it's expected. the static variable will take the value from the "number generator" for the first time, then when this variable has a value it will never take the value from the generator next time, it will return the stored value in it. so it's better to use a static method instead of a variable to generate new values.
note : as not multiple running exe
let's suppose we have this class that has static variable and static method:
public class StaticTest
{
static public int RandomNumber=GenerateRandomNumber();
static public int GenerateRandomNumber()
{
Random rnd = new Random();
int temp = rnd.Next();
return temp;
}
}
and you can test it by calling it from another class and see the output:
int TempVariable1 =StaticTest.RandomNumber;
int TempVariable2= StaticTest.RandomNumber;
int TempMethod1 = StaticTest.GenerateRandomNumber();
int TempMethod2 = StaticTest.GenerateRandomNumber();
System.Console.WriteLine("TempVariable1 = {0}", TempVariable1);
System.Console.WriteLine("TempVariable2 = {0}", TempVariable2);
System.Console.WriteLine("TempMethod1 = {0}", TempMethod1);
System.Console.WriteLine("TempMethod2 = {0}", TempMethod2);
the result will be as following :
TempVariable1 1936885472 int
TempVariable2 1936885472 int
TempMethod1 1887921393 int
TempMethod2 2129299766 int
I think the Random number generator generating same number. That is the reason I am getting same value. The below code sample I did and the mail I received. I have weekly jobs, but the number is repeating all weeks. :(
Steps to reproduce:
Build the below code in release mode.
Copy to 3 folders bin1, bin2, bin3
Create three task scheduler job for the three solutions.
Select three task jobs together and run together.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading;
namespace XXX.HistoryExtractor { public class Test { public static String RandomNum1;
/// <summary>
/// Start method for Job
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
RandomNum1 = new Random().Next(9999).ToString() + " ";
MailHandler2 mh = new MailHandler2();
mh.SendJobStartUpdate();
}
}
public class MailHandler2
{
String RandomNum;
public MailHandler2()
{
//Thread.Sleep(1000);
RandomNum = new Random().Next(9999).ToString() + " ";
}
public void SendJobStartUpdate()
{
try
{
var mail = new MailMessage();
mail.To.Add("lijo.john@urmail.com");
mail.Subject = "Job Started " + RandomNum + " - " + DateTime.Now;
mail.Body = "<br/>Job Started...";
mail.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
try
{
smtp.Send(mail);
}
catch (Exception c)
{
throw c;
}
}
}
catch (FormatException eF)
{
}
}
}
}
See the set of mails I received and random nuber get duplicated.
From Subject Received Size Categories
noreply@mailserver.com Job Started 6887 - 1/22/2020 12:42:55 AM 11:13 41 KB
noreply@mailserver.com Job Started 6887 - 1/22/2020 12:42:55 AM 11:13 40 KB
noreply@mailserver.com Job Started 4936 - 1/22/2020 12:42:55 AM 11:13 41 KB
=====================
From Subject Received Size Categories
noreply@mailserver.com Job Started 7953 - 1/22/2020 12:41:16 AM 11:11 41 KB
noreply@mailserver.com Job Started 6001 - 1/22/2020 12:41:16 AM 11:11 41 KB
noreply@mailserver.com Job Started 7953 - 1/22/2020 12:41:16 AM 11:11 41 KB
===============================
From Subject Received Size Categories
noreply@mailserver.com Job Started 1420 - 1/22/2020 12:41:08 AM 11:11 41 KB
noreply@mailserver.com Job Started 5280 - 1/22/2020 12:41:05 AM 11:11 41 KB
noreply@mailserver.com Job Started 5280 - 1/22/2020 12:41:05 AM 11:11 41 KB