質問

I have one aspx code behind page, that uses a process (p) to call an application (exe file) as following the code:

p.Start();
while(!p.HasExited)
{
    Thread.Sleep(2000);
}
p.Dispose();

The while loop to take waiting of 2s, but is always throwing the exception like below:

System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   at Insyma.ContentUpdate.XmlReader.DoSleep()

At the statement Thread.Sleep(2000). So anybody can help me some solutions. Thanks so much,

役に立ちましたか?

解決

Use AutoResetEvent

class BasicWaitHandle
{
   static EventWaitHandle _waitHandle = new AutoResetEvent (false);
   static void Main()
   {
       new Thread (Waiter).Start();        
       _waitHandle.WaitOne(); //wait for notification
       //Do operartion
   }

   static void Waiter()
   {
      //Do operations
      _waitHandle.Set(); //Unblock
   }
}

他のヒント

Seems like you need some Fire and Forget pattern. thread is exiting because once page is been rendered its instance is prone to be disposed.. so this is may be what you need.

ThreadPool.QueueUserWorkItem(o => FireAway());

private void FireAway()
{
p.Start();
while(!p.HasExited)
{
    Thread.Sleep(2000);
}
p.Dispose();

}

For more info: http://weblogs.asp.net/albertpascual/archive/2009/05/14/fire-and-forget-class-for-asp-net.aspx

Simplest way to do a fire and forget method in C#?

Regards.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top