zoukankan      html  css  js  c++  java
  • 问题记录Delegate to an instance method cannot have null 'this'

    最近在写Socket服务器程序,写了一个简易的消息队列用于数据入库,遇到了这样的问题“Delegate to an instance method cannot have null 'this'”。

    代码如下:

      1 using System;
      2 using System.Collections;
      3 using System.Collections.Generic;
      4 using System.Threading;
      5 using AmtServer.Common;
      6 using System.Linq;
      7 
      8 namespace AmtServer.DAL
      9 {
     10     public class DBJobProcessor
     11     {
     12         public readonly static DBJobProcessor Instance = new DBJobProcessor();
     13         //任务列表
     14         private List<JobArgs> j_list = new List<JobArgs>();
     15         private bool isdoingwork = false;
     16         private Thread jobthrd = new Thread(new ThreadStart(Instance.DoWork));
     17         private DBJobProcessor()
     18         {
     19             try
     20             {
     21                 Start();
     22             }
     23             catch (Exception ex)
     24             {
     25                 LogHelper.Logger.Error(ex);
     26             }
     27         }
     28         /// <summary>
     29         /// 开启线程
     30         /// </summary>
     31         public void Start()
     32         {
     33             jobthrd.IsBackground = true;
     34             jobthrd.Start();
     35         }
     36         /// <summary>
     37         /// 停止线程
     38         /// </summary>
     39         public void Stop()
     40         {
     41             try
     42             {
     43                 jobthrd.Abort();
     44             }
     45             catch (System.Exception ex)
     46             {
     47                 LogHelper.Logger.Error(ex);
     48             }
     49         }
     50 
     51         private void DoWork()
     52         {
     53             try
     54             {
     55                 while (true)
     56                 {
     57                     if (j_list.Count > 0)
     58                     {
     59                         if (!isdoingwork)
     60                         {
     61                             List<JobArgs> templst;
     62                             lock (((ICollection)j_list).SyncRoot)
     63                             {
     64                                 int n = 0;
     65                                 if (j_list.Count > 200)
     66                                 {
     67                                     n = 200;
     68                                 }
     69                                 else
     70                                 {
     71                                     n = j_list.Count;
     72                                 }
     73                                 JobArgs[] temp = new JobArgs[n];
     74                                 j_list.CopyTo(0, temp, 0, n);
     75                                 templst = new List<JobArgs>(temp);
     76                                 j_list.RemoveRange(0, n);
     77                             }
     78                             DoWriteDB(templst.ToArray());
     79                         }
     80                     }
     81                     else
     82                     {
     83                         Thread.Sleep(10000);
     84                     }
     85                 }
     86             }
     87             catch (Exception ex)
     88             {
     89                 LogHelper.Logger.Error(ex);
     90             }
     91         }
     92         //执行数据库操作
     93         private void DoWriteDB(JobArgs[] ls)
     94         {
     95             try
     96             {
     97                 isdoingwork = true;
     98                 int result = 0;
     99                 result += DapDBHelper.Execute(Constant.SQL_Position_Upsert, DapDBHelper.constr, ls);
    100                 LogHelper.Instance.ShowInfoLog($"插入{result}/{ls.Length}条");
    101             }
    102             catch (Exception ex)
    103             {
    104                 LogHelper.Logger.Error(ex);
    105                 LogHelper.Instance.ShowInfoLog("DoWriteDB_Error:"+ex.Message);
    106             }
    107             finally
    108             {
    109                 isdoingwork = false;
    110             }
    111         }
    112         /// <summary>
    113         /// 添加任务
    114         /// </summary>
    115         /// <param name="job"></param>
    116         public void AddJob(JobArgs job)
    117         {
    118             try
    119             {
    120                 lock (((ICollection)j_list).SyncRoot)
    121                 {
    122                     j_list.Add(job);
    123                 }
    124             }
    125             catch (Exception ex)
    126             {
    127                 LogHelper.Logger.Error(ex);
    128             }
    129         }
    130 
    131     }
    132 }

    问题出现在16行,Instance实例的线程订阅了Instance的方法,自己订阅自己。

    很惭愧到现在还出现这种低级问题,还是得看书,基础不能拉下啊。。

    我们都是阴沟里的虫子,但总得有人仰望星空。
  • 相关阅读:
    Java 7的javax.net.ssl.SSLHandshakeException
    Oracle数据泵导出数据库
    ORA-00054: 资源正忙 --锁表的解决方法
    Linux学习私人笔记-Shell基础
    Linux学习私人笔记-目录和文件的基本命令
    Linux学习私人笔记-文件基本命令
    Linux学习私人笔记-账号管理
    Linux学习私人笔记-Vim
    form提交表单时本地下载
    SQL Servel 中分页解决
  • 原文地址:https://www.cnblogs.com/BeyondTime/p/12448172.html
Copyright © 2011-2022 走看看