How to shutdown Visual Studio process started by EnvDTE - visual-studio

I have written a console app that uses EnvDTE to process 10 or so solutions and refactor them programmatically - changing references and project structure
var envDteType = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
var envDte = Activator.CreateInstance(envDteType, true);
var dte2 = (DTE2)envDte;
var solution = (Solution4)dte2.Solution;
solution.Open(filename);
// execute various tasks
solution.Close();
// how to dispose of dte2?
The trick is, when my app finishes there are Visual Studio processes still running presumably opened by EnvDTE. I shutdown my only Visual Studio instance visibly running and they persist.
Is there a way to shutdown those processes that the EnvDTE object spawned?
The following seems to have no effect
dte2.Application.ActiveWindow.Close();

Hans' is correct - dte2.Quit(); does the job

Related

Visual Studio ASP.NET crashes after Stop Debug in Chrome

I'm working on a website which can stream audio files to the browser. It's a asp.net application which im developing in Visual Studio 2013. To stream the audio to the client im using partial content response using the range request headers. The code looks like this:
[HttpGet, ActionName("GetFile")]
public HttpResponseMessage GetFile(string fileName)
{
var rootPath = AppDomain.CurrentDomain.BaseDirectory;
var fullPath = Path.Combine(rootPath, "SoundFiles", fileName);
byte[] file = File.ReadAllBytes(fullPath);
var mediaType = MediaTypeHeaderValue.Parse("audio/mpeg");
var memStream = new MemoryStream(file);
if (Request.Headers.Range != null)
{
try
{
var partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
partialResponse.Content = new ByteRangeStreamContent(memStream, Request.Headers.Range, mediaType);
return partialResponse;
}
catch (InvalidByteRangeException invalidByteRangeException)
{
return Request.CreateErrorResponse(invalidByteRangeException);
}
}
else
{
var fullResponse = Request.CreateResponse(HttpStatusCode.OK);
fullResponse.Content = new StreamContent(memStream);
fullResponse.Content.Headers.ContentType = mediaType;
return fullResponse;
}
}
Every time a song is played this method is called to get the file from the server and to stream it partially to the client. The whole application is built with signalr.
This worked perfectly for me there is nothing wrong with that code. Until i bought a new pc. I installed my Visual Studio 2013 normally on it including IIS Express 8.0 etc. But something strange happened to this.
If I start debugging with Google Chrome, and then if I do normal things to the website but not playing a song (not calling the method), and then stop debugging it again I have no problems. But if I start it in Chrome and play the song (call the method) and then try to stop debugging, visual studio freezes but there is no information that it is hanging. Its just hanging and I cannot close it. I cant even close it with the task manager. No error is showed. If I try to shut down my pc, then the pc won't shut down until I press the power button a long time. Now the strange thing: If I try the exact same thing but in Internet Explorer, Visual Studio works normally and is not hanging at all. This only happens if I use Chrome for debug and only on the same host as Visual studio is running. If I connect a Chrome on a other Device in my network, it does not have any effect.
I figured out, that I can run the application without debugging. If I do that it works good, but if I play a song and then try to close Visual Studio, it shortly hangs for about 30 seconds and then quits.
I am unable to find out what the problem is.
What I already tried to do:
Reinstall Visual Studio 2013
Try with Visual Studio 2015, same effect
Reinstall Windows 8.1
Reset Import/Export Settings in Visual Studio
Run Visual Studio as Administrator
Move SoundFiles to an other directory
Try with Google Chrome Canary, same effect
And the really strange thing is, that it only happens on Chrome on the same machine, and only on my new pc. The same scenario works perfectly on my surface or on the old laptop.
What I could imagin, for the chrome reason is, that chrome uses range requests and ie does not. It takes the full Response of audio as 200 and chrome takes the range as partial content. This could be the problem, but I'm not sure and I don't know how to solve or workaround this problem.
Have you guys an idea, what could causing this problem?
Ok, I didn't manage to find the problem, but I found the solution. Because it was a new PC, there was little bit of bloatware on it. I've uninstalled the things I don't needed, and then it magically worked. No crashes anymore. I have a ASUS ROG G20AJ. I don't know exactly which of the bloatware was causing the problem, but it was some of them. So if you have a similar problem on a ASUS Machine, try uninstalling some of the not used software.
It wasn't Visual Studio crashing, it was the IIS Service. I really think that this is a really strange behaviour, because the crashes did only happen, if I executed the code snippet above... Anyways, it works now!

Why can't I use STAThread attribute in C++/CLI?

