大家好,有个线程控制的问题想向大家请教一下,我想实现如下功能:
两个exe程序在同一台机上运行。
应用程序A.exe中,开启了一个线程,代码如下:
Thread myThread = new  Thread (new System .Threading .ThreadStart (this.MethodA() ));
myThread .Start ();private void MethodA()
{
    ....
    //给当前线程命名,
System.Threading .Thread .CurrentThread .Name = "myThread";
//线程挂起,保持用户指定的时间
System.Threading.Thread.CurrentThread.Join(1000*60*5);
   int intTemp = 100;
  ...
}在应用程序B.exe中,我想获取在A.exe中创建的名称为myThread的线程,如果这个线程还没有阻塞五分钟,我想直接让它执行下面的:int intTemp=100;这个语句,不知道如何实现,请大家指点一下!谢谢!

解决方案 »

  1.   

    用一个信号灯来控制比较好:private   void   MethodA() 

                Semaphore semaphore = new Semaphore(0, 1, "MySemaphore");
                if (semaphore.WaitOne(1000 * 60 * 5))
                {
                    int intTemp = 100;
                }
    }
    //A.exe中:
                try
                {
                    Semaphore semaphore = Semaphore.OpenExisting("MySemaphore");
                    semaphore.Release(1);
                }
                catch (WaitHandleCannotBeOpenedException ex)
                {            }
      

  2.   

    忘了加一个参数了:
    if(semaphore.WaitOne(1000 * 60 * 5,true))
      

  3.   

    谢谢 Whislly :
    我还想请教一下,我如何在B.exe中去终止A.exe中的等待呢,我不太理解你的代码,请指教
      

  4.   

    Whislly :是不是//A.exe中:这句应该放在B.exe中?
    1.0的框架支持Semaphore 吗?好像System.Threading中没有Semaphore ,请指教!
      

  5.   

    Mutex 类是用于进程间同步的
    不过没有可以终止其他应用中刮起线程的能力
      

  6.   

    1.0的框架不支持Semaphore的话,可以自己写一个:     public enum SemaphoreRights
        {
            Modify = 2,
            Delete = 65536,
            ReadPermissions = 131072,
            ChangePermissions = 262144,
            TakeOwnership = 524288,
            Synchronize = 1048576,
            FullControl = 2031619,
        }
        class Semaphore : IDisposable
        {
            [DllImport("KERNEL32.DLL",CharSet=CharSet.Unicode)]
            private extern static IntPtr CreateSemaphore(IntPtr attributes,int initialCount,int maximumCount,string name);
            [DllImport("KERNEL32.DLL", CharSet = CharSet.Unicode)]
            private extern static IntPtr OpenSemaphore(int dwDesiredAccess, int bInheritHandle, string lpName);
            [DllImport("KERNEL32.DLL")]
            private extern static int WaitForSingleObject(IntPtr hHandle, int dwMilliseconds);
            [DllImport("KERNEL32.DLL")]
            private extern static int ReleaseSemaphore(IntPtr hSemaphore, int lReleaseCount, IntPtr lpPreviousCount);
            [DllImport("KERNEL32.DLL")]
            private extern static bool CloseHandle(IntPtr hObject);        private IntPtr hSemaphore = IntPtr.Zero;        private Semaphore(IntPtr hSemaphore)
            {
                this.hSemaphore = hSemaphore;
            }        public Semaphore(int initialCount, int maximumCount, string name)
            {
                this.hSemaphore = CreateSemaphore(IntPtr.Zero, initialCount, maximumCount, name);
            }        public bool Release(int nCount)
            {
                int ret = ReleaseSemaphore(this.hSemaphore, nCount, IntPtr.Zero);
                return (ret != 0);
            }        public bool WaitOne()
            {
                return WaitOne(-1);
            }        public bool WaitOne(int dwMilliseconds)
            {
                if (WaitForSingleObject(this.hSemaphore, dwMilliseconds) == 0)
                {
                    return true;
                }
                return false;
            }        public void Dispose()
            {
                CloseHandle(this.hSemaphore);
            }        public static Semaphore OpenExisting(string name)
            {
                return OpenExisting(name, SemaphoreRights.FullControl);
            }        public static Semaphore OpenExisting(string name,SemaphoreRights rigths)
            {
                IntPtr hSemaphore = OpenSemaphore((int)rigths, 0, name);
                if (hSemaphore == IntPtr.Zero)
                {
                    throw new Exception(string.Format("open semaphore:{0} error.",name));
                }
                return new Semaphore(hSemaphore);
            }
        }
    //B.exe中:
                try
                {
                    Semaphore semaphore = Semaphore.OpenExisting("MySemaphore");
                    semaphore.Release(1);
                }
                catch (Exception ex)
                {            }