国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - 淺談C#跨線程調用窗體控件(比如TextBox)引發的線程安全問題

淺談C#跨線程調用窗體控件(比如TextBox)引發的線程安全問題

2022-02-10 15:35絳河 C#

下面小編就為大家分享一篇淺談C#跨線程調用窗體控件(比如TextBox)引發的線程安全問題,具有很好的參考價值,希望對大家有所幫助

如何:對 Windows 窗體控件進行線程安全調用

訪問 Windows 窗體控件本質上不是線程安全的。 如果有兩個或多個線程操作某一控件的狀態,則可能會迫使該控件進入一種不一致的狀態。 還可能會出現其他與線程相關的 Bug,例如爭用情況和死鎖。 確保以線程安全方式訪問控件非常重要。

在未使用 Invoke 方法的情況下,從不是創建某個控件的線程的其他線程調用該控件是不安全的。 以下非線程安全的調用的示例。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This event handler creates a thread that calls a
  // Windows Forms control in an unsafe way.
  private void setTextUnsafeBtn_Click(
   object sender,
   EventArgs e)
  {
   this.demoThread =
    new Thread(new ThreadStart(this.ThreadProcUnsafe));
   this.demoThread.Start();
  }
  // This method is executed on the worker thread and makes
  // an unsafe call on the TextBox control.
  private void ThreadProcUnsafe()
  {
   this.textBox1.Text = "This text was set unsafely.";
  }

.NET Framework 可幫助您檢測以非線程安全方式訪問控件這一問題。 在調試器中運行應用程序時,如果一個不是創建某個控件的線程的其他線程調用該控件,則調試器會引發一個 InvalidOperationException,并顯示以下消息:“從不是創建控件控件名稱 的線程訪問它。”

此異常在調試期間和運行時的某些情況下可靠地發生。 在調試以 .NET Framework 2.0 版之前的 .NET Framework 編寫的應用程序時,可能會出現此異常。 我們強烈建議您在發現此問題時進行修復,但您可以通過將 CheckForIllegalCrossThreadCalls 屬性設置為 false 來禁用它。(不推薦)

對 Windows 窗體控件進行線程安全調用

查詢控件的 InvokeRequired 屬性。

如果 InvokeRequired 返回 true,則使用實際調用控件的委托來調用 Invoke。

如果 InvokeRequired 返回 false,則直接調用控件。

