-3

我在 C# 中编写代码以在进程中创建线程,并在不同的进程中每 2 分钟调用一次 Jquery ajax(使用 JQuery SetInterval)以创建相同的线程。

但是我想,如果线程池中已经存在一个线程,那么创建线程的新请求什么也不做。

我怎样才能做到这一点?

更新 -

我做职位发布工作。当用户在Job Site. 我需要在一段时间(约 2 分钟)后获得工作状态(工作岗位与否),所以为此我在服务器端代码和睡眠线程上创建一个线程 2 分钟和return false. 同时,在客户端,我设置了 2 分钟的时间间隔并调用 ajax 函数来创建相同的线程。

我的代码 -

var List = ["JobTitle", "JobDescription", "JobId"];
var JobDetails = JSON.stringify({ list: List });
$.ajax({
    type: "POST",
    url: "JobManagement.aspx/JobPost",
    data: JobDetails,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
          if (response.d.indexOf("Error") != -1) {
               alert(response.d);
          }
          else if (response.d.indexOf("Job Queued up") != -1) {
             var TransactionId = response.d.split(':')[1];
             var GetJobStatusInterval = setInterval(function () {
               $.ajax({
                    type: "POST",
                    url: "JobManagement.aspx/GetJobStatusMethod",
                    data: '{TransactionId: ' + "'" + TransactionId + "'" + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                             clearInterval(GetJobStatusInterval);
                    },
                    failure: function (response) { }
                   });
              }, 20000);
           }
     },
     failure: function (response) { }

 });

C# 代码 -

[System.Web.Services.WebMethod()]
        public static string JobPost(List<string> list)
        {
            try
            {
                string JobTitle = list[1];
                string JobDescription = HttpUtility.HtmlEncode(list[2]);
                string JobId = list[2];
                string TransactionDID = "";
                JobManagement oJobManagement = new JobManagement();
                string JobData = "<JobTitle>" + JobTitle + "</JobTitle><JobDescription>" + JobDescription + "</JobDescription><JobId>" + JobId + "</JobId>";

                XmlDocument soapEnvelopeXml = new XmlDocument();
                //-- Creating web request with soap action
                HttpWebRequest Soapreq = (HttpWebRequest)WebRequest.Create("http://dpi.careerbuilder.com/WebServices/RealTimeJobPost.asmx");
                Soapreq.Headers.Add("SOAPAction", "http://dpi.careerbuilder.com/WebServices/RealTimeJobPost/ProcessJob");
                Soapreq.ContentType = "text/xml; charset=utf-8";
                Soapreq.Accept = "text/xml";
                Soapreq.Method = "POST";
                soapEnvelopeXml.LoadXml("<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><ProcessJob xmlns='http://dpi.careerbuilder.com/WebServices/RealTimeJobPost'><Job>" + JobData + "</Job></xmlJob></ProcessJob></soap:Body></soap:Envelope>");


                //-- request to the server
                using (Stream stream = Soapreq.GetRequestStream())
                {
                    using (StreamWriter stmw = new StreamWriter(stream))
                    {
                        stmw.Write(soapEnvelopeXml.InnerXml.ToString());
                    }
                }

                // -- Getting response to the server
                using (WebResponse response = Soapreq.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                    {
                        string soapResult = rd.ReadToEnd();
                        Console.WriteLine(soapResult);
                        TransactionDID = soapResult.ToString().Substring(soapResult.LastIndexOf("<TransactionDID>"));
                        TransactionDID = TransactionDID.Substring(TransactionDID.IndexOf("<TransactionDID>"), TransactionDID.IndexOf("</TransactionDID>")).Split('>')[1];
                    }
                }
                string CurrentJobStatus = "";
                CurrentJobStatus = oJobManagement.GetCBJobStatus(TransactionDID);

                if (CurrentJobStatus == "Job Queued up")
                {
                    string objJobStatus = TransactionDID + ":" + oJobManagement.oUser.ID.ToString() + ":" + oJobManagement.oUser.Organisation_ID.ToString();
                    System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(oJobManagement.GetJobStatusForCB), (object)objJobStatus);
                    return "";
                }
                return "";
            }
            catch {
                return "";
            }
        }

