The Perfect Day

分享技术,编写未来

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  73 随笔 :: 0 文章 :: 23 评论 :: 0 引用

2010年3月24日 #

深入Unity 1.x依赖注入容器

Unity Application Block 1.0系列

 

posted @ 2010-03-24 13:39 StephenJu 阅读(57) 评论(0) 编辑

2009年9月25日 #

this.textEdit1.Properties.EditFormat.FormatString = "yyyy-MM-dd";
this.textEdit1.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
this.textEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
this.textEdit1.Properties.Mask.EditMask = "\\d{2}\\d{2}?-([0]?[1-9]|10|11|12)-(0?[1-9]|[12]\\d|30|31) (([01]?[0-9]|2[0-3])(:[0-" +"5]\\d)?)?";
//"\\d{2}\\d{2}?-([0]?[1-9]|10|11|12)-(0?[1-9]|[12]\\d|30|31)";
this.textEdit1.Properties.Mask.ShowPlaceHolders = false;
posted @ 2009-09-25 15:09 StephenJu 阅读(167) 评论(1) 编辑

2009年9月11日 #

引自博客:Yes I Can 什么是.Net异步调用机制

 

代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.ComponentModel;//ISynchronizeInvoke
using System.Runtime.Remoting.Messaging;//AsyncResult
using System.Windows.Forms;
using System.Threading;

namespace WinApp
{
    
public class AsyncQueryHelper
    {
        FrmProgressBar frmProgressBar 
= null;

        
public AsyncQueryHelper()
        {
            frmProgressBar 
= new FrmProgressBar(10);
        }

        
public delegate DataSet ExecQueryHandler(DataSet dsRequest);
        
public ExecQueryHandler execQueryHandlerInstance = null;

        
private AsyncCallback asyncCallbackInstance = null;
        
public void ExecQuery(DataSet dsRequest)
        {
            frmProgressBar.StartThread();
            asyncCallbackInstance 
= new AsyncCallback(ExecAsyncCallBack);//<=>asyncCallbackInstance = ExecAsyncCallBack;

            
if (execQueryHandlerInstance != null)
            {
                
//execQueryHandlerInstance.BeginInvoke(dsRequest, asyncCallbackInstance, execQueryHandlerInstance);
                IAsyncResult result = execQueryHandlerInstance.BeginInvoke(dsRequest, ExecAsyncCallBack, execQueryHandlerInstance);//同上

                
//=======================WaitOne=======================//
                
//阻碍当前线程,直到异步调用结束
                result.AsyncWaitHandle.WaitOne();
                
//开始其他工作.
                
//DoAnotherWork();

                
//=======================WaitAll=======================//
                /*
                IAsyncResult result1 = execQueryHandlerInstance.BeginInvoke(dsRequest, ExecAsyncCallBack, execQueryHandlerInstance);
                IAsyncResult result2 = execQueryHandlerInstance.BeginInvoke(dsRequest, ExecAsyncCallBack, execQueryHandlerInstance);
                //把所有异步的句柄保存到WaitHandle 对象中
                WaitHandle[] waitHandles = { result1.AsyncWaitHandle, result2.AsyncWaitHandle };
                //阻碍当前线程,直到所有异步调用结束
                WaitHandle.WaitAll(waitHandles);
                //开始其他工作
                //DoAnotherWork();
                
*/

                
//====WaitAny:可以使用 WaitAny 来指定某个/某几个委托先等待=====//
                /*
                 WaitHandle[] oneWaitHandle = { result1.AsyncWaitHandle };
                 WaitHandle.WaitAny(oneWaitHandle);
                 //开始其他工作
                 //DoAnotherWork();
                
*/
            }
        }

        
public delegate void BindDataToGrid(DataSet dsReponse);
        
public BindDataToGrid bindDataToGridInstance = null;

        
public DataGridView dataGridView = null;
        
public void ExecAsyncCallBack(IAsyncResult asyncResult)
        {
            endHandlerInstance 
= new EndHandler(frmProgressBar.EndThread);
            frmProgressBar.BeginInvoke(endHandlerInstance, 
null);//执行控件的BeginInvoke

            
////======================1=========================////
            //此处的asyncResult.AsyncState对象就是BeginInvoke里的最后一个参数
            /*
            ExecQueryHandler execQueryHandler = asyncResult.AsyncState as ExecQueryHandler;
            DataSet dsReponse = null;
            if (asyncResult.IsCompleted)
            {
                if (execQueryHandler != null)
                {
                    dsReponse = new DataSet();
                    dsReponse = execQueryHandler.EndInvoke(asyncResult);
                    ISynchronizeInvoke async = this.dataGridView;
                    if (async.InvokeRequired)
                    {
                        async.Invoke(bindDataToGridInstance, new object[] { dsReponse });
                    }
                }
            }
            
*/

            
////======================2=========================////
            AsyncResult originAsyncResult = asyncResult as AsyncResult;
            
if (originAsyncResult != null)
            {
                ExecQueryHandler execQuery 
= (ExecQueryHandler)originAsyncResult.AsyncDelegate;
                DataSet dsReponse 
= execQuery.EndInvoke(asyncResult);

                ISynchronizeInvoke async 
= this.dataGridView;
                
//InvokeRequired == false表示来自主线程;为true表示来自异步线程
                if (dataGridView != null && bindDataToGridInstance != null)
                {
                    
if (async.InvokeRequired)
                    {
                        async.Invoke(bindDataToGridInstance, 
new object[] { dsReponse });
                    }
                }
                asyncResult.AsyncWaitHandle.Close();
//显示的释放资源
            }
        }

        
public delegate void SendRequestHandler();
        
public SendRequestHandler sendRequestHandlerInstance = null;

        
public delegate void EndHandler();
        
public EndHandler endHandlerInstance = null;


        
static void DoAnotherJob()
        {
            Thread.Sleep(
1000);//需要1秒才能完成这个工作
            MessageBox.Show("[{0}]:Do Another Job", DateTime.Now.ToString());
        }

    }
}

 

 

 

