国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C# 積木模塊 ABC(一)(轉自新一代技術網)

2024-07-21 02:22:09
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 第一個c#程序:經典例程hello world

    “hello world”可以說是學習每一種編程語言的第一個例程了。我們可以在notepad、wordpad等任何編輯器中輸入以下的c#代碼,并保存為helloworld.cs,最后在命令行中執行csc helloworld.cs來運行文件:


    // using system
    using system;
    class hello
    {
    static void main() { // display output on console
    console.writeline("hello,c# world!");
    }
    }


    用openfiledialog類瀏覽或打開文件

    同vc++中cfiledialog的 open 方法相同,c#中的openfiledialog類可用于打開一個文件。這個類是從filedialog派生出來的。用這個類中的 openfile方法打開一個文件,然后就可以通過流(steam)來讀取這個文件。

    請看下面的例程代碼,它使用 openfiledialog類瀏覽一個文件:

    openfiledialog fdlg = new openfiledialog();
    fdlg.title = "c# corner open file dialog" ;
    fdlg.initialdirectory = @"c:/" ;
    fdlg.filter = "all files (*.*)|*.*|all files (*.*)|*.*" ;
    fdlg.filterindex = 2 ;
    fdlg.restoredirectory = true ;
    if(fdlg.showdialog() == dialogresult.ok)
    {
    textbox1.text = fdlg.filename ;
    }

    title一行設置打開對話框的標題,filter一行為打開的文件類型設置一個過濾器,filename一行包含了所選中的文件名。


    下圖就是運行的結果:




    從c#中調用com組件

    .net框架是com的一個自然發展,兩者共享許多核心要素,這包括組件的再利用以及語言的中立性。為了向后兼容,com interop可以使用現存的com組件而不要求對原始組件進行修改。當一個 .net 框架開發人員想將com代碼合并到一個管理應用程序中時,就可以用com interop功能引入相關的com類型。引入之后,這個com類型就可以使用了。這屬于前期連接。但是有時候你需要對象的后期連接,這在.net中也能實現,使用名稱空間映射就可以通過后期連接來調用com對象。


    這里介紹一個應用程序例程,它將調用excel,并且通過使用后期連接使它可視。


    后期連接將使用reflectionb的type類,這個type類有許多方法可以取得com對象,就象我們已經使用過的 gettypefromprogid("application"),這個方法從系統注冊表中得到com id,然后使用static類的成員 activator.createinstance()創建這個com對象的一個新例示。


    要想調用com對象的方法、函數和屬性,就必須使用包含正確設置的type對象的invokemethod()方法。這個方法接受一些參數變量,其中最重要的一個是方法類型的ex屬性(get或set)。在例子中我們為excel.visible使用了set屬性,從而使excel應用程序可視。


    我們將嘗試在.net環境中調用excel應用程序。這是一個后期連接應用程序,因為如果是前期連接的話你就需要使用com對象的rcw(runtime callable wraper:運行時間的可調用包)來完成下面的命令行程序tblimp所完成的任務:


    ex. c:/> tblimp /out:


    下載comindotnet.zip,這是一個控制臺應用程序。下面是調用excel的代碼:


    //variable
    type excel;
    object[] parameter= new object[1];
    object excelobject;
    try
    {
    //get the excel object
    excel = type.gettypefromprogid("excel.application");
    //create instance of excel
    excelobject = activator.createinstance(excel);
    //set the parameter whic u want to set
    parameter[0] = true;
    //set the visible property
    excel.invokemember("visible", bindingflags.setproperty, null, excelobject, parameter);
    }
    catch(exception e)
    {
    console.writeline("error stack {0} ", e.message) ;
    }
    finally
    {
    //when this object is destroyed the excel application will be closed
    //so sleep for sometime and see the excel application
    thread.sleep(5000);
    //relaese the object
    //gc.runfinalizers()
    }


    創建多線程應用程序


    在.net和c#中編寫一個多線程應用程序將非常得容易。即使對于那些從沒有用c#編寫過多線程應用程序的初學者,只需遵循以下這些簡單的步驟就可以實現目的。


    定義名稱空間

    在.net中,多線程功能是在system.threading名稱空間中定義的。因此,在使用任何線程類之前,必須定義 system.threading名稱空間。定義方法如下:


    using system.threading;


    啟動線程

    system.threading名稱空間中的thread類代表一個線程對象,用這個類對象可以創建新的線程,刪除、暫停和恢復線程。 下面的代碼使用thread類創建一個新的線程,然后啟動這個線程:


    thread = new thread(new threadstart( writedata ));

    thread.start();


    其中writedata是這個線程要執行的一個函數,代碼如下:


    protected void writedata()

    {

    string str ;

    for ( int i = 0; i<=10000; i++ )

    {

    str = "secondary thread" + i.tostring();

    console.writeline(listview1.listitems.count, str, 0, new string[]{""} );

    update();

    }

    }

    殺死線程


    thread類的abort方法用于永久地殺死一個線程。但是請注意,在調用abort方法前一定要判斷線程是否還激活,也就是判斷thread.isalive的值:


    if ( thread.isalive )

    {

    thread.abort();

    }


    暫停線程


    thread.sleep方法用于將一個線程暫停一段時間,代碼如下:


    thread.sleep();


    設置線程的優先權


    我們可以使用thread類的threadpriority屬性設置線程的優先權。線程優先權的取值范圍是normal、abovenormal、belownormal、highest或者lowest。請看下面的設置代碼:


    thread.priority = threadpriority.highest;


    延遲線程


    thread類的suspend方法可以延遲一個線程。線程被延遲到調用resume方法為止。


    if (thread.threadstate = threadstate.running )

    {

    thread.suspend();

    }


    恢復被延遲的線程


    調用resume方法可以恢復一個被延遲的線程。如果線程沒有被延遲,resume方法就是無效的。


    if (thread.threadstate = threadstate.suspended )

    {

    thread.resume();

    }
    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 铜陵市| 青神县| 萨迦县| 兴隆县| 宣恩县| 齐齐哈尔市| 武隆县| 海宁市| 宁安市| 巩义市| 武鸣县| 甘泉县| 迭部县| 探索| 铜山县| 乐东| 墨玉县| 巴青县| 建阳市| 民丰县| 洞头县| 清丰县| 乐都县| 屏南县| 岐山县| 古浪县| 安陆市| 遂宁市| 桃园市| 江城| 东乡族自治县| 新干县| 吴忠市| 天全县| 岳阳市| 枞阳县| 敦化市| 江油市| 汉源县| 禄丰县| 北京市|