private string GetCBJobStatus(string TransactionId)
        {
            try
            {
                string PostJobStatus = "";
                JobManagement oJobManagement = new JobManagement();
                HttpWebRequest JobStatusreq = (HttpWebRequest)WebRequest.Create("http://dpi.careerbuilder.com/webservices/RealTimeJobStatus.asmx/GetJobPostStatus?sTGDID=" + TransactionId);
                JobStatusreq.Method = "GET";
                using (WebResponse Statusresponse = JobStatusreq.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(Statusresponse.GetResponseStream()))
                    {
                        string JobStatus = rd.ReadToEnd();
                        // - Post job status
                        PostJobStatus = JobStatus.ToString().Substring(JobStatus.LastIndexOf("<PostStatus>"));
                        PostJobStatus = PostJobStatus.Substring(PostJobStatus.IndexOf("<PostStatus>"), PostJobStatus.IndexOf("</PostStatus>")).Split('>')[1];
                    }
                }
                if (PostJobStatus == "Success")
                {
                    return "Job Posted Successfully";
                }
                else if (PostJobStatus == "Queued")
                {
                    return "Job Queued up";
                }
                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }

线程的 C# 函数 -

// -- Make thread
        private void GetJobStatusForCB(object objJobStatus)
        {
            try
            {
                System.Threading.Thread.Sleep(20000);
                string TransactionDID = objJobStatus.ToString().Split(':')[0];
                string JobStatus = "";
                JobManagement oJobManagement = new JobManagement();
                JobStatus = oJobManagement.GetCBJobStatus(TransactionDID);    

               if (JobStatus == "Job Queued up")
                {
                    System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(oJobManagement.GetJobStatusForCB), (object)objJobStatus);
                }

            }
            catch (Exception ex)
            {

            }            
        }

调用设置间隔的 Web 方法 -

[System.Web.Services.WebMethod()]
        public static string GetJobStatusMethod(string TransactionId)
        {
            try
            {
                string JobStatusCB = "";
                JobManagement oJobManagement = new JobManagement();
                oJobManagement.FillUserobj();
                JobStatusCB = oJobManagement.GetCBJobStatus(TransactionId);

                if (JobStatusCB == "Job Queued up")
                {

                  // -- I want to check here if a thread is already running "Do Nothing"
                  // --  if "A Thread is already running" return false;
                    string objJobStatus = TransactionId + ":" + oJobManagement.oUser.ID.ToString() + ":" + oJobManagement.oUser.Organisation_ID.ToString();
                    System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(oJobManagement.GetJobStatusForCB), (object)objJobStatus);
                }                
            }
            catch (Exception ex)
            {
                return "";
            }
            return "";
        }

在上面的代码中,jquery ajax当用户点击发布作业时主要运行。我使用JobPostweb 方法获得了我的工作的第一个状态,如果我得到了状态,我调用一个线程来使用函数Job Queued up获取工作状态。GetJobStatusForCB进入此功能sleep当前进程 2 分钟return false,然后在客户端获取作业状态并设置客户端SetInterval2 分钟。在此间隔内,我调用不同Jquery ajax的方法来获取工作状态。在这个 ajax 中,我调用 web 方法GetJobStatusMethod。在 web 方法中首先检查作业状态,如果得到Job Queued up则调用另一个线程来获取状态。在这个词干中出现了我的问题 - 因为如果一个线程已经运行以获得作业状态,我不需要创建另一个线程。我不想改变Time设定时间间隔。

所以,我想你们理解我真正的问题。谢谢你的帮助。

4

1 回答 1

2

You can use a pattern like this to ensure that if this particular method is started in a new thread pool thread it will do nothing if a previous instance is already running.

class Foo
{
    //make static if this is called from different instances and should still
    //be synchronized
    private int isRunning = 0;
    public void DoStuff()
    {
        if (Interlocked.Exchange(ref isRunning, 1) == 0)
        {
            try
            {
                DoRealStuff();
            }
            finally
            {
                Interlocked.Exchange(ref isRunning, 0);
            }
        }
    }
}
于 2013-07-09T14:03:57.440 回答