I wan to use the STAThread attribute on my the main thread of my program. However, Visual Studio says it cannot find it. I have tried references necessary assemblies and using proper namespace, but it just can't find it.
Edit:
I have been able to get to work successfully after manually creating a thread with the
ApartmentState to STA. I think this is the equivalent to setting the thread, be it the main thread, but not exactly because i'm creating another thread. Anyone have another way to do this.
Here is the code:
void threadStart ()
{
Application::Run (gcnew GraphicsForm());
}
[System::STAThread] // This will not work!
int main(array<System::String ^> ^args)
{
Thread ^t = gcnew Thread(gcnew ThreadStart (threadStart));
t->ApartmentState = ApartmentState::STA;
t->Start();
return 0;
}
When I create a new C++/CLI project in Visual Studio 2012 with only a single main() function and then add [System::STAThread] in front of main(), it compiles and runs without a problem. To me, this means that it is most likely a settings difference between projects.
My recommendation is to do the same thing. Create a new C++/CLI project, add [System::STAThread] and see if it has any issues. If not, then you're at the point of checking the differences between the two projects to determine why one works properly and the other is giving you an error.

Is there an automatic check for disposing of SPWeb and SPSite in Visual Studio?

I'm reviewing some code and I thought it would be useful for Visual Studio to throw a warning anytime an SPWeb or SPSite object is initiated without using "using(SPSite x = ...){}".
Is there a way to configure this in Visual Studio?
Sometimes you need to dispose (e.g. var site = new SPSite()), sometimes you should not dispose (e.g. var web = site.RootWeb). So you can't do warnings based on absolutes.
Your best bet is to use SPDisposeCheck.
Using is a keyword that automatically dispose the object if it is no longer used.
So for spweb and spsite object we put in using keyword that's why they are automatically disposer when not used further.

How Can I Add an "ATL Simple Object" to Old ATL DLL Project Upgraded to VS 2010?

We have a DLL project which has existed for a long time (maybe as far back as Visual Studio 6) which has been updated for each new version of VS. The project contains a number COM classes implemented using ATL.
After upgrade to VS 2010, the project still builds fine. However, if I try to right-click the project and choose Add -> Class... -> ATL Simple Object, I get an error box that says this:
ATL classes can only be added to MFC EXE and MFC Regular DLL projects or projects with full ATL support.
This worked in VS 2008.
When I look at the project properties, Use of MFC was set to Use Standard Windows Libraries and Use of ATL was set to Not Using ATL. I changed these to Use MFC in a Shared DLL and Dynamic Link to ATL respectively, but still get the same error.
I know how to add new ATL objects without using the wizard, and I could try to recreate the project from scratch using VS 2010 to make it happy. But does anyone know of any easy way to get VS to allow me to use the ATL Simple Object wizard with a project that it doesn't recognize as a project "with full ATL support"?
Check this thread out.
It seems that adding this fragment info your ATL C++ code make it work. You don't need to actually build the project, just remove this stuff away after you are done with the wizard (provided that solution works for you).
// Added fake code begins here
class CAppModule :
public CComModule
{
};
// Added fake code ends here, below is regular ATL project stuff
CAppModule _Module;
This is where it all comes from, in $(VisualStudio)\VC\VCWizards\1033\common.js:
/******************************************************************************
Description: Returns a boolean indicating whether project is ATL-based.
oProj: Project object
******************************************************************************/
function IsATLProject(oProj)
{
try
{
var oCM = oProj.CodeModel;
oCM.Synchronize();
// look for global variable derived from CAtlModuleT
var oVariables = oCM.Variables;
for (var nCntr = 1; nCntr <= oVariables.Count; nCntr++)
{
var oVariable = oVariables(nCntr);
var strTypeString = oVariable.TypeString;
if (strTypeString == "ATL::CComModule" || strTypeString == "ATL::CAutoThreadModule")
{
return true;
}
Same problem here, but the project source already had CComModule _Module;
Fixed it, based on the IsATLProject script shown above, by changing it to
**ATL::**CComModule _Module;

Loading Visual Studio solution into programmatic instance

I have an F# program that creates an instance of Visual Studio:
let vsTy = Type.GetTypeFromProgID("VisualStudio.DTE.10.0") in
let dte2 = Activator.CreateInstance(vsTy,true) :?> EnvDTE80.DTE2 in
...
That works well; I can probe all kinds of info about the running instance
of Visual Studio.
But I can't determine how to load a solution into the instance of Visual Studio. I've tried dte2.ItemOperations.OpenFile(). I've also tried dte2.ExecuteCommand("File.OpenProject"), which works, but requires the user to select from a file dialog -- I want something that works from code alone.
Here's how to do it:
let soln2 = dte2.Solution :?> EnvDTE80.Solution2 in
let _ = soln2.Open(solutionFile) in
...
Bizarre, because dte2.Solution isn't a solution at all.

Resources