在下面的代碼示例中,將在由后臺線程執行的 ThreadProcSafe 方法中實現線程安全調用。 如果 TextBox 控件的 InvokeRequired 返回 true,則 ThreadProcSafe 方法會創建 SetTextCallback 的一個實例,并將該實例傳遞給窗體的 Invoke 方法。 這使得 SetText 方法被創建 TextBox 控件的線程調用,而且在此線程上下文中將直接設置 Text 屬性。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// This event handler creates a thread that calls a
  // Windows Forms control in a thread-safe way.
  private void setTextSafeBtn_Click(
   object sender,
   EventArgs e)
  {
    this.demoThread =
    new Thread(new ThreadStart(this.ThreadProcSafe));
    this.demoThread.Start();
  }
 
  // This method is executed on the worker thread and makes
  // a thread-safe call on the TextBox control.
  private void ThreadProcSafe()
  {
   this.SetText("This text was set safely.");
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
 
namespace CrossThreadDemo
{
 public class Form1 : Form
 {
  // This delegate enables asynchronous calls for setting
  // the text property on a TextBox control.
  delegate void SetTextCallback(string text);
 
  // This thread is used to demonstrate both thread-safe and
  // unsafe ways to call a Windows Forms control.
  private Thread demoThread = null;
 
  // This BackgroundWorker is used to demonstrate the
  // preferred way of performing asynchronous operations.
  private BackgroundWorker backgroundWorker1;
 
  private TextBox textBox1;
  private Button setTextUnsafeBtn;
  private Button setTextSafeBtn;
  private Button setTextBackgroundWorkerBtn;
 
  private System.ComponentModel.IContainer components = null;
 
  public Form1()
  {
   InitializeComponent();
  }
 
  protected override void Dispose(bool disposing)
  {
   if (disposing && (components != null))
   {
    components.Dispose();
   }
   base.Dispose(disposing);
  }
 
  // This event handler creates a thread that calls a
  // Windows Forms control in an unsafe way.
  private void setTextUnsafeBtn_Click(
   object sender,
   EventArgs e)
  {
   this.demoThread =
    new Thread(new ThreadStart(this.ThreadProcUnsafe));
 
   this.demoThread.Start();
  }
 
  // This method is executed on the worker thread and makes
  // an unsafe call on the TextBox control.
  private void ThreadProcUnsafe()
  {
   this.textBox1.Text = "This text was set unsafely.";
  }
 
  // This event handler creates a thread that calls a
  // Windows Forms control in a thread-safe way.
  private void setTextSafeBtn_Click(
   object sender,
   EventArgs e)
  {
   this.demoThread =
    new Thread(new ThreadStart(this.ThreadProcSafe));
 
   this.demoThread.Start();
  }
 
  // This method is executed on the worker thread and makes
  // a thread-safe call on the TextBox control.
  private void ThreadProcSafe()
  {
   this.SetText("This text was set safely.");
  }
 
  // This method demonstrates a pattern for making thread-safe
  // calls on a Windows Forms control.
  //
  // If the calling thread is different from the thread that
  // created the TextBox control, this method creates a
  // SetTextCallback and calls itself asynchronously using the
  // Invoke method.
  //
  // If the calling thread is the same as the thread that created
  // the TextBox control, the Text property is set directly.
 
  private void SetText(string text)
  {
   // InvokeRequired required compares the thread ID of the
   // calling thread to the thread ID of the creating thread.
   // If these threads are different, it returns true.
   if (this.textBox1.InvokeRequired)
   {
    SetTextCallback d = new SetTextCallback(SetText);
    this.Invoke(d, new object[] { text });
   }
   else
   {
    this.textBox1.Text = text;
   }
  }
 
  // This event handler starts the form's
  // BackgroundWorker by calling RunWorkerAsync.
  //
  // The Text property of the TextBox control is set
  // when the BackgroundWorker raises the RunWorkerCompleted
  // event.
  private void setTextBackgroundWorkerBtn_Click(
   object sender,
   EventArgs e)
  {
   this.backgroundWorker1.RunWorkerAsync();
  }
  
  // This event handler sets the Text property of the TextBox
  // control. It is called on the thread that created the
  // TextBox control, so the call is thread-safe.
  //
  // BackgroundWorker is the preferred way to perform asynchronous
  // operations.
 
  private void backgroundWorker1_RunWorkerCompleted(
   object sender,
   RunWorkerCompletedEventArgs e)
  {
   this.textBox1.Text =
    "This text was set safely by BackgroundWorker.";
  }
 
  #region Windows Form Designer generated code
 
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.setTextUnsafeBtn = new System.Windows.Forms.Button();
   this.setTextSafeBtn = new System.Windows.Forms.Button();
   this.setTextBackgroundWorkerBtn = new System.Windows.Forms.Button();
   this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
   this.SuspendLayout();
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(12, 12);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(240, 20);
   this.textBox1.TabIndex = 0;
   //
   // setTextUnsafeBtn
   //
   this.setTextUnsafeBtn.Location = new System.Drawing.Point(15, 55);
   this.setTextUnsafeBtn.Name = "setTextUnsafeBtn";
   this.setTextUnsafeBtn.TabIndex = 1;
   this.setTextUnsafeBtn.Text = "Unsafe Call";
   this.setTextUnsafeBtn.Click += new System.EventHandler(this.setTextUnsafeBtn_Click);
   //
   // setTextSafeBtn
   //
   this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55);
   this.setTextSafeBtn.Name = "setTextSafeBtn";
   this.setTextSafeBtn.TabIndex = 2;
   this.setTextSafeBtn.Text = "Safe Call";
   this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click);
   //
   // setTextBackgroundWorkerBtn
   //
   this.setTextBackgroundWorkerBtn.Location = new System.Drawing.Point(177, 55);
   this.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn";
   this.setTextBackgroundWorkerBtn.TabIndex = 3;
   this.setTextBackgroundWorkerBtn.Text = "Safe BW Call";
   this.setTextBackgroundWorkerBtn.Click += new System.EventHandler(this.setTextBackgroundWorkerBtn_Click);
   //
   // backgroundWorker1
   //
   this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
   //
   // Form1
   //
   this.ClientSize = new System.Drawing.Size(268, 96);
   this.Controls.Add(this.setTextBackgroundWorkerBtn);
   this.Controls.Add(this.setTextSafeBtn);
   this.Controls.Add(this.setTextUnsafeBtn);
   this.Controls.Add(this.textBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);
   this.PerformLayout();
 
  }
 
  #endregion
 
 
  [STAThread]
  static void Main()
  {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
  }
 
 }
}

以上這篇淺談C#跨線程調用窗體控件(比如TextBox)引發的線程安全問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/janghe/archive/2017/11/21/7874849.html

延伸 · 閱讀

精彩推薦
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 成人午夜网| 中文字幕在线观看日本 | 亚洲一区二区精品 | 四虎影院网站 | 国产日产久久高清欧美一区 | 国产免费视频在线 | 天天干女人网 | 黄色免费毛片 | 青青久久av北条麻妃黑人 | 在线国产专区 | av免费资源| 国产精品视屏 | 日韩国产| 97久久精品人人做人人爽50路 | 国产福利一区二区 | 日韩av在线一区 | 国产91精品久久久久 | 国产午夜久久 | 国产在线观看91一区二区三区 | 99pao成人国产永久免费视频 | 蜜臀网 | 韩日在线| 久久精品亚洲精品国产欧美kt∨ | 久久se精品一区精品二区 | 国产黄色a级 | 在线观看91 | 欧美日韩午夜 | 色av中文字幕 | 久久国产综合 | 一区二区在线影院 | 国内偷拍av | 久久精品超碰 | 欧美日韩亚洲成人 | 转生成为史莱姆这档事第四季在线观看 | 色多多导航 | 五月婷婷在线观看视频 | 欧美一区二区三区在线观看视频 | www成人精品 | 欧美日韩三级在线 | 国产成人精品久久二区二区91 | 亚洲综合激情 |