如何一次又一次地在主线程中调用函数.

编程入门 行业动态 更新时间:2024-10-26 19:35:50
本文介绍了如何一次又一次地在主线程中调用函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 请检查我的代码,

Hi guys, please check my code,

private void ButtonStartUp_Click(object sender, EventArgs e) { for (int i = 0; i < maxthread; i++) { QHThreadArray[i] = new TQHThread(); QHThreadArray[i].ThreadNo = i; QHThreadArray[i].QH_id = QH_id; QHThreadArray[i].Start(); } } private void Button_UpdateOS_Click(object sender, EventArgs e) { fileName = "osdays.csv"; if (!File.Exists(fileName)) { MessageBox.Show(fileName + "File Does Not Exist!"); } else if (File.Exists(fileName)) { //some code which reads contents from file and adds to a List<string> named DBPendingList in a Query format which can be directly given //to data base for execution; sendNextRequestFromList(); } } public static void sendNextRequestFromList() { while (DBPendingList.Count > 0) { int ThNum = -1; QH = null; //Where QH is an instance of TQHThread class for (int i = 0; i < maxthread; i++) { if (QHThreadArray[i].Status == "Running") { continue; } if (QHThreadArray[i].Status == "Suspended") { QH = QHThreadArray[i]; ThNum = i; break; } } if (ThNum == -1) break; // exit; used in Delphi code. if (QH != null) { QH.RequestStr = DBPendingList[0]; DBPendingList.RemoveAt(0); QH.Resume(); } } //Following "if" code is required to be eliminated, because the reason is given //below. if (DBPendingList.Count != 0) { ret = new retFreeThreadDelegate(sendNextRequestFromList); ret.BeginInvoke(null, null); } } public class TQHThread { public int ThreadNo; public string QH_id; public string RequestStr=null; public SqlCommand cmd; public SqlConnection con; public Thread t; public readonly object locker = new object(); public int result = -1; public void Start() { t = new Thread(new ThreadStart(this.Execute)); t.Start(); } public void Resume() { if (Status == "Suspended") t.Resume(); } public void Suspend() { if (Status == "Running") { t.Suspend(); } } public string Status { get { return t.ThreadState.ToString(); } } public void Execute() { con = new SqlConnection(File.ReadAllText("Connection" + QH_id + ".udl")); cmd = new SqlCommand(); cmd.Connection = con; con.Open(); if (RequestStr == null) { Suspend(); } while (this.Status != "Suspended") { try { cmd.CommandText = RequestStr; result=cmd.ExecuteNonQuery(); } catch (SqlException) { } catch (InvalidOperationException) { } frmQuoteHandler.UpdateDBComplete(this); } } } we want to return result to the main thread in UpdateDBComplete(). public static void UpdateDBComplete(TQHThread QH) { lock (locker) { if (QH != null) { int x = QH.result; QH.Suspend(); } } }</string>

如果代码在无法得到结果后还是不能进行任何工作的情况下仍能正常工作,则我想在MAIN线程中再次调用"sendNextRequestFromList()",即我无法执行,因此需要您的建议. > 由于使用了BeginInvoke,因此暂时无法使用,但在逻辑上不正确,也不是我想要的方式. 并告诉我在获得结果或特定线程空闲时可以在MAIN线程中调用"sendNextRequestFromList()"的可能方法. 等待很快的答复,并在此先感谢. 代码包装在"pre"标签中.[/编辑]

in case code is working bit properly BUT AFTER GETTING RESULT OUT OF THREAD OR WHENEVER A THREAD IS FREE TO WORK, I want to call "sendNextRequestFromList()" again in the MAIN thread, i.e. I am unable to perform so need your suggestion. For time being its working because of the BeginInvoke is used but its not the logically correct and the way I want. and just tell me the possible ways that i can call "sendNextRequestFromList()" in the MAIN thread whenever result obtained or whenever a particular thread is free. waiting for soon reply, and Thanks in advance. Code is wrapped in "pre" tag[/Edited]

推荐答案

已批准.您可能在同一线程上多次调用一个方法.不用担心说谢谢",没关系. :-)
—SA
Approved. You may call a method more than once on the same thread. Don''t bother about saying "thank you", it''s OK. :-)
—SA

更多推荐

如何一次又一次地在主线程中调用函数.

本文发布于:2023-11-27 23:53:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主线   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!