C#的一些必备技术

  一些C#中比较重要的技术的基本使用方法和条件...

  HashTable、Timer、Thread、WebService...

HashTable:

  hashtable,可用于存储键值对,基本使用如下:

 1             //创建新的Hashtable对象
 2             Hashtable ht = new Hashtable();
 3 
 4             //添加键值对
 5             ht.Add('a', "add");
 6             ht.Add('b', "back");
 7             ht.Add('c', "call");
 8 
 9             //显示key值为‘c’对应的Value
10             MessageBox.Show(ht['c'].ToString());
11 
12             //循环访问ht中的值
13             foreach (DictionaryEntry de in ht)
14             {
15                 MessageBox.Show(de.Key.ToString() + " | " + de.Value.ToString());
16             }

 

Timer:

  定时器,可以用于重复一定时间执行某一段代码,或是定时执行某一段代码:

1、WinForm中可以直接添加Timer控件,然后在它的事件中执行想要执行的代码:

属性配置:

事件函数:

1         private void timer1_Tick(object sender, EventArgs e)
2         {
3             //重复或定时执行的代码
4         }

 

2、直接使用Timer类:
  命名空间:System.Threading(除了这个命名空间,还有其他命名空间也含有Timer类的定义);

 1         //设置调用时间
 2         public void StartTimer(int dueTime)
 3         {
 4             //TimerProc为Timer的回调函数,当时间到时,会自动调用该函数
 5             Timer t = new Timer(new TimerCallback(TimerProc)); 
 6             //dueTime为间隔多少毫秒后调用TimerProc函数
 7             t.Change(dueTime, 0);
 8         }
 9 
10         private void TimerProc(object state)
11         {
12             // The state object is the Timer object.
13             Timer t = (Timer)state;
14             t.Dispose(); //释放资源
15             Console.WriteLine("The timer callback executes.");
16         }


此外,还可以设置Timer的许多属性,可以直接使用Timer对象设置。

   

Thread:

  线程,应用命名空间System.Threading

基本使用方法:

1           //定义一个线程对象td,ThreadCallBack为其调用函数
2        Thread td = new Thread(ThreadCallBack);
3           //启动线程
4        td.Start();

ThreadCallBack函数定义:

1         //无参数,无返回值
2         private void ThreadCallBack()
3         {
4             //执行代码
5             MessageBox.Show("Threading...");
6         }

 给线程传递数据:

  使用带ParameterizedThreadStart委托参数的构造函数;

  创建一个自定义类,把线程的方法定义为实例方法,这样就可以初始化实例的数据,之后启动线程。

 后台线程:

  使用Thread类时,设置IsBackground属性即可。

线程的优先级:

  使用Thread类时,设置Priority属性。

WebService:

  在C#的WinForm程序中添加ASP.NET Web服务。

1、创建一个一个ASP.NET Web服务

添加一个简单函数:

1         [WebMethod]
2         public string SetStringAndGetString(string sStr)
3         {
4             return sStr;
5         }

  可以运行测试一下,看是否正确。

2、在WinForm程序中添加服务引用,点击发现,选择服务,并将命名空间改为MyServiceReference(根据自己需要修改名称),确定。

3、调用:

  直接调用:

//MyServiceReference就是引用的命名空间,定义一个WebServiceSoapClient对象,然后用该对象调用自己定义的Web服务函数。
1
MyServiceReference.WebServiceSoapClient client = new MyServiceReference.WebServiceSoapClient(); 2 tbGetString.Text = client.SetStringAndGetString(tbSetString.Text.ToString());

  异步调用:

    选择服务引用MyServiceReference,打开关联菜单,选择Configure Service Reference,打开对话框,在Service Reference Setting对话框中选中Generate asynchronous operations复选框(选择异步调用)。

1         MyServiceReference.WebServiceSoapClient client = new MyServiceReference.WebServiceSoapClient();
2         client.SetStringAndGetStringCompleted +=new EventHandler<MyServiceReference.SetStringAndGetStringCompletedEventArgs>(client_SetStringAndGetStringCompleted);
3         client.SetStringAndGetStringAsync(tbSetString.Text);
1         //异步调用
2         private void client_SetStringAndGetStringCompleted(object sender, MyServiceReference.SetStringAndGetStringCompletedEventArgs e)
3         {
4             tbGetString.Text = e.Result;
5         }


将e.Result中的调用结果返回.

posted @ 2012-08-22 15:53  Longlycsu  阅读(842)  评论(0编辑  收藏  举报