none
Thread里使用 回调功能,报错,说未将对象引用到实例化(callback) RRS feed

  • 问题

  • /////接口
    
    [ServiceContract(Name = "IServices", 
        Namespace = "http://www.wally.com.cn", 
        SessionMode = SessionMode.Required,
        CallbackContract = typeof(IServicesCallBack))]
      public interface IServices
      { 
        [OperationContract(IsOneWay=true,Name="StopMessage")]
        void StopMessage();
    
    }
    
    ////end 
    
    /////class
    
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Multiple,
        UseSynchronizationContext=true)]//TransactionAutoCompleteOnSessionClose=true,TransactionTimeout="00:03:00")]
      public class Services : IServices
      {
    public void SendMessage()//string message)
        {
          try
          {
            IsExistBroadcastMessage = true;
            int sleep = 1000 * 100;
            string strMess = "";
            Thread thread = new System.Threading.Thread(new ThreadStart(delegate
            {
              lock (obj)
              {
                while (IsExistBroadcastMessage)
                {
                  if (File.Exists(Application.StartupPath + (@"\BroadcastMessages.xml")))
                  {
                    XDocument xdoc = XDocument.Load(Application.StartupPath + (@"\BroadcastMessages.xml"));
                    var Messages = from o in xdoc.Descendants("Messages") select o;
    
                    foreach (var m in Messages)
                    {
                      strMess = m.Value;
                      sleep = (m.LastAttribute == null) ? (Convert.ToInt32(m.LastAttribute.Value.ToString())) : (1000 * 60 * 5);
                      break;
                    }
                    CallBack.BroadcastMessage(strMess);//这里报错(在使用多线程情况才会报错,如果不用多线程就可以正常使用)
                    Thread.Sleep(sleep);
                  }
                  else
                    Thread.Sleep(1000 * 60 * 5);
                }
              }
            }));
            thread.IsBackground = true;
            thread.Start();
          }
          catch (Exception ex)
          {
            WallySoft.IDAL.ILog log = new WallySoft.BLL.SystemLog();
            log.Write(ex, IDAL.LogType.Error);
          }
        }
    ///callback
    IServicesCallBack CallBack
        {
          get { return OperationContext.Current.GetCallbackChannel<IServicesCallBack>(); }
        }
    
    }
    


    认真学习,不耻下问.^_^
    2011年8月30日 7:48

全部回复