Threading calling Powerpoint

G

Guest

I am calling a method like s

System.Threading.Thread myThread =
new System.Threading.Thread(new ThreadStart(CreateTextFile))
myThread.Priority = ThreadPriority.Normal
myThread.Start()

the method creates a simple textfile and then calles another method that opens powerpoin

public void CreateTextFile()//int orderI

///create text fil
string sPath = Server.MapPath(ConfigurationSettings.AppSettings["FileLocation"])
string sFileName = orderId + ".txt"
FileStream fs = File.Create(sPath + sFileName)
StreamWriter sw = new StreamWriter(fs)

for (int i=0; i<MyDataGrid.Items.Count; i++

DataGridItem item = MyDataGrid.Items
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)item.FindControl("linkImage")
sw.WriteLine(img.ImageUrl)
}
sw.Close()

///create powerpoint method calle
PPT.GeneratePPT getPPT = new PPT.GeneratePPT()
strPPTfile = PPT.GeneratePPT.CreatePowerPoint(orderId)


the textfile is created fine and powerpoint service gets initiated (i can see it in the task manager) but it never creates the powerpoint. if i call the method directly without using a thread it works fine. i have to use a thread though because some of these powerpoints take minutes to process.

thanks for your help
chris
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

I believe the issue here is that you are not setting the threading
apartment of the thread. In order to do this, at the beginning of your
thread procedure, get a reference to the current thread (through the static
CurrentThread property on the Thread class). Once you have that, you can
set the ApartmentState property to ApartmentState.STA, and it should work.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris McIlvoy said:
I am calling a method like so

System.Threading.Thread myThread =
new System.Threading.Thread(new ThreadStart(CreateTextFile));
myThread.Priority = ThreadPriority.Normal;
myThread.Start();

the method creates a simple textfile and then calles another method that opens powerpoint

public void CreateTextFile()//int orderId
{
///create text file
string sPath = Server.MapPath(ConfigurationSettings.AppSettings["FileLocation"]);
string sFileName = orderId + ".txt";
FileStream fs = File.Create(sPath + sFileName);
StreamWriter sw = new StreamWriter(fs);

for (int i=0; i<MyDataGrid.Items.Count; i++)
{
DataGridItem item = MyDataGrid.Items;
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)item.FindControl("linkImage");
sw.WriteLine(img.ImageUrl);
}
sw.Close();

///create powerpoint method called
PPT.GeneratePPT getPPT = new PPT.GeneratePPT();
strPPTfile = PPT.GeneratePPT.CreatePowerPoint(orderId);
}

the textfile is created fine and powerpoint service gets initiated (i can

see it in the task manager) but it never creates the powerpoint. if i call
the method directly without using a thread it works fine. i have to use a
thread though because some of these powerpoints take minutes to process.
 
G

Guest

Thanks for your help. Actually it did not correct it though. I added the following line

Thread.CurrentThread.ApartmentState = ApartmentState.STA

System.Threading.Thread myThread = new System.Threading.Thread(new ThreadStart(CreateTextFile))
Thread.CurrentThread.ApartmentState = ApartmentState.STA
myThread.Start()

and i also added it to the CreateTextFile procedure. Add I added aspcompat=true to the page calling the code. i am getting a access denied error in my event log. but i already went into dcomcnfg and gave permissions to aspnet to launch and access powerpoint. this code i had worked fine with the office 10 library but now i am using the office 11.0 and it does not work. i am not sure where to go from here. thanks agai
 
G

Guest

I resolved the DCOM error, it was just the fact I had not opened powerpoint before under the aspnet account. i just changed the user account in the machine.confi

in the task manager the powerpoint.exe mem usage grows but never completes the powerpoint.
 
W

Willy Denoyette [MVP]

What do you mean with "never completes", please correct me when I'm wrong,
but do you expect your slide(s) be become visible?
I can tell you they won't, as PowerPoint isn't able to access the visible
desktop when started from IIS/asp.net.
What's more, Office xx products are not supposed to be used in such scenario
and it is not supported by MSFT.

Willy.
 
G

Guest

No i do not expect to see the powerpoint, i do however expect it to save the powerpoint file. if i call the method directly, not in the thread, it works, however if i call in the thread the process starts but never saves a ppt file. if office products are not to be used in such a scenario then why am i using office tools for visual studio? should i not be able to start a thread that creates a powerpoint file from a aspx page? i am not sure why microsoft would not support this ? what is confusing me is that it works with the office 10 libraries. we now have office 2003 on the office so i am stuck using office 11 dlls since they are not backward compatible, correct?
 
Top