Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, i wanna ask you about thread in webservice, i have two main threads that will handle some process.
Example : Thread A = > Handle process 1.
Thread B = > Handle process 2.

When the message come into my webservice, Thread A will process the message (this process is inserting data into database).
Simultanously, Thread B always check is exist data in database, if exist Thread B will read some messages in the database and processing it tobe a meaningful data. As your information, Thread B will never be die, may be like a "daemon" i think :-\ . So, when Thread A has finished its process, Thread B is always run eventhough the data in database is not exist. But unfortunately, while Thread B is still run, sudden an error has occured : "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack".
I have tried to restart the iis but it doesn't work.. :~ It has made me like a stupid boy... arrrgghhh... :mad:

This is my code :

C#
[WebMethod]
    public void MethodOAQ(byte[] messages, string memberCode)
    {
      CController cc = new CController(memberCode, messages, 6);
      BasicController bc = new BasicController();
      Thread oThread1 = new Thread(() => cc._SaveMsgToTbl_());
      Thread oThread2 = new Thread(() => bc._BackgroundWorker(null));

      oThread2.IsBackground = true;

      oThread1.Start();
      oThread2.Start();
    }


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Threading;
using System.ComponentModel;

/// <summary>
/// Summary description for CController
/// This class will control first task.
/// </summary>
public class CController
{
  CExecutorQuery ceq;
  private readonly string memberid = string.Empty;
  private readonly byte[] content = null;
  private readonly int type_msg;

  public CController(string memberid, byte[] content, int type_msg) 
  {
    this.memberid = memberid;
    this.content = content;
    this.type_msg = type_msg;
  }

  public int SaveMsgToTbl() 
  {
    ceq = new CExecutorQuery();
    int kondisi = 0;

    try
    {
      ceq.INSERT_DATA_T_INCOMING_MSGS(Encoding.UTF8.GetString(content), DateTime.Now.ToString("yyyyMMdd"), memberid, "NEW", type_msg);
      kondisi = 1;
    }
    catch 
    {
      kondisi = 0;
    }
    return kondisi;
  }

  public void _SaveMsgToTbl_() 
  {
    ceq = new CExecutorQuery();
    ceq.INSERT_DATA_T_INCOMING_MSGS(Encoding.UTF8.GetString(content), DateTime.Now.ToString("yyyyMMdd"), memberid, "NEW", type_msg);
  }
}

internal sealed class BasicController 
{
  private CReaderConfig crc;
  private ClassPesan cp;
  private CExecutorQuery ceq;

  public void _BackgroundWorker(object sender) 
  {
    if (IS_THREAD_ALIVE.Equals(false)) 
    {
      IS_THREAD_ALIVE = true;
      ThreadPool.QueueUserWorkItem(new WaitCallback(_Process), null);   
    }
  }

  private void _Process(object sender) 
  {
    crc = new CReaderConfig();
    ceq = new CExecutorQuery();
    IDictionary<string, IDictionary<int, string>> KAMUS = new Dictionary<string, IDictionary<int, string>>();

    while (true) 
    {
      System.Threading.Thread.Sleep(1000);
      foreach (string membercode in crc.List_Payment_Bank())
      {
        KAMUS.Add(membercode, ceq.COLLECT_DATA_T_INCOMING_MSG(membercode, "NEW"));
      }
      foreach (KeyValuePair<string, IDictionary<int, string>> dict in KAMUS) 
      {
        foreach (KeyValuePair<int, string> pair in dict.Value) 
        {
          try
          {
            cp = new ClassPesan(Encoding.UTF8.GetBytes(pair.Value), dict.Key);
            cp.PROCESS_DATA(); // While execute this method an error occured
            ceq.DO_UPDATE_DATA_T_INCOMING_MSGS(dict.Key, pair.Key);
          }
          catch (Exception ex)
          {
            continue;
          }
        }
      }
    }
  }


NB : I think this has nothing to do with the "Response.Redirect" because of this issue to talk about webservice.

Would you please help me ?? :confused:
Posted
Comments
Teamsar Muliadi 7-Oct-10 7:08am    
Is there anybody can help me..??

1 solution

My understanding of a webservice is that it can be unloaded by the host e.g. IIS at any point after a call. This would make it unsafe to start a thread and leave it running after a web service call has completed which is what your example is doing. You wil either have to wait for your threads to complete or rethink your strategy.

the client to the web service can make asychronous webmethod calls though.

My advice is keep a webservice call a complete operation that is complete at the end of the call.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900