views:

97

answers:

1

Hi, I have a windows app and an an dll(windows form) that im trying to open (ActivationCheck), im trying to pause the current thread open a new thread (ActivationCheck) wait for that form event to return true then continue the main thread. Could someone explain\show me what im doing wrong - thanks.

 static class Program
    {
        private static SplashScreen splash;
        private static bool quitApp;
        private static bool activationFinished;

        [STAThread]
        static void Main()
        {

        Thread thread = new Thread(ActivationCheck);
        thread.Start();



               do
               {
                   Thread.Sleep(1000);
               } while (activationFinished);

           if (!quitApp)
           {


               Thread.Sleep(0);
              // WizardRun();
               Application.Run(new Main(ref splash));
             }
}
}

     private static void ActivationCheck()
        {

            splash.SetStatus = "Checking License...";

            Guid productId = new Guid(Properties.Settings.Default.ProductId);
            Guid versionId = new Guid(Properties.Settings.Default.VersionId);

            Client.UI.EntryPoint entryPoint = new EntryPoint();

            activationFinished = false;

            Client.BLL.ProductActivation.GenerateTrialLicense(productId1, versionId2, EditionId3);
            entryPoint.IniatePlugin(productId, versionId);
            entryPoint.PluginFinished += new EventHandlers.PluginFinishEventHandler(entryPoint_PluginFinished);


        } 
  static void entryPoint_PluginFinished(bool forceQuit)
        {
            quitApp = forceQuit;
            activationFinished = true;


        }
+6  A: 

You could just do thread.Join()? To be honest, though, I'm not quite sure what the point is of starting a second thread and pausing the first; just do the work on the original thread?

The problem with the code is possibly that activationFinished is being held in a register; try marking it as volatile, or alternatively use a lock at both places that access this variable. Even better would be to use a ManualResetEvent or similar, and open it from the activation code.

using System;
using System.Threading;
static class Program
{
    static void Main()
    {
        new Thread(DoActivation).Start();
        Console.WriteLine("Main: waiting for activation");
        activation.WaitOne();
        Console.WriteLine("Main: and off we go...");
    }
    static void DoActivation(object state)
    {
        Console.WriteLine("DoActivation: activating...");
        Thread.Sleep(2000); // pretend this takes a while
        Console.WriteLine("DoActivation: activated");
        activation.Set();
        // any other stuff on this thread...
    }
    static ManualResetEvent activation = new ManualResetEvent(false);
}
Marc Gravell