asp.net利用多线程,生成大量图片

注:
如果图片文件名重复,则会报错:‘GDI+ 中发生一般性错误。ExternalException’

 

大体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Threading;
using System.Data.SqlClient;
using UBIQ.ETS.TIC.NS.Biz;
using System.IO;

namespace WebMultiThread
{
    public partial class ThreadGenPict : System.Web.UI.Page
    {
        private DataTable threadOneResult;
        private DataTable threadTwoResult;
        private DataTable threadThreeResult;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(outdir))
                Directory.CreateDirectory(outdir);

            if (threadOneResult == null)
            {
                threadOneResult = new DataTable();
                threadOneResult.Columns.Add(new DataColumn());
            }

            if (threadTwoResult == null)
            {
                threadTwoResult = new DataTable();
                threadTwoResult.Columns.Add(new DataColumn());
            }
            if (threadThreeResult == null)
            {
                threadThreeResult = new DataTable();
                threadThreeResult.Columns.Add(new DataColumn());
            }

        }

        public void AsyncCallsWithThreadPool()
        {
            int row1 = 10;
            int row2 = 5000;
            int row3 = 10;
            ThreadPool.QueueUserWorkItem(ThreadOne, row1);
            ThreadPool.QueueUserWorkItem(ThreadTwo, row2);
            ThreadPool.QueueUserWorkItem(ThreadThree, row3);

            //while (threadOneResult.Rows.Count == row1 ||
            //    threadTwoResult.Rows.Count == row2 ||
            //    threadThreeResult.Rows.Count == row3)
            //{
            //    Thread.Sleep(10000);
            //}
            //int sleep = int.Parse(TextBox1.Text);
            //Thread.Sleep(sleep*1000);


            //threadOneResult.Merge(threadTwoResult);
            //threadOneResult.Merge(threadThreeResult);
            //DetailsView1.DataSource = threadOneResult;
            //DetailsView1.DataBind();
        }
        string outdir = AppDomain.CurrentDomain.BaseDirectory + @"OutImage\";
        string backfile = AppDomain.CurrentDomain.BaseDirectory + "TicketBack.jpg";
        private void ThreadOne(object state)
        {
            int i = 0;
            int max = int.Parse(state.ToString());
            while (true)
            {
                try
                {
                    BizTool.SaveTicketImage(BizTool.GetTicketCode() + "Type1" + i.ToString(), outdir + BizTool.GetTicketCode() + "Type1" + i.ToString() + ".jpg", "Type1", "11元", backfile);
                }
                catch (IOException ex)
                {
                    BizTool.MsgToFile(ex.Message);
                }
                i++;
                if (i > max)
                    break;
                DataRow row = threadOneResult.NewRow();
                row[0] = (BizTool.GetTicketCode() + "Type1" + i.ToString());
                threadOneResult.Rows.Add(row);
            }
        }

        private void ThreadTwo(object state)
        {
            int i = 0;
            int max = int.Parse(state.ToString());
            while (true)
            {
                try
                {
                    BizTool.SaveTicketImage(BizTool.GetTicketCode() + "Type2" + i.ToString(), outdir + BizTool.GetTicketCode() + "Type2" + i.ToString() + ".jpg", "Type2", "12元", backfile);
                }
                catch (IOException ex)
                {
                    BizTool.MsgToFile(ex.Message);
                }
                i++;
                if (i > max)
                    break;
                DataRow row = threadTwoResult.NewRow();
                row[0] = (BizTool.GetTicketCode() + "Type2" + i.ToString());
                threadTwoResult.Rows.Add(row);
            }
        }

        private void ThreadThree(object state)
        {
            int i = 0;
            int max = int.Parse(state.ToString());
            while (true)
            {
                try
                {
                    BizTool.SaveTicketImage(BizTool.GetTicketCode() + "Type3" + i.ToString(), outdir + BizTool.GetTicketCode() + "Type3" + i.ToString() + ".jpg", "Type3", "13元", backfile);
                }
                catch (IOException ex)
                {
                    BizTool.MsgToFile(ex.Message);
                }
                i++;
                if (i > max)
                    break;
                DataRow row = threadThreeResult.NewRow();
                row[0] = (BizTool.GetTicketCode() + "Type3" + i.ToString());
                threadThreeResult.Rows.Add(row);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://www.baidu.com");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            Response.Redirect("Default.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            AsyncCallsWithThreadPool();

        }

    }
}