Seems like I have a lot to learn about the proper handeling of threads. Would any of you have a recommendation on the topic but in relations to c#? I dislike VB...
svanwass
Posts
-
Question on threading -
Question on threadingMy apologies. Main Form
Public Class Form1
Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim passing(0, 3) As String passing(0, 0) = "10.1.1.1" passing(0, 1) = "10.1.1.2" passing(0, 2) = "10.1.1.3" passing(0, 3) = "10.1.1.4" Dim myworker As New Foo(passing) End Sub
Private Sub IHeardThat() Handles Foo.IamDone
'this does NOT work
End Sub
End ClassMy Foo class
Imports System.ComponentModel
Imports System.IO
Imports System.Threading
Public Class FooPublic WithEvents backgroundWorker1 As System.ComponentModel.BackgroundWorker Public Event IamDone(ByVal sender As Object, ByVal IP As String) Dim devicelist(,) As String Dim txtFile Public Sub New(ByVal Value As String(,)) devicelist = Value backgroundWorker1 = New BackgroundWorker backgroundWorker1.WorkerReportsProgress = True backgroundWorker1.RunWorkerAsync(devicelist) End Sub Private Sub backgroundWorker1\_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork ' Get the BackgroundWorker object that raised this event. Dim worker As BackgroundWorker = CType(sender, BackgroundWorker) Dim myargs As String(,) = e.Argument End Sub Private Sub backgroundWorker1\_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted RaiseEvent IamDone(Me, "Done") End If End Sub
End Class
So that's basically it. I attempted to write a sub that would handle the custom event but the form class doesnt see the foo class event. Does that make sense?
-
Question on threadingI wrote a simple simple version of what I'm attempting. http://www.4shared.com/file/-tIHs2FE/CustomEventsVB.html[^] Does that help explain my issue?
-
Question on threadingOK so I attempted it and I think I ran into an issue with scope. In the worker thread, I created an event
Public Event IamDone(ByVal sender As Object, ByVal IP As String)
I call that event in the RunWorkerCompleted sub
RaiseEvent IamDone(Me, "111")
And now I am trippng up on how to create the handler in my thread manager. I assumed it would be
AddHandler myworkerthreadclass.IamDone, Addressof myworkerthreadclass.IamDone
but that doesn't work
-
Question on threadingOK so I attempted it and I think I ran into an issue with scope. In the worker thread, I created an event
Public Event IamDone(ByVal sender As Object, ByVal IP As String)
I call that event in the RunWorkerCompleted sub
RaiseEvent IamDone(Me, "111")
And now I am trippng up on how to create the handler in my thread manager. I assumed it would be
AddHandler myworkerthreadclass.IamDone, Addressof myworkerthreadclass.IamDone
but that doenst work
-
Question on threadingSo, if all of your work is being done in these worker threads, then your main application just becomes a thread manager. Right? (lets take that as true)
Yes
- Each thread should throw an event which represents "WorkDone"; the thread manager will
listen for this event and update the corresponding array slot.
How can I make the thread throw a custom event? Conversly, how do I make the thread manager listen?
- Each thread should throw an event which represents "WorkDone"; the thread manager will
-
Question on threadingI am attempting to write a multithreaded application. The idea is to have a scripted exe run that takes a list of devices, strips out one device and its information into an array, creates a new object of a class. which contains a backgroundworker that contains a telnet object, to telnet to said device and perform simple work (just login at this time). I have 100 device I am doing this for. So far, everything looks like it works. All 100 devices have a new class object and bgw worker created for them. My problem lies in how to determine when those have completed their work. This is a problem because the rest of the exe needs to process the results of my simple work but can only be done after all have completed. How can I tell when the work is done? I was thinking about writing something into the RunWorkerCompleted event that tallys but that seems like not the best idea. :sigh: -Steve
-
Reading standardoutput to a fileWow. Responses to the dome. I will attempt it and get back.
-
Reading standardoutput to a fileI should have been more specific. The part of saving to a specific file name/path is where I am lacking.
-
Reading standardoutput to a fileI am trying to call a program that comverts BMP to PPM formats. The exe to do so dumps the binary contents of the conversion to the console unless redirected as such
bmptoppm.exe input.bmp >output.ppm
I am attempting to call this exe from inside a different windows forms application and save the ouput to output.ppm. I am doing so using the process classProcess myProcess = new Process(); myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.Arguments = "C:\\input.bmp"; myProcess.StartInfo.FileName = "C:\\bmptoppm.exe"; myProcess.StartInfo.CreateNoWindow = false; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardError = true; myProcess.Start(); //file writing code here?
I am getting hung on on how exactly I can do the writing of the standardoutput to a file. Could someone point me in the right direction please? -
Different way to copy a fileIn code, how can I copy a file from X:\ to X:\archive without reading entire file contents? I am using :
FileStream fs = new FileStream("X:\\file.dat", FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length byte\[\] writeArray = new byte\[fs.Length\]; //Read block of bytes from stream into the byte array fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); // Create a synchronization object that gets // signaled when verification is complete. ManualResetEvent manualEvent = new ManualResetEvent(false); FileStream fStream = new FileStream("X:\\\\archive\\file.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true); // Check that the FileStream was opened asynchronously. Debug.Print("fStream was {0}opened asynchronously.", fStream.IsAsync ? "" : "not "); // Asynchronously write to the file. IAsyncResult asyncResult = fStream.BeginWrite ( writeArray, 0, writeArray.Length, new AsyncCallback(EndWriteCallback), new State(fStream, writeArray, manualEvent) );
This works, but performing
fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length));
essentially copies the file contents back to across the network
-
Weird delay when writing a fileHow would the size of my buffer change the delay when calling the Close method? Wouldn't a smaller/larger buffer size effect the Flush method only?
-
Weird delay when writing a fileNo, I have not tried Begin/EndWrite. I read the MSDN article but to be sure, testing these out would satisfy Async writing?
-
Weird delay when writing a fileYea, I have to map to 600 servers, archive some files (one of which is large) then copy over new version of those files.
-
Weird delay when writing a fileThat's going to be difficult. There are ~600 devices this has to happen to.
-
Weird delay when writing a fileTaking what you said
Aside from performing the copy operation on the system where the files reside rather than going through a mapped drive.
How can I do that?
-
Weird delay when writing a fileOh, and I did add all these statements after the while loop:
destStream.Flush();
destStream.Close();
destStream.Dispose();Close is where it is hanging at. More thoughts?
-
Weird delay when writing a fileI certainly didn't mean to start a fight over debugging... :)
-
Weird delay when writing a file -
Weird delay when writing a fileI am getting a weird delay when writing a file. I am not able to figure out where the delay is coming from. The facts: I map a drive to a NT server (W:\) I copy a file at W:\largefile.dat to W:\Archive\largefile.dat My copy loop:
try
{
using (FileStream sourceStream = new FileStream(nd_CopyDrive.LocalDrive + "\\" + fi.Name, FileMode.Open))
{
byte[] buffer = new byte[64 * 1024];
using (FileStream destStream = new FileStream(nd_CopyDrive.LocalDrive + "\\Archive\\" + fi.Name, FileMode.Create))
{
int q;
while ((q = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
{
destStream.Write(buffer, 0, q);
}
}
}
}When the file is completely written my program hangs at the closing bracket for the second using statement. Nothing is shown in the Output window that gives me some clues. This block of code IS running in a background worker as added information. Could my problem be related to how the CLR "releases the memory used to store objects that are no longer required". If I place a debug marker at that ending bracket and perform a step into, the Output window shows:
Step into: Stepping over method without symbols 'System.IO.Stream.Dispose' (which takes forever to complete)
Any help would be greatly appreciated!