代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WinApp
{
    
public class AsyncReadWrite
    {
        
//同步读文件
        public static string SyncRead()
        {
            
string path = @"d:\test.txt";
            FileStream fs 
= new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 20480false);
            StringBuilder sbReadResult 
= new StringBuilder();
            
using (fs)
            {
                
byte[] data = new byte[1024];
                
int nbyteReads;
                
do
                {
                    nbyteReads 
= fs.Read(data, 0, data.Length);
                    sbReadResult.Append(Encoding.Default.GetString(data, 
0, nbyteReads));
                } 
while (nbyteReads > 0);
                
//Console.WriteLine(sbReadResult.ToString());
            }
            
//Console.ReadLine();
            return sbReadResult.ToString().Trim();
        }

        
//异步读文件
        public static string AsyncRead()
        {
            
string path = @"d:\Query.txt";
            
byte[] data = new byte[1024];
            FileStream fs 
= new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 20480true);
            StringBuilder sbReadResult 
= new StringBuilder();
            AsyncCallback asyncCallback 
= null;
            
//匿名方法
            asyncCallback = delegate(IAsyncResult asyncResult)
            {
                
int nRead = fs.EndRead(asyncResult);
                sbReadResult.Append(Encoding.Default.GetString(data, 
0, nRead));

                
if (nRead > 0)
                {
                    fs.BeginRead(data, 
01024, asyncCallback, null);
                }
                
else
                {
                    fs.Close();
//显示释放托管堆资源
                }
            };
            IAsyncResult asyncReadResult 
= fs.BeginRead(data, 01024, asyncCallback, null);
            
return sbReadResult.ToString().Trim();
        }

        
//异步写文件
        public static void AsyncWrite(string content)
        {
            
string path = @"d:\write.txt";
            FileStream fs 
= new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 20480true);
            
byte[] data = Encoding.Default.GetBytes(content);
            AsyncCallback asyncCallback 
= null;
            asyncCallback 
= delegate(IAsyncResult async)
            {
                fs.EndWrite(async);
                fs.Close();
//显示释放托管堆资源
            };
            IAsyncResult asyncWriteResult 
= fs.BeginWrite(data, 0, data.Length, asyncCallback, null);
            Console.ReadLine();
        }
    }
}

 

 

posted @ 2009-09-11 12:21 StephenJu 阅读(141) 评论(0) 编辑

2009年9月10日 #

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinAppAsync
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void btnLoad_Click(object sender, EventArgs e)
        {
            
this.Text = "正在加载";
            
//异步执行
            LoadDataHandlerInstance = new LoadDataHandler(CreateData);
            AsyncCallback callBackMethod 
= new AsyncCallback(CallBackLoad);
            LoadDataHandlerInstance.BeginInvoke(callBackMethod, LoadDataHandlerInstance);
        }

        
public delegate DataTable LoadDataHandler();
        
public LoadDataHandler LoadDataHandlerInstance = null;
        
