顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2009年3月8日 星期日

開啟/關閉 WIFI 裝置

問題
http://topic.csdn.net/u/20080728/20/a5efe62a-629c-4811-add5-bc6c53fcdd5b.html

解法

Windows Mobile 中开关 WIFI 的“通用”代码

SystemState.WiFiStatePowerOn Property  讀取WIFI狀態的屬性,但無法設置開關
英文的 ><

Deactivate /Activate all Services like (Internet,WIFI,Bluetooth,WLAN..)

Enabling Radio in the Windows Mobile 5.0

WiFi Power On Event

開啟D0 關閉D4

We can implement this functionality with ease. Hope this can give your some idea.

See the code below:

// D0 indicates open; D4 indicates close;

CEDEVICE_POWER_STATE state = D0;

HRESULT hr = SetDevicePower(TEXT(""),POWER_NAME,state);

Note: We use SetDevicePower function here. It sets the device power state for a device.

  1. 1st parameter: specify the device.
  2. 2nd parameter: we use POWER_NAME here to indicate that the first parameter is just a device name.
  3. 3rd parameter: sets the power state.

In order to get information in details, you can find it in MSDN. Thanks!

Regards,

Zero Dai - MSFT



之後
Battery Status

GPS Application

Mobile GPS

2009年3月5日 星期四

在不同執行緒下存取UI控制項(textbox combobox)

問題描述 :在不同執行緒下呼叫方法,方法裡面需要存取主執行緒下的UI控制項

解法:

1.Form.CheckForIllegalCrossThreadCalls = False

2.建立委派

3.使用BackgroundWorker


這裡介紹第二種

首先在windows.form 的 onload事件建置一條執行緒

compareandprintout = new Thread(new ThreadStart(MRTcompareThread));
compareandprintout.Start();

這條thread 會執行compare()方法,存取UI控制項的程式就寫在裡面(存取textbox,combobox)
public void MRTcompareThread()
{
compare(); //裡面有無限迴圈
}

委派的建立!!


1.建立方法

publik int XXmethod(int a, int b);

2.宣告委派;委派參數,需要與被呼叫的方法一樣,包含回傳型別 (相同 signature)

   private delegate void XXDelegate();
   private delegate int XXDelegate(int a, int b);

3.引用委派(實體化),後指定方法

XXDelegate d = new XXDelegate(XXMethod); //參考的方法只要名稱就可以了
4.呼叫函數

invoke();


真的開始囉~~

於class form1裡宣告委派

delegate void SetTextCallback(Control ctl,String str); 設置textbox.text
delegate string GetTextCallback(Control ctl); 讀取textbox.text
delegate string GetCBTextCallback(ComboBox ctl, int i); 讀取ComboBox
delegate string GetCBTextCallback(Listcontrol ctl, int i); 讀取ComboBox


另外要寫委派方法
public string gettext(Control ctl)
{
if (this.InvokeRequired) //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.
{

GetTextCallback d = new GetTextCallback(gettext);
// this.Invoke(d);
string x;
x =(string) this.Invoke(d, new Object[] { ctl });
return x;
}
else
{
return ctl.Text;
}

}
public void settext(Control ctl,String str)
{
if (this.InvokeRequired)
{
SetTextCallback s = new SetTextCallback(settext);
this.Invoke(s, ctl, str );
}
else
{
ctl.Text = str;
}
}
public string getCBtext(ComboBox ctl, int i)
{
if (this.InvokeRequired)
{
string try1 = ctl.GetItemText(i); //i = 1 2 3 4 遞增,奇怪~難道我誤會MSDN? 給出來的是combobox的item索引
string try2 = (string)ctl.Items[i];//

GetCBTextCallback Gcb = new GetCBTextCallback(getCBtext); //建立
string x;
x = (string)this.Invoke(Gcb, ctl, i);
return x;
}
else
{
return (string) ctl.Items[i];
// return ctl.GetItemText(i);
}
}


在非產生UI控制項的副執行緒主程式 compare裡面有

while(true)
{
for (i = 1; i < style="color: rgb(51, 204, 255);"> getCBtext(comboBox1,i) && startstation == true)
//if (nowBox.Text == comboBox1.Items[i].ToString() )
{
//onBox.Text = comboBox1.Items[i].ToString(); //debug會出錯(不同控制項執行緒叫用 ... 我忘了)
string y = getCBtext(comboBox1,i) ;
settext(onBox, y); //叫用方法,自動判斷叫用invoke

}
}

for (i = 1; i < style="color: rgb(255, 0, 0);">// if (onBox.Text == comboBox1.Items[i].ToString())
{
if (goal > getonposition)
{
//nexterrorstop = comboBox1.Items[i - 1].ToString();
nexterrorstop = getCBtext(comboBox1,i-1);
}
else
{
nexterrorstop = "error";
}
}
}



}


VB版
一樣 ,先建立一個拿來更新 UI 的方法( UpdateUI),再建立一個有相同 signature 的委派( UpdateUICallBack)

Private Delegate Sub UpdateUICallBack(ByVal newText As String, ByVal c As Control)

Private Sub UpdateUI(ByVal newText As String, ByVal c As Control)
If Me.InvokeRequired() Then
Dim cb As New UpdateUICallBack(AddressOf UpdateUI)
Me.Invoke(cb, newText, c)
Else
c.Text = newText
End If
End Sub


ref:

舊文重發:Windows 表單與多執行緒


HOW TO:進行對 Windows Form 控制項的安全執行緒呼叫

VB/ VBA/ C#/ Java/ C++ 語言學習筆記

2009年1月16日 星期五

ThreadPool..::.QueueUserWorkItem 方法 (WaitCallback, Object)

參考 MSDN  http://msdn.microsoft.com/zh-tw/library/4yd16hza.aspx

// This example shows how to create an object containing task

// information, and pass that object to a task queued for
// execution by the thread pool.
using System;
using System.Threading;

// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public class TaskInfo {
// State information for the task. These members
// can be implemented as read-only properties, read/write
// properties with validation, and so on, as required.
public string Boilerplate;
public int Value;

// Public constructor provides an easy way to supply all
// the information needed for the task.
public TaskInfo(string text, int number) {
Boilerplate = text;
Value = number;
}
}

public class Example {
public static void Main() {
// Create an object containing the information needed
// for the task.
TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);

// Queue the task and data.
if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti)) {
Console.WriteLine("Main thread does some work, then sleeps.");

// If you comment out the Sleep, the main thread exits before
// the ThreadPool task has a chance to run. ThreadPool uses
// background threads, which do not keep the application
// running. (This is a simple example of a race condition.)
Thread.Sleep(1000);

Console.WriteLine("Main thread exits.");
}
else {
Console.WriteLine("Unable to queue ThreadPool request.");
}
}

// The thread procedure performs the independent task, in this case
// formatting and printing a very simple report.
//
static void ThreadProc(Object stateInfo) {
TaskInfo ti = (TaskInfo) stateInfo;
Console.WriteLine(ti.Boilerplate, ti.Value);
}
}
///////////////////////
當執行此敘述 if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti
ti會一起傳送到 ThreadProc(Object stateInfo) 裡面的
stateInfo
因此 在
ThreadProc(Object stateInfo)中 需要轉換 Object 為 TaskInfo 型態再轉存一次

2009年1月12日 星期一

淺談多執行緒

參考

C# 隨性筆記


淺談多執行緒(一) --多執行緒介紹:定義造物件,與啟動

淺談多執行緒(二) --多執行緒共用資料的碰撞問題與LOCK

淺談多執行緒(三) --多執行緒死結deadlock 與使用Monitor.TryEnter方法鎖定

補充:

1.參考MSDN
  • 建立和執行執行緒
  • 執行緒的同步化
  • 執行緒間的互動
  • 使用執行緒集區
  • 使用 Mutex 物件來保護共用的資源
http://msdn.microsoft.com/zh-tw/library/aa288472(VS.71).aspx

2.用於非同步的
ManualResetEvent
http://www.webjx.com/htmldata/2006-08-14/1155517019.html

2008年10月3日 星期五