0

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!

Fede Antuña
  • 72
  • 2
  • 11
  • If you want the data to be stored temporarily on web server you can use cache with expiry. Using thread in web application makes things complicated unnecessary. – Chetan Nov 14 '17 at 03:09
  • I've been reading and I think I will do that, but I would still like to know how to do this with threads – Fede Antuña Nov 14 '17 at 03:25
  • 3
    Would not it be nice if one come up with some sort of [session](https://stackoverflow.com/questions/14138872/how-to-use-sessions-in-an-asp-net-mvc-4-application) storage where you can keep data between requests? – Alexei Levenkov Nov 14 '17 at 03:46
  • For storing values in a stateless environment, there are `session`s, `cache`s, `HiddenFor` helpers, and even temporary database or file solutions. Keeping a different separate thread alive for 1000 concurrent visitors will be the last thing you would want if you are using an elastic cloud platform. even if the deployment target is yours, why waste so much resources? – mcy Nov 14 '17 at 08:11
  • Caches can be protected from the client? I want to keep the PrivateString as safe as possible from the client and anyone peeking on the client side... can I save something only in the server's cache? – Fede Antuña Nov 14 '17 at 11:19
  • 2
    it has been some time since I have dealt with asp.net but as far as i remember session data is hold only on the server, client just holds the session key. I think you may also encrypt these. maybe https://msdn.microsoft.com/en-us/library/ms178201.aspx has some more starting clues for you. – mcy Nov 17 '17 at 14:57
  • Thanks mcy!! I'll look into that – Fede Antuña Nov 18 '17 at 15:59

0 Answers0