I have a particular string (actually a RSAParameter but I can convert it to string) that I need to keep "saved" in an MVC app without actually saving it. Considering how MVC works I think that the only solution (if there is any) to do this in the way that I want to do it is to create a thread and keep it alive holding the information that I need. That thread should die when I ask for that information again or when certain amount of time has passed.
So, I thought of the following
public string GetPublicString()
{
string PublicString = "MyPublicString";
string PrivateString = "MyPrivateString";
Thread thread = new Thread(PrivateStringHolder);
thread.Start(PrivateString);
return PublicString;
}
public void PrivateStringHolder(object state)
{
string PrivateString = (string)state;
// Something that keeps this thread alive for 30 seconds
// Something that keeps this thread alive until it's called
}
public GetPrivateString()
{
// Something that retrieves the PrivateString inside PrivateStringHolder
}
I don't know if I'm thinking correctly... I never worked with threads before so I'm asking for your help. Anyone knows how to code something that keeps my thread alive for 30 seconds or until it gets called from the "GetPrivateString" method, and how to make that call from the "GetPrivateString" method to retrieve the "PrivateString" inside the thread?
I red that I can use ManualResetEvent, so maybe the "... or until it gets called from the GetPrivateString..." could be solved doing this:
public string GetPublicString()
{
string PublicString = "MyPublicString";
string PrivateString = "MyPrivateString";
Thread thread = new Thread(PrivateStringHolder);
thread.Start(PrivateString);
return PublicString;
}
ManualResetEvent mre = new ManualResetEvent();
public void PrivateStringHolder(object state)
{
string PrivateString = (string)state;
// Something that keeps this thread alive for 30 seconds
mre.WaitOne();
}
public string GetPrivateString()
{
// Something that retrieves the PrivateString inside PrivateStringHolder
mre.Set();
return PrivateString; // Assuming that I saved the private string in the PrivateString var
}
I'll appreciate any help. Thanks!