简体   繁体   中英

How to wait for a specific thread of Threadpool

I have a synchronous task but sometime thread get stuck while making a remoting call which blocks the execution. So I want to make remote call making part async using a ThreadPool thread , but my idea is to keep it synchronous as much possible, that is I want main thread to wait for a timout value or ThreadPool thread to complete ,so that this activity is asynchronous only in case of thread stuck. ThreadPool thread doesn't give any handle so that I can call thread.join(timeout) on that and I can't use autoreset events also as that part of code can be accessed by multiple threads so which thread sets / resets autoreset event becomes even complicated and also it is an overhead.

Can you please suggest a cleaner way for this problem ? I can't create Thread also using Thread class because as I mentioned above that area of code can be accessed by multiple threads and it is a small task that remote call does ,something like updating a text like progress .

Code Snippet:

public static Exception AddProgress(Session session, ITaskSetProgress progress)
{
  IIconUIServer server = IconRemotingServer.Instance.GetUiServer(session);//gets remote proxy object
  server.AddProgress(progress);// calls a method over remoting IPC channel 
}

And this method can be called by more than one thread at a time.

My intention is to write AppProgressAsync method ,so that when call server.AddProgress(progress); hangs then only it should behave as async ,so I have a timeout .

I can't use Task Library as I need to support old customers who are on .net 3.5 still.

I solved this issue by using async delegates :something like below:

public static void AddProgressAsync(Session session, ITaskSetProgress progress) { AddProgressDelegate addProgress = new AddProgressDelegate(AddProgress);

        try
        {

            IAsyncResult result = addProgress.BeginInvoke(session, progress, null, null);
           result.AsyncWaitHandle.WaitOne(timeoutValue);


        }
        catch (Exception ex)
        {

        }
        finally
        {
            addProgress = null;

        }


    }

public static void AddProgress(Session session, ITaskSetProgress progress) { IIconUIServer server = IconRemotingServer.Instance.GetUiServer(session);//gets remote proxy object server.AddProgress(progress);// calls a method over remoting IPC channel }

It works fine in this scenario !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM