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

2012年3月8日 星期四

Using MCR in Windows Azure Web/Worker Role

ppt版本
找了眾多的方法,終於找到一種可以用的了
首先說明我使用的架構是User <=WCF=> Web Role <=Azure Queue=> Worker Role(Run MCR)
皆為64位元版本
在此只講Worker Role環境架設就好。

行前準備:
MCRInstaller.exe <== 自己想辦法得到
BootStrapper.exe <== Google "Windows Azure Bootstrapper"
vcredistx64.exe  <== Google一下,可以抓到
AzureStorageExplorer4.exe <== Google "AzureStorageExplorer" (可以先在本機端上安裝)

動作:
1.將取得的MCRInstaller.exe
(在Windows Server 2008 R2的作業系統上執行安裝一次,建議安裝路徑為:D:\Program Files\MATLAB\MATLAB Compiler Runtime\v714\runtime\win64\ ,此範例使用64位元 R2010b版本,不同版本路徑會有差異,注意一下就好)
(沒電腦的話,開一台Worker role用遠端桌面登入安裝好後,再抓回來是個笨方法,但有時還蠻好用的...)

2.到剛安裝的路徑D:\Program Files\,把MATLAB整個資料夾用"zip"壓縮起來,在此命為MATLAB.zip

3.開啟AzureStorageExplorer,在你的Blob中建立新的Container名為initfile

4.將MATLAB.zip上傳到雲端Blob的initfile下

5.將vcredistx64.exe上傳到雲端Blob的initfile下

6.開啟你的雲端專案,在ServiceDefinition.csdef 的WorkerRole部分,設定Startup 和 Runtime如下:


7.在WorkerRole的環境中增加連接字串 "ConnectionString" ,並將他定為放置MATLAB.zip的Storage位置

8.在WorkerRole的專案中加入新的檔案名為InstallVcredistx64.cmd,設定"永遠複製",其檔案內容如下:
d:\vcredistx64.exe /q

9.在WorkerRole的專案中加入BootStrapper.exe (和 BootStrapper.exe.config),設定"永遠複製"。

10.將D:\Program Files\MATLAB\MATLAB Compiler Runtime\v714\toolbox\dotnetbuilder\bin\win64\v2.0\MWArray.dll加到WorkerRole的專案中,順便將自己要用的DLL一起加入,並設定"永遠複製"。
注意:自己的DLL也要用相同本版的Matlab編譯成的才行

11. 確認其他的專案也弄好後,就可以發佈了,發佈的服務位置最好和Storage相同,不然起始時會很慢。

by Keng-li.Lin

2011年11月15日 星期二

Windows 作業系統的暫存檔功能

這是在 [小朱® 的技術隨手寫] 中找到的
由於暫存檔會交由WS自行清除,也不用費心去想檔名和自己清除,應該有可以利用的空間。

以下為節錄修改後的程式範例:

FileInfo tempFile = new FileInfo(Path.GetTempFileName());
FileStream fs = tempFile.OpenWrite();
fs.Write(DataString, 0, DataString.Length);
fs.Close();
// tempFile.FullName 檔案位址


來源網址:
[bug逃走中][.NET] 莫名奇妙的 "在根層次的資料無效,行1,位置1"
http://www.dotblogs.com.tw/regionbbs/archive/2011/09/10/35944.aspx

by Keng-li.Lin

2011年11月14日 星期一

Stop WebClinet.DownloadFiles() in MultiThread

使用WebClinet在multithread環境下下載檔案時的停止方式

ThreadList為 List元件 Thread形態的全域變數

假定Thread宣告內容為
Thread t1 = new Thread(() =>;
{
 try
 {
  using (WebClient mywebclient = new WebClient())
  {
   string sUrlToReadFileFrom = ImageUrl;
   string sFilePathToWriteFileTo = targeturl;
   Stream streamRemote = null;
   Stream streamLocal = null;
   using (WebClient client = new WebClient())
   {
    try
    {
     streamRemote = client.OpenRead(sUrlToReadFileFrom);

     streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None);

     int iByteSize = 0;
     byte[] byteBuffer = new byte[4096];

     while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
     {
      streamLocal.Write(byteBuffer, 0, iByteSize);
     }
    }
    catch (System.Net.WebException){// 可加入console.write}
    catch (Exception){// 可加入console.write}

    if (streamLocal != null){
streamLocal.Close();}
    if (streamRemote != null){streamRemote.Close();}
   }
  }
 }
 catch (System.Net.WebException){// 可加入console.write}
 catch (Exception){// 可加入console.write}
 ThreadList.Add(t1); 

 t1.Start();
}

停止則是另開了一個BackgroundWorker來進行
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
 int icount = 0;
 for (int ia = 0; ia < ThreadList.Count; ia++)
 {
  if (ThreadList[ia].IsAlive)
  {
   ThreadList[ia].Interrupt();
   icount++;
  }
  else
  {
   ThreadList[ia] = null;
  }
 }
 Thread.Sleep(500);
 bool totalStop = false;
 while (!totalStop)
 {
  totalStop = true;
  icount = 0;
  Thread.Sleep(500);
  for (int ia = 0; ia < ThreadList.Count; ia++)
  {
   if (ThreadList[ia] != null)
   {
    if (ThreadList[ia].IsAlive)
    {
     totalStop = false;
     icount++;
    }
    else
    {
     ThreadList[ia] = null;
    }
   }
  }
 }
}


by Keng-li.Lin

2011年11月10日 星期四

giveMeBreakfast 練習程式

最近完成了一個抓網頁的程式

開發環境:
Visual Studio 2010 .net 4

主要功能流程:
1.為抓取洪爺的網頁
2.截取分頁帖數量與連結後,置入Queue中
3.啟動Timer元件進行,對Queue中的連結進行內頁內碼擷取
4.轉換內碼,移除所有特殊html語法,並開啟Thread,使用WebClient進行圖片背景下載
5.將轉換後的內碼組合成html網頁
6.等待圖片下載完成後,自動展示網頁。

主要學習目的為:
web網頁擷取、html語法、多執行序分工

原始碼載點(google Docs空間):
giveMeBreakfast[source].7z
或
https://docs.google.com/leaf?id=0B4zlMKG7kf2mZTMyZDZlM2YtY2NiMi00MzRiLTg4YWQtZDYxMzAwOTFlMDcz

執行檔載點(google Docs空間):
giveMeBreakfast[bin].7z
或
https://docs.google.com/leaf?id=0B4zlMKG7kf2mNzM2NDVlMDMtMmNmMS00ODhiLWI4YTgtNmUxNDRhOTEyODFm

p.s. 此程式行為符合網站操作流程,非惡意攻擊程式,請放心使用。
另外,程式展示的內容含有限制級內容,請慎重考慮後使用。
by Keng-li.Lin

2011年10月27日 星期四

C# 解決WebBrowser 網頁中文為亂碼的方式

WebBrowser WB = new WebBrowser();

using (StreamReader SR = new StreamReader(WB.DocumentStream, System.Text.Encoding.GetEncoding("BIG5"))) // 要先確認該網頁使用的編碼
{
using (StreamWriter SW = new StreamWriter(".\\WebDocument.txt"))
{
SW.Write(SR.ReadToEnd());
SW.Flush();
SW.Close();
}
SR.Close();
}
WB.Dispose();
WB = null;



by Keng-li.Lin