CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2010
    Posts
    8

    Problem with processes (opening and closing files)

    Used: .NET Compact Framework , Visual Studio 2008 Professional Edition, c#.

    It seems maybe a problem is with Refresh. It zeros a process and then a program breaks down on this line - intp=myProcess.MainWindowHandle.

    myStartInfo.FileName = processType;

    02 myStartInfo.Arguments = fileNameToOpen;

    03 myStartInfo.UseShellExecute = true;

    04 bool notResponding = true;

    05

    06 myProcess.EnableRaisingEvents = true;

    07

    08 myProcess = Process.Start(myStartInfo);

    09

    10 IntPtr intp = myProcess.MainWindowHandle;

    11 notResponding = myProcess.Responding;

    12 while ((int)intp == 0)

    13 {

    14 myProcess.Refresh();

    15 intp = myProcess.MainWindowHandle;

    16

    17 Thread.Sleep(2000);

    18 }

    19

    20 int myID = myProcess.Id;

    21

    22 // Close process by sending a close message to its main window.

    23 myProcess.CloseMainWindow();

    24 myProcess.Refresh();

    25

    26 // Free resources associated with process.

    27 //myProcess.Close();

    28 myProcess.Kill();

    29 }

    Purpose of this program is to open and close word, excel files.. And do it many times. But as you see, something is wrong with this while cycle and I don't understand what..

  2. #2
    Join Date
    Sep 2010
    Posts
    13

    Re: Problem with processes (opening and closing files)

    Hi,

    Just a couple of things - what does the variable notResponding do? In line 11 you call the method to set it, but then it is never called again?

    Have you tried outputting the window handle to see what it does at various states? If it is failing then you could put a try..catch block round the IntPtr intp = myProcess.MainWindowHandle; and exit when it fails. Alternatively, if you can check the value then you should be able to determine what the value is and set accordingly.

    Thanks



    Andrew

  3. #3
    Join Date
    Dec 2010
    Posts
    8

    Re: Problem with processes (opening and closing files)

    This while cycle works perfectly when I am opening calculator. I took an example from it. But I don't know how to change this cycle, if I could openening word file or Internet Explorer. . It brings mistakes then:

    > Pirmas.exe!Pirmas.Form1.Start_Process(string processType = "IExplore.exe") Line 179 C#
    Pirmas.exe!Pirmas.Form1.doOperation() Line 146 + 0xc bytes C#
    Pirmas.exe!Pirmas.Form1.menuItemStart_Click(object sender = {System.Windows.Forms.MenuItem}, System.EventArgs e = {System.EventArgs}) Line 100 + 0x6 bytes C#


    179 line from that code is: while ((int)intp == 0)

  4. #4
    Join Date
    Dec 2010
    Posts
    8

    Question Re: Problem with processes (opening and closing files)

    Mm nobody knows how to open and close a file, using a process in .NET Compact Framework? :/ (I mean how should I start process from applicaton in .NET..)

  5. #5
    Join Date
    Nov 2010
    Posts
    34

    Re: Problem with processes (opening and closing files)

    Where exactly is it bombing out?

    I'm not sure about the compact framework, but I've done similar with the 'Microsoft.Office.Interop.Word' dll... something like this let's you have more control over how it opens than just process.start() routines..

    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    documents1.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform)

  6. #6
    Join Date
    Dec 2010
    Posts
    8

    Re: Problem with processes (opening and closing files)

    This link seems simple, without processes.. Just after running a program opens a file, but then it stops and shows that something is wrong in that cycle from "while..

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Problem with processes (opening and closing files)

    Is your sample for the COMPACT FRAMEWORK? Do you have OFFICE installed?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Dec 2010
    Posts
    8

    Re: Problem with processes (opening and closing files)

    Examples are here, http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx That is what I use. But still I cannot see what I need.

  9. #9
    Join Date
    Dec 2010
    Posts
    8

    Unhappy Re: Problem with processes (opening and closing files)

    More detailed explanation would be:

    When a process ir running, it gets some task, like to open word file. But we don't know when this program will be opened, you have to catch this moment in order to know when you can end the process. That is why mainwindowhandle event is used. But it breaks down I don't know why. So I don't know how to do this in other way. .
    Last edited by Ronna12; December 21st, 2010 at 01:37 PM.

  10. #10
    Join Date
    Dec 2010
    Posts
    8

    Unhappy Re: Problem with processes (opening and closing files)

    Quote Originally Posted by dglienna View Post
    Is your sample for the COMPACT FRAMEWORK? Do you have OFFICE installed?
    Yes, it is for .NET compact framework. I mentioned that above. Also it has Windows Mobile 6 professional sdk, active Sync. So I have word, excel and power point there. This place from my code:
    while ((int)intp==0)
    {
    myProcess.Refresh();
    intp = myProcess.MainWindowHandle;
    Thread.Sleep(2000);
    }

    perfectly suits for MobileCalculator, but not for opening word or IExplorer. So I can't imagine how to change it for this purpose..Cause program breaks down in this place.

  11. #11
    Join Date
    Dec 2010
    Posts
    8

    Unhappy Re: Problem with processes (opening and closing files)

    So, any more thoughts, guys, what can be wrong, why this process breaks in this place when opening something?

  12. #12
    Join Date
    Nov 2010
    Posts
    34

    Re: Problem with processes (opening and closing files)

    You might just need to change your ==0 to ==IntPtr.Zero*

    Can you tie in Win32 stuff to the compact framework? (I know nothing about the CF, I'm sure it's nerfed!
    So something like the below to find a window...
    I actually think I've had an issue like this and I just plain used a goto above the block, and then a counter to result in an error after X seconds within the goto loop.....



    [DllImport("USER32.DLL")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hwnd);
    [DllImport("USER32.DLL")]
    public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr next, string sClassname, IntPtr sWindowTitle);
    [DllImport("USER32.DLL")]
    public static extern uint GetDlgCtrlID(IntPtr hWnd);
    [DllImport("USER32.DLL")]
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
    [DllImport("USER32.DLL")]
    public static extern IntPtr SetActiveWindow(IntPtr hWnd);




    int y = 0;
    findconfirmprompt:
    if (y > 60)
    {
    MessageBox.Show("Unable to find the dialog to close Internet Explorer. Please contact your administrator or try running the program again.");
    Environment.Exit(0);
    }
    try
    {
    IntPtr targetwindow2 = FindWindow(null, "Windows Internet Explorer");
    if (targetwindow2 == IntPtr.Zero)
    {
    y = y + 1;
    Thread.Sleep(500);
    goto findconfirmprompt;
    }
    SetForegroundWindow(targetwindow2);
    SendKeys.SendWait("{ENTER}");

    }
    catch (Exception e)
    {
    MessageBox.Show("problem finding confirm popup");
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured