Pergunta

I have a weird problem..

I am working on creating a thumbnail of a webpage! I have never worked with threading and I have a issue

I am using the following code

protected void GetScreenshot_Click(object sender, EventArgs e)
{
  path = Server.MapPath(FolderID);
  Thread webBrowseThread = new Thread(new ThreadStart(PerformWebBrowseOp));
  webBrowseThread.SetApartmentState(ApartmentState.STA);
  webBrowseThread.Start();
}          

protected void PerformWebBrowseOp()
{
  if(searchedword!='')
  {
    Directory.CreateDirectory(path);
  }

  string path1 = Mypath + "/image.png";
  GeneateScreenshot gn = new GeneateScreenshot();
  Bitmap thumbnail = gn.GenerateScreenshot("http://www.google.com/search?hl=en&q=" + Searchedword, 1024, 768);
  FileStream imageStream = new FileStream(path1, FileMode.Create);
  thumbnail.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
  imageStream.Close();
  downloadbutton.Visible=true;
}

now my problem is i have a download button to be displayed when the thumbnail has been created.. but for some reason the donwload button is not getting generated or is not visible!

I tried placing in the getscreenshot_click but it gets created even before the the thread has started running and before the thumbnail has been created.

Can someone help me fix this issue?

Foi útil?

Solução 2

The simplest way to solve this problem was to use webBrowseThread.join() to wait till the process is done and then say downloadbutton.visible= true !

Outras dicas

The short answer is that you don't want to do this for a webpage. The webpage is designed so that when the click handler finishes it moves on with the page lifecycle, and after ASP takes care of a bunch of it's own stuff it will send the response to the client. That response is being sent (or at least finalized so it can't be edited) before your background thread is finishing.

In a web environment if you're spinning off another thread you need to assume that you won't be able to edit anything for that same request in the other thread. If you do it soon enough, or if you have a really slow server, then it might work, but usually it won't, and you can't ever assume that it will.

My guess is you simply don't want to create another thread, and you want the user to wait until the image is saved before returning control back to the user.

If it's really important for you to receive the request, do some stuff, send a response to the user, and then update the page again after something happens in your background thread it is possible, but it's a LOT of work. (And also really sucks the resources out of your server.) Here is an example on MSDN that does it, but my guess is that in this case it's overkill and you're better off not bothering.

Edit: Based on your comments the code needs to be in another thread as it's currently not in an STA thread. Therefore the solution is to Join that thread in your click event handler so that you wait for it to finish before sending the response.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top