protected virtual void WriteAsyn(MvaLoggerEventArgs ea) { this.queue.Enqueue(ea); if (!Monitor.TryEnter(this, 1000)) { return; } try { //若還沒結束執行, 先return if (this.task != null && !this.task.IsEnd()) { return; } this.CloseTask(); this.task = MvaTask.RunLoop(() => { MvaLoggerEventArgs myea; lock (this) { if (!this.queue.TryDequeue(out myea)) { return(true); //取不出來就下次再取 } } this.WriteSyn(myea); //若Count等於零, 這個task會結束, IsEnd() = true return(this.queue.Count > 0); }); } finally { Monitor.Exit(this); } }
private void btnStartPns0102_Click(object sender, EventArgs e) { if (this.task != null) { return; } this.task = MvaTask.RunLoop(() => { //大迴圈, 來回一次 try { robotHandler.SgsVerifyStartPns0102(ri => { this.Invoke(new Action(() => { tbPoseX.Text = ri.x + ""; tbPoseY.Text = ri.y + ""; tbPoseZ.Text = ri.z + ""; tbPoseW.Text = ri.w + ""; tbPoseP.Text = ri.p + ""; tbPoseR.Text = ri.r + ""; })); }); return(true); } catch (Exception ex) { this.LogWrite(ex.Message); return(false); } }, 1000); }
public int StartAsyn() { this.m_keepConnection = MvaTask.RunLoop(() => { this.Write(EnumMacHalPlcVariable.PC_TO_PLC_CheckClock, false); if (!SpinWait.SpinUntil(() => !this.Read <bool>(EnumMacHalPlcVariable.PC_TO_PLC_CheckClock_Reply), 2500)) { this.LockAssign(ref this.m_isConnected, false); return(false); } this.LockAssign(ref this.m_isConnected, true); this.Write(EnumMacHalPlcVariable.PC_TO_PLC_CheckClock, true); if (!SpinWait.SpinUntil(() => this.Read <bool>(EnumMacHalPlcVariable.PC_TO_PLC_CheckClock_Reply), 2500)) { this.LockAssign(ref this.m_isConnected, false); //throw new MvException("PLC connection T1 timeout"); return(false); } else { this.LockAssign(ref this.m_isConnected, true); } return(true); }, 1000); return(0); }
public static void DisposeTask(MvaTask task) { if (task == null) { return; } task.Dispose(); }
void TaskClose() { if (this.task != null) { using (var obj = this.task) obj.Cancel(); this.task = null; } }
public static bool DisposeTaskTry(MvaTask task) { try { DisposeTask(task); return(true); } catch (Exception ex) { MvaLog.Warn(ex); return(false); } }
public void Close() { if (this.PlcLdd != null) { using (var obj = this.PlcLdd) { obj.NLPLC_ClosePort(); this.PlcLdd = null; } } if (this.m_keepConnection != null) { using (var obj = this.m_keepConnection) { obj.Cancel(); SpinWait.SpinUntil(() => obj.IsEnd(), 1000); } this.m_keepConnection = null; } }
private void Button4_Click(object sender, EventArgs e) { if (this.task != null) { return; } if (this.task != null && !this.task.IsEnd()) { return; } this.task = MvaTask.RunLoop(() => { //大迴圈, 來回一次 try { robotHandler.SgsVerifyStartPns0102(ri => { this.Invoke(new Action(() => { X_VALUE.Text = ri.x + ""; Y_VALUE.Text = ri.y + ""; Z_VALUE.Text = ri.z + ""; W_VALUE.Text = ri.w + ""; P_VALUE.Text = ri.p + ""; R_VALUE.Text = ri.r + ""; E1_VALUE.Text = ri.e1 + ""; })); }); return(true); } catch (Exception ex) { this.LogWrite(ex.Message); return(false); } }, 1000); }
public async Task Run(string command) { Console.WriteLine("command is :" + command); try { switch (command) { case "bingnews": var task = new BingNewsTask(); List <BingNews> bingNewsList = await task.GetNews("微软"); Console.WriteLine(bingNewsList.Count); foreach (BingNews bingNews in bingNewsList) { await Echo(bingNews.Title); } List <BingNews> bingNewsList1 = await task.GetNews("科技"); Console.WriteLine(bingNewsList.Count); foreach (BingNews bingNews in bingNewsList1) { await Echo(bingNews.Title); } await Echo("Done"); break; case "c9article": var task1 = new Channel9Task(); //获取最近5页articles for (int i = 5; i >= 1; i--) { List <C9Articles> articles = await task1.SaveArticles(i); if (articles == null) { continue; } foreach (C9Articles c9Article in articles) { await Echo("article:" + c9Article?.Title); } } // 更新视频页内容 var videos = await task1.SaveVideosAsync(0, 60); if (videos != null) { foreach (C9Videos video in videos) { await Echo("video:" + video?.Title); } } await Echo("Done"); break; case "mvavideos": var task2 = new MvaTask(); List <MvaVideos> re = await task2.SaveMvaVideo(); foreach (MvaVideos video in re) { var newDetails = task2.GetMvaDetailAsync(video).Result; await Echo("video:" + video?.Title); if (newDetails?.Count > 0) { await Echo("\t包括子视频:" + newDetails?.Count + "个"); } else { await Echo("\t没有子视频或获取子视频失败"); } } await Echo("Done"); break; case "mvadetails": var task3 = new MvaTask(); List <MvaVideos> videoList = await task3.UpdateRecentDetailAsync(); foreach (MvaVideos video in videoList) { await Echo("video:" + video?.Title); } await Echo("Done"); break; case "devblogs": var task4 = new DevBlogsTask(); var rssnews = task4.GetNewsAsync().Result; foreach (var news in rssnews) { await Echo("devblog:" + news?.Title); } await Echo("Done"); break; default: break; } } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// 自动执行任务 /// </summary> public static void StartAutoTask() { while (true) { int hour = DateTime.Now.ToLocalTime().Hour; if (hour != 7 && hour != 12 && hour != 18) { Thread.Sleep(60 * 60 * 1000); continue; } ; string fileName = "./AutoTask/Task-" + DateTime.Now.ToLocalTime().Date.ToString("yyyy-MM-dd") + ".txt"; try { Console.WriteLine(hour + ": Task start "); Log.Write(fileName, $"{hour}点 采集内容:"); Log.Write(fileName, "rssNews Start!"); var task3 = new DevBlogsTask(); var rssnews = task3.GetNewsAsync().Result; foreach (var news in rssnews) { Log.Write(fileName, "\t" + news?.Title); } Log.Write(fileName, "rssNews End!\n"); Log.Write(fileName, "BingNewsTask Start!"); var task = new BingNewsTask(); var bingNewsList = task.GetNews("微软").Result; foreach (BingNews bingNews in bingNewsList) { Log.Write(fileName, "\t" + bingNews?.Title); } var bingNewsList1 = task.GetNews("科技").Result; foreach (BingNews bingNews in bingNewsList1) { Log.Write(fileName, "\t" + bingNews?.Title); } Log.Write(fileName, "BingNewsTask End!\n"); //TODO: 处理登录 Log.Write(fileName, "Channel9Task Start!"); var task1 = new Channel9Task(); //获取最近5页articles for (int i = 5; i >= 1; i--) { var articles = task1.SaveArticles(i).Result; if (articles == null) { continue; } foreach (C9Articles c9Article in articles) { Log.Write(fileName, "\t" + c9Article?.Title); } } // 更新视频页内容 var videos = task1.SaveVideosAsync(0, 60).Result; if (videos != null) { foreach (C9Videos video in videos) { Log.Write(fileName, "\t" + video?.Title); } } Log.Write(fileName, "Channel9Task End!\n"); Log.Write(fileName, "MVATask Start!"); var task2 = new MvaTask(); var re = task2.SaveMvaVideo().Result; //更新视频 详细内容 foreach (MvaVideos video in re) { var newDetails = task2.GetMvaDetailAsync(video).Result; Log.Write(fileName, "\t" + video?.Title); Log.Write(fileName, "\t 包括子视频:" + newDetails.Count + "个"); } Log.Write(fileName, "MVATask End!\n"); Log.Write(fileName, "=========Finished==========\n"); } catch (Exception e) { Console.WriteLine(e.Source + e.Message + e.InnerException); Log.Write(fileName, e.Source + e.Message); } Thread.Sleep(60 * 60 * 1000); } }