⑴ 安卓如何獲取程序最上層ui控制項信息,比如說控制項上的文本內容
比較簡單的寫法,在你需要讀取的時候,直接string str="";this.Invoke((EventHandler)(delegate{ // 這里寫你的控制項代碼,比如 //str= target.SelectedText; }));至於普通的寫法怎麼寫,你搜索一下C#跨線程訪問就算。
⑵ 多線程讀取UI控制項內容線程安全嗎
線程安全就是多線程訪問時,採用了加鎖機制,當一個線程訪問該類的某個數據時,進行保護,其他線程不能進行訪問直到該線程讀取完,其他線程才可使用。不會出現數據不一致或者數據污染。
可以在Thread中向Handler發送消息,Handler的處理則在View中,Handler會判斷消息類型,並根據消息類型更新View。
⑶ c# 跨線程 讀取 主線程UI界面上的控制項屬性值應該怎麼編寫程序
在輔助線程調用下面的方法InvokeControl(); //(在創建輔助線程時可將此方法通過delegate傳到輔助線程中)
//寫在主線程中(windows控制項)
private void InvokeControl()
{
if (this.InvokeRequired)
this.Invoke(new DelegateChangeText(ChangeText));
else
this.ChangeText();
}
private void ChangeText()
{
this.TextBox.Text = "sd";
}
public delegate void DelegateChangeText();
我在窗體內 創建了線程A用於監聽連接 怠粻壁餃撰祭辯熄菠隴 然後在接受到連接後 由線程A創建了線程B來處理連接 在線程B內 我要改寫窗體上控制項的屬性 請問怎麼弄?
⑷ C# 子線程獲取Ui線程的控制項屬性問題
比較簡單的寫法,在你需要讀取的時候,直接
stringstr="";
this.Invoke((EventHandler)(delegate
{
//這里寫你的控制項代碼,比如
//str=target.SelectedText;
}
));
至於普通的寫法怎麼寫,你搜索一下C#跨線程訪問就算。
⑸ C#多線程與UI響應 跨線程更新UI
lblStatus.Text = "執行中,請稍候……"; Func<int> longTask = new Func<int>(delegate(){ // 模擬長時間任務 Thread.Sleep(2000); // 返回任務結果:5 return 5;});// 發起一次非同步調用,實際上就是在.net線程池中執行longTask// 這時由於是其它線程在工作,UI線程未被阻塞,所以窗體不會假死longTask.BeginInvoke(ar =>{ // 使用EndInvoke獲取到任務結果(5) int result = longTask.EndInvoke(ar); // 使用Control.Invoke方法將5顯示到一個label上,如果沒有Invoke, // 直接寫lblStatus.Text="5",將會拋出跨線程訪問UI控制項的異常 Invoke(new Action(() => lblStatus.Text = "執行結果是:" + result));}, null);
⑹ 如何實時更新ui,datagridview 跨線程訪問控制項
很多時候,我開發軟體的時候需要開啟線程到後台處理大數據,不斷更新資料庫,但又要同時修改前台UI,比如迅雷的下載任務,開多個線程去下載,顯示層UI也要同時體現給用戶知道,顯示當前用戶下載進度等,這樣必須用到多線程,但是C#中多線程處理問題涉及到一個「界面控制項不能跨線程」,因為微軟認為這樣會導致線程不安全問題,那麼我們一般怎麼處理這樣的問題,既要處理數據,又要實時顯示 。轉載
看設計界面
資料庫用到的是sqlite 英文sqlite是多線程的 但同一時間只能一個線程操作資料庫,
所以要用到線程同步問題
我用LOCK進行線程同步處理
lock (ModifStatic.o)
{
queue = queueBLL.GetModel(i);
queueNew = queue;
queueNew.remainNum++;
queueBLL.Update(queueNew);
}
這里又一點要注意
因為lock 裡面的對象必須是一個對象,而且是全局 的 所以我吧它放到一個靜態類裡面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test.BLL
{
public static class ModifStatic
{
private static int id = 1;
public static int Id
{
get { return ModifStatic.id; }
set { ModifStatic.id = value; }
}
public static object o = new object();
}
}
這個靜態類還有一個 就是記錄當前是修改哪一個id的數據的
這樣我們就可以只針對某個row進行修改 而不是全部整個datagridview修改,否則就顯得很卡
用戶會感覺界面一閃一閃的
這個程序是模擬的
寫一個類來專門處理修改資料庫的 開一個線程用來處理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test.BLL
{
[Serializable]
public class ChangeDB
{
int i = 0;
Random r = new Random();
Test.Model.queue queue = null;
Test.Model.queue queueNew = null;
Test.BLL.queue queueBLL = new Test.BLL.queue();
public void DBAdd()
{
while (true)
{
i = r.Next(1,8);
lock (ModifStatic.o)
{
queue = queueBLL.GetModel(i);
queueNew = queue;
queueNew.remainNum++;
queueBLL.Update(queueNew);
}
ModifStatic.Id = i;
System.Threading.Thread.Sleep(5000);
}
}
}
}
主頁面UI
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
[Serializable]
public partial class FormMain : Form
{
Test.Model.queue queue = null;
Test.BLL.queue queueBLL = new Test.BLL.queue();
Test.BLL.ChangeDB changeDbBLL = new Test.BLL.ChangeDB();
private delegate void myDelegate(int id);//定義委託
//初始化UI
public FormMain()
{
InitializeComponent();
this.dataGridView1.DataSource = queueBLL.GetAllList();
this.dataGridView1.DataMember = "ds";
}
//啟動更新線程
Thread myThread;
private void FormMain_Load(object sender, EventArgs e)
{
myThread = new Thread(startFillDv);//實例化線程
myThread.IsBackground = true;
myThread.Start();
}
//不斷更新UI
private void startFillDv()
{
while (true)
{
lock (Test.BLL.ModifStatic.o)
{
Grid(Test.BLL.ModifStatic.Id);
}
Thread.Sleep(3000);//更新頻率為3秒
}
}
//更新UI
private void Grid(int id)
{
if (this.InvokeRequired)
{
this.Invoke(new myDelegate(Grid), new object[] { id });
}
else
{
try
{
//修改改id對應的行
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value) == id)
{
queue = queueBLL.GetModel(id);
this.dataGridView1.Rows[i].Cells[1].Value = queue.remainNum;
}
}
}
catch
{
}
}
}
//結束線程
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.myThread.IsAlive)
{
this.myThread.Abort();
}
}
//修改資料庫
Thread th = null;
private void buttonModif_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(changeDbBLL.DBAdd));
th.IsBackground = true;
th.Start();
}
}
}