问题记录Delegate to an instance method cannot have null 'this'
Posted on 2020-03-09 13:07 时间之外 阅读(895) 评论(0) 编辑 收藏 举报最近在写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的方法,自己订阅自己。
很惭愧到现在还出现这种低级问题,还是得看书,基础不能拉下啊。。
我们都是阴沟里的虫子,但总得有人仰望星空。