private DataTable CreateData()
        {
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
"Id"typeof(string));
            dt.Columns.Add(
"Name"typeof(string));
            dt.Columns.Add(
"Address"typeof(string));

            
for (int i = 0; i < 800000; i++)
            {
                DataRow row 
= dt.NewRow();
                row[
"Id"= i.ToString();
                row[
"Name"= "Name_" + i.ToString();
                row[
"Address"= "Address_" + i.ToString();
                dt.Rows.Add(row);
            }
            
return dt;
        }
        
public void CallBackLoad(IAsyncResult result)
        {
            LoadDataHandler loadInstance 
= (LoadDataHandler)result.AsyncState;
            DataTable dt 
= loadInstance.EndInvoke(result);

            bindGridHandlerInstance 
= new BindGridHandler(BindGrid);
            
this.dgv.BeginInvoke(bindGridHandlerInstance, new object[] { dt });//执行控件的Invoke或BeginInvoke以修改主线程上的属性
        }

        
public delegate void BindGridHandler(DataTable dt);
        
public BindGridHandler bindGridHandlerInstance = null;
        
private void BindGrid(DataTable dt)
        {
            
this.dgv.DataSource = dt;
        }
    }
}
posted @ 2009-09-10 19:46 StephenJu 阅读(36) 评论(0) 编辑

2009年1月4日 #

private int iSendCount = 0;
        
private void Test(DSSendCollection dsRequest)
        {
            
//取基准数据做比较
            string strS1Name = Convert.ToString(dsSend.Sub1.Rows[0]["s1_name"]).Trim();
            
string strS1UUID = Convert.ToString(dsSend.Sub1.Rows[0]["uuid"]).Trim();
            

            DSSendCollection dsSendEdi 
= new DSSendCollection();
            
//对主表排序
            DSSendCollection.Sub1Row[] drs = dsRequest.Sub1.Select("""s1_Name,uuid"as DSSendCollection.Sub1Row[];
            
for (int i = 0; i < drs.Length; i++)
            {
                
if (drs[i].s1_Name.Trim() != strS1Name || drs[i].uuid.Trim() != strS1UUID)
                {
                    DataRow[] filterRows 
= dsRequest.Sub1.Select("s1_Name='" + strS1Name + "' and uuid='" + strS1UUID + "'");
                    
for (int j = 0; j < filterRows.Length; j++)
                    {
                        DataRow drShipment 
= filterRows[j];//主表的行
                        ImportRow(drShipment, dsSendEdi);
                    }
                    
//发送Edi
                    iSendCount++;
                    MessageBox.Show(
"发送EDI    "+""+iSendCount+"");
                    strS1Name 
= drs[i].s1_Name.Trim();
                    strS1UUID 
= drs[i].uuid.Trim();
                    dsSendEdi.Clear();
                    dsSendEdi.AcceptChanges();
                }
               
                
if (i == drs.Length - 1)
                {
                    
if (iSendCount == 0)
                    {
                        
//发送Edi:直接传dsRequest
                        
                    }
                    
else
                    {
                        DataRow[] filterRows 
= dsRequest.Sub1.Select("s1_Name='" + strS1Name + "' and uuid='" + strS1UUID + "'");
                        
for (int j = 0; j < filterRows.Length; j++)
                        {
                            DataRow drShipment 
= filterRows[j];
                            ImportRow(drShipment, dsSendEdi);
                        }
                        
//发送Edi
                        iSendCount++;
                        MessageBox.Show(
"发送EDI    " + "" + iSendCount + "");
                        dsSendEdi.Clear();
                        dsSendEdi.AcceptChanges();
                    }
                }
            }
posted @ 2009-01-04 16:37 StephenJu 阅读(56) 评论(0) 编辑

2008年12月26日 #

摘要: [代码]阅读全文
posted @ 2008-12-26 15:38 StephenJu 阅读(57) 评论(1) 编辑

2008年12月11日 #

摘要: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Wind...阅读全文
posted @ 2008-12-11 00:26 StephenJu 阅读(109) 评论(0) 编辑

2008年6月17日 #

摘要: usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.IO;namespaceSys{[Serializable]//指示一个类可以序列化publicclassElement{//复制对象publicob...阅读全文
posted @ 2008-06-17 13:04 StephenJu 阅读(124) 评论(0) 编辑

2008年6月10日 #

摘要: usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceSys{publicpar...阅读全文
posted @ 2008-06-10 11:45 StephenJu 阅读(1289) 评论(1) 编辑

2008年5月28日 #

摘要: ValidateDataintheWindowsFormsDataGridViewControlSamples: private void dataGridView1_CellValidating(object sender,DataGridViewCellValidatingEventArgs e){// Validate the CompanyName entry by disallowing...阅读全文
posted @ 2008-05-28 21:29 StephenJu 阅读(686) 评论(0) 编辑