开发者

asp.net Multithreading with timer

i have a web application that count the words of a document. so it take quite long.i have divided that into pages and invoked method for each page in a different thread. i am using threadpool as

        ThreadPool.SetMaxThreads(25, 200);
        State state;
        Hashtable ht = new Hashtable();
        for (int i = 0; i < 100; i++)
        {
            state = new State();
            state.Input = "Pakistan";
            ThreadPool.QueueUserWorkItem(PerformUserWorkItem, state);
            ht.Add(i, state);
        }
        State tempState;
        for (int i = 0; i < 100; i++)
        {
            tempState = (State)ht[i];
            if (tempState.eventWaitHandle.WaitOne())
            {
                Session["percentage"] = i;
                lblPercentage.Text = (i * 1).ToString();
            }
        }
private static void PerformUserWorkItem(Object stateObject)
    {
        State state = stateObject as State;
        if (state != null)
        {
            // do something lengthy with state.inputString                

            state.result = state.Input.Length;
            state.eventWaitHandle.Set(); // signal we're done
        }
    }

In the code above i have queued threads for working and saved their initial state in the hashtable . I have timer that updates the Progress bar on the page that how many pages counting is done and how much left.

I need to redirect开发者_如何学JAVA my page to another page and i get the error that Response is not available in this context. i have tried to use the server.transfer but getting the error as

"Error Executing child request" then i have disabled the target page(to which i am redirecting)property enabeviewstate =false but it also didn't worked for me. after all this my visual studio get stucked and i get this error twice "localhost cannot connect to local web server" after the error 2 times my visual studio works fine.

Update

Here is my timer declaration's

     System.Timers.Timer t = new System.Timers.Timer(3000);
     t.Enabled = true;
     t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
  void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        lbl1.Text = DateTime.Now.ToString();           
    }


It is error becus u user EventWaitHandle. wait handle makes your main thread(client thread) still alive. and after u use Server.Transfer it will have ThreadAbourtException becus the main thread is working and collaborate with subthread.

i recommend .Net 4.0

public void perform()
{
 Object[] works = ...;
 System.Threading.Tasks.Parallel.ForEach(works, each => { PerformUserWorkItem(each); });
 Server.Transfer("progress.aspx");
}

private static void PerformUserWorkItem(Object stateObject)
    {
        ...
    }

And in progress.aspx page, u can use javascript function called 'setTimer()' to call in to get progress.

Besides, System.Timers.Timer is a timer object which run on server which has no control to remote environment. If u want asp page to call back, u gonna need timer control in asp page to trigger client call

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