Filewatch saga continues

G

Guest

Hello!

I posted this problem earlier. I wrote a quick test project, and I still
have the same problem. So I'm sure it's something in my code that I'm not
writing correctly.
Basicly I'm creating a file with abutton click from one application. The
other application has a file watch setup, when it see the newly created file,
it is prompted to open up a new form. But it locks up atn the very end of
opening up the form.

Take a look at my code, what am I doing wrong? Or is there a better way of
doing this? Thanks!

Rudy
SERVER APPLICATION
<BEGIN CODE>
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim TimeStamp As String = Guid.NewGuid.GetHashCode
Dim TempFile As String = "C:\test\watch"
Dim ext As String = ".out"
Dim FileName As String = TempFile & TimeStamp & ext
Dim fs As New FileStream(FileName, FileMode.Create)
fs.Close()
End Sub

End Class
CLIENT APPLICATION with from that has watch process
<BEGIN CODE>
Imports System.IO
Public Class Form2
Inherits System.Windows.Forms.Form

Public NKWatch As New FileSystemWatcher

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler NKWatch.Created, AddressOf NKWatch_Created
Try
NKWatch.Path = "C:\test\watch"
NKWatch.Filter = "*.out"
NKWatch.IncludeSubdirectories = False
NKWatch.EnableRaisingEvents = True
Catch ERR As Exception
MessageBox.Show(ERR.Message)
End Try
End Sub

Public Sub NKWatch_Created(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
Dim frmFirst As New Form1
Me.Hide()
frmFirst.Show()
Application.DoEvents()
End Sub
End Class
 
T

Tom Leylan

There are a couple of suggestions I would like to offer. The first is (when
you're testing things that don't work) is to eliminate the parts that don't
(or probably don't) impact the problem. You could for instance still see
the problem even if you eliminate the "server app" from the equation. You
can simply copy a file into the folder and that will work as well. The
reason is that you're trying to narrow things down, the more pieces involved
the harder that will be.

And don't create and show a form when you're not certain the watcher is
working. Again you can output a simple message "I see it" to an output
window and reduce the number of suspicious spots even more.

The next suggestion is to use the debugger. What is your client application
doing when you think it has locked up? What do you want it to do? If I'm
not mistaken you'll find that your client app is running but without a form
on display. You are hiding the main form and showing a "Form1" (you should
rename that class) but it won't retain the focus the way you've written it.
It closes, the event handler is exited and frmFirst goes out of scope and is
destroyed.

I don't believe it has locked up I believe you can't see that it is still
running.

Tom
 
G

Guest

Hi Tom!

Thnks for the suggestions. I did make sure my watch works properly, but
having a s simple message box displayed. So I'm good there. I did try and
create a click event on the client ap, and open up the second form, and that
works fine. Iknow the form is locked up, because after a few seconds, I get
a non-responding on the window on my form. When I try to debug it, it doesnt
stop anywhere. Even when I put breka points, at the form load of the form 2,
form 1 and the file wacther. And to make things more intersting, after I get
the window that ask if I want to stop this program, do you want to send it
Microsoft. I get a meesage form CS 2003 that says "There is no source code
avaiable for the current location"

So I'm not sure sure where to look. I wrote this simple app, eleminating
everything elses in my real program. I hope if I can get this to work, I
might be able to fix my real application.

Any other thoughts?

Thanks!
Rudy
 
W

Wolfgang Hauer

Hi!

AFAIK runs your AddressOf NKWatch_Created in an own thread and you can't (
more or less in praxis) use a form with the messagpump of the main thread.

What you can try (but i don't recommend) is a application.start(form2) to
create a new messagepump. Or better ( i think) queue the request and start
processing with a timer or a monitor. And think about the problem when more
then 1 file is created and you have a form already open.


Wolfgang
 
T

Tom Leylan

But if everything else is working fine then the only thing left is the form
right?

I just cut and pasted the code and had it out "file created" to the
immediate window and it works. So try not hiding "me" and see if the form
isn't sitting there waiting for you to do something. The
Application.DoEvents() isn't doing anything as far as I can tell either.
I'm still quite certain your program is running you just can't see the form
because you've hidden it. frmFirst doesn't have focus and it's destroyed
immediately.
 
G

Guest

Hi Wolfgang!
I think it your right. It has something to do with the threading. When I
watch the threads in my program, I get 2 threads with no name or location.
When I clcik to run it, it say no source code available.
I hate to use a timer, I have enough of those running. What do you suggest
I monitor with?
 
G

Guest

Hi Tom!

I took out the hide, no change. With out the application.doevents, my
screen comes up white. But you say it works on yours? Are you running 2003
or 2005. Could it be my application of Visual Studio. It's been on this
workstation since 2004.

What do you think?

Rudy
 
G

Guest

Hi Guys!

OK. So I think I got it to work.

Public Sub NKWatch_Created(ByVal sender As Object, ByVal e AsMy question is this the best way. The form in question will only be up for
15 -20 seconds. The the user quits out. It seems to work great! I just want
to make sure I might not get in some kind of issue down the line.

Thanks for all your help!
Rudy

Ta
 
T

Tom Leylan

I have to say that I doubt your solution is the best way but there is no way
to tell unless we know what are you ultimately trying to do. What is point
of the pop-up form?

Again you're creating a temporary form (with a very short lifespan) and
displaying it non-modally. Is it supposed to take over for the main form?
Try this and see if some sort of dialog window would suit your needs.
Obviously you can fancy up the form but this demonstrates it can display
one.

Public Sub NKWatch_Created(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)

Dim frm As Form = New Form()
With frm
.Text = "File Created"
.ShowDialog()
End With

End Sub
 
W

Wolfgang Hauer

OOPs:

application.Run(...) is correct. And one hint: In the event the new created
file is maybe still open, so you can't open it. So bilding a queue is your
friend.
And the more i think about your problem i would use a timer and check the
queue. And don't forget to lock your queue when accessing.

Wolfgang
 
G

Guest

Hi Tom!

For this form, ShowDialog wouldn't work. I have values that are taken from
other forms, and populated into this one. I also have several connections
going on to SQL . The form is only up for 15 to 20 seconds, but there is alot
of stuff goin on in that time frame. After the form is closed, it will be
opened again from the server application in about 30 seconds to a minute, and
then the whole process starts. I'll do alot of testing on thsi, but I think
for this need, this is the way to go.

But I do beleive the ShowDialog would work on other formsI have. I want to
you custom messagboxes that pop up for 3-5 seconds then go out. I think that
would be perfect for that.

I certainly learned alot about managing forms, and have alot of
re-programming to do to make my program better. Thanks for all your help on
this!

Rudy
 
T

Tom Leylan

For this form, ShowDialog wouldn't work. I have values that are taken
from
other forms, and populated into this one. I also have several connections
going on to SQL . The form is only up for 15 to 20 seconds, but there is
alot
of stuff goin on in that time frame. After the form is closed, it will be
opened again from the server application in about 30 seconds to a minute,
and
then the whole process starts. I'll do alot of testing on thsi, but I
think
for this need, this is the way to go.

Rudy:

It was trickier than I thought due in part (in so far as I can tell) to the
thread-safe features added to VS2005. Starting from here
http://msdn2.microsoft.com/en-us/library/ms171728.aspx I managed to create a
pop-up (non-modal) window that displays "file created" in a label which then
disappears when a set number of seconds elapses.

I might be able to cut enough code in to a reply to make it make sense
without boring everybody... let's see.

Public Class <your main form>

Private pop As PopUp
Private poptimer As System.Windows.Forms.Timer

Delegate Sub PopCallback()

Private WithEvents worker1 As BackgroundWorker

Public NKWatch As FileSystemWatcher

PopUp is a form which will display, poptimer is a timer to make it go away,
PopCallback is used by the NKWatch event handler worker1 is a
BackgroundWorker (per the instructions found on the MSDN site) and you know
about NKWatch.

Inside <your main form> initialization add the following. Notice I've only
set up a single pop-up form. If you need more than one you'll have to do a
bit more work. In any case this pop is ready for use but not visible until
a file is detected. I've set the timer to close it after 2 seconds.

pop = New PopUp

With pop
.Owner = Me
.TopMost = True
.Visible = False
End With

poptimer = New System.Windows.Forms.Timer
With poptimer
.Interval = 2000
.Enabled = False
End With

AddHandler poptimer.Tick, AddressOf PopHandler

worker1 = New BackgroundWorker

NKWatch = New FileSystemWatcher

With NKWatch
.Path = "C:\"
.Filter = "*.txt"
.IncludeSubdirectories = False
.EnableRaisingEvents = True
End With

AddHandler NKWatch.Created, AddressOf NKWatch_Created

You already have the event handler so simply add the code within this.

Public Sub NKWatch_Created(ByVal sender As Object, ByVal e As
FileSystemEventArgs)
Me.worker1.RunWorkerAsync()
End Sub

Add this event handler which the article describes will be called when the
RunWorker completed event is called.

Private Sub PopWorkerCompleted(ByVal sender As Object, ByVal e As
RunWorkerCompletedEventArgs) Handles worker1.RunWorkerCompleted
Dim p As New PopCallback(AddressOf PopShow)
Me.Invoke(p, New Object() {})
End Sub


Add PopShow to make the popup window appear and PopHandler to make it
disappear.

Private Sub PopShow()
pop.Visible = True
poptimer.Start()
End Sub

Private Sub PopHandler(ByVal myObject As Object, ByVal myEventArgs As
EventArgs)
pop.Visible = False
poptimer.Stop()
End Sub

Clearly you'll have to futz with it somewhat and I've mixed my AddHandler
and Handles syntax (I prefer AddHandler) but hopefully this points you in
the right direction.

Tom
 
G

Guest

Hi Tom!

WOW!! That is cool! So I'm trying to work through this. I'm confused
where I'm grabbing "BackgroundWorker" from. As in "Private WithEvents worker1
as BackgroundWorker". What namespace am I importing, or am I missing
something?

Thanks!

Rudy
 
T

Tom Leylan

Well Rudy... there is the "help" system but it's also a fair guess it is
included in one of the 4 listed in the code included on the MSDN page and it
won't be System.Windows.Forms.
 
G

Guest

Hi Tom!
I did look at the help, But I'll take a closer look. I'll have to tweak the
code a little. But I definitely appreciate all your help on this. I'll have
to look in that safe threading as well. I have VS 2005 for work, and VS 2003
at home. I haven't upgraded yet. I really should, because I'm starting to
really like 2005, even though I don't program at work as much as I do at home.

Thanks again for all your help on this, I have really leanred alot from this.

Rudy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top