|
Hi,
I have implemented a communication model where I have a listening socket, waiting for requests on a specific port.
Once it gets called, it generates a client that creates two sockets: one sending a request to my server and another one that represents the client itself (and displays the data).
Perhaps it is a bit clearer with code:
public void StartRelay()
{
try
{
ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket);
DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(this.OnRemoteReceive), DestinationSocket);
}
catch
{
Dispose();
}
}
protected void OnClientReceive(IAsyncResult ar)
{
try
{
int Ret = ClientSocket.EndReceive(ar);
if (Ret <= 0)
{
Dispose();
return;
}
DestinationSocket.BeginSend(Buffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnRemoteSent), DestinationSocket);
}
catch
{
Dispose();
}
}
protected void OnRemoteSent(IAsyncResult ar)
{
try
{
int Ret = DestinationSocket.EndSend(ar);
if (Ret > 0)
{
ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket);
return;
}
}
catch { }
Dispose();
}
protected void OnRemoteReceive(IAsyncResult ar)
{
try
{
int Ret = DestinationSocket.EndReceive(ar);
if (Ret <= 0)
{
Dispose();
return;
}
ClientSocket.BeginSend(RemoteBuffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnClientSent), ClientSocket);
}
catch
{
Dispose();
}
}
protected void OnClientSent(IAsyncResult ar)
{
try
{
int Ret = ClientSocket.EndSend(ar);
if (Ret > 0)
{
DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(this.OnRemoteReceive), DestinationSocket);
return;
}
}
catch { }
Dispose();
}
This is working fine. However, I would like to replace the destination socket by an asynchronous webrequest and set the RemoteBuffer on "ClientSocket.BeginSend", by the result of that request (in bytes). The reason why I want to do this, is because I have a server with authentication and the webrequest allows me to use credentials, while the socket communication (in destinationSocket) will fail...
I am a bit confuse on how to use the asyncronous webrequest with the asyncronous client socket...
I imagine I have to do something like this?
request = (HttpWebRequest)WebRequest.Create(myUri);
request.Credentials = CredentialCache.DefaultCredentials;
IAsyncResult r = (IAsyncResult)request.BeginGetResponse(
new AsyncCallback(RespCallback), request);
and "RespCallback" call the "ClientSocket.BeginSend"; however, for retrieving the buffer, I need to get the stream of the request, and if that is also asynchronous I also need a "begin..." and a callback... for the stream...
Plus, do I need to store this buffer in a variable anywhere? (I imagine if the buffer is smaller than the response, the function will get called multiple times!)
Does anybody has implemented or experienced something like this?
Basically, I have a client socket and I want to send to it assyncronously a buffer of data that is get asynchronously from a webrequest...
Any suggestions will be really welcome! Thanks in advance for your time,
cheers,
Jo
|
|
|
|
|
I am trying to delete a file that is being reported in use and I am not sure how to conquer the issue. Here is the relevant code, in my opinion.
string destdir = "c:\\moved\\";<br />
this.picturebox1.image = image.fromfile("c:\\test.jpg");
--code stuff--
<br />
this.picturebox1.image = null;<br />
this.picturebox1.invalidate();<br />
File.Move("c:\\test.jpg",destdir + "test.jpg");
There is a lot more to the code than that, obviously, but this is the crux of the issue, anyone have a suggestion on how I can properly close the file in order to move / delete it?
|
|
|
|
|
Image.FromFile() keeps the file open as long as the Image is alive.
You must Dispose of it to free the file! Setting the reference null does not achieve this, it may or may not result in the GC to collect the image the next time it runs, i.e. the next time a memory request cannot be satisfied without first performing a collection.
The alternative is to replace FromFile() by FromSream(); that one does not lock the file.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
modified on Tuesday, August 4, 2009 12:26 PM
|
|
|
|
|
Luc Pattyn wrote: The alternative is to replace FromFile() by FromSream(); that one does not lock the file.
I was going to suggest that. But I was not sure that if you disposed the stream, will picture box still shows it?
|
|
|
|
|
Image.FromFile() generates a real Image and locks the file so it can still get at the metadata available through GetPropertyItem().
Image.FromStream() generates a real Image and no longer needs the stream; you can do to the stream whatever you like, the image couldn't care less.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
hi,i need to use a telephone component for my application,such as answering machin or ...
some friend adviced me kdtele component ,but i dont know how to use them
if any body knows please help me
thanx
|
|
|
|
|
Try here[^]
only two letters away from being an asset
|
|
|
|
|
|
This is probably pretty simple, but I'm a bit unsure of how to approach this. I have a path to a particular bmp file in my C# code. I want to display this picture in my HTML page. How would I pass this path to the HTML code instead of hardcoding the path?
C#:
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\temp.bmp";
HTML (hard coded at the moment):
<img src="C:\\Users\\L****\\AppData\\Local\\Program\\temp.bmp" alt="Graph"/>
|
|
|
|
|
You can do this either in the code behind img.src = filePath or in the page <img src=<%=filePath%>
only two letters away from being an asset
|
|
|
|
|
I'm still a bit confused since I haven't worked with this before.
The path: string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\temp.bmp"; is located in ChartContainer.ScreenShotWin.
From what you wrote, I would assume I would make these changes to my hardcoded html file, but this is not solving passing filePath. File '<%=filePath%>' was not found.
<%@ Language="C#" Inherits="ChartContainer.ScreenShotWin" %>
<html>
<head>
<title></title>
</head>
<body>
<img src=<%=filePath%> alt="Graph"/>
</body>
</html>
What am I still misunderstanding? Thank you!
|
|
|
|
|
I suggest you modify your img tag to make it a server-side tag, and modify it's src property somewhere in the loading stages of your form.
<img runat="server" id="graphImage" alt="Graph" />
And in your code, possibly in your Form_Load method, you would assign the value to it.
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\temp.bmp";
graphImage.Src = filePath;
Adam Maras | Software Developer
Microsoft Certified Professional Developer
|
|
|
|
|
What type would graphImage.Src be defined as within my c# code?
My code needs to save a picture to a local folder, navigate to an html file via webbrowser offscreen displaying said prior saved photo (by accessing it via the local folder path which I want to pass to it), take an offscreen screenshot, and save to file. In the end certain bits of data will also need to be created at runtime and displayed on the webbrowser for the screenshot. They want an image report. I guess I better start reading up on ASP.
Everything else works fine except for accessing the picture in html without hardcoding it because I still am a bit in the dark about how this is passing the pathway to the image at runtime.
<%@ Language="C#" Inherits="ChartContainer.ScreenShotWin" %>
<html>
<head>
<title></title>
</head>
<body>
<img alt="Missing Graph" runat="server" id="graphImage" />
blah blah blah...
private void ScreenShotWin_Load(object sender, EventArgs e)
{
filePath = Environment.GetFolderPat(Environment.SpecialFolder.LocalApplicationData) + "\\temp.bmp";
webBrowser1.Navigate("C:\\Projects\\HTMLReport.htm");
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 1000;
t.Tick += new EventHandler(t_Tick);
t.Start();
}
|
|
|
|
|
Good morning all.
I am coming up with an error: Cross-thread operation not valid. Attempted to access from a thread other than the thread it was created on.
My code:
private void btnSubmit_Click(object sender, EventArgs e)
{
if (tbRACFID.Text == string.Empty)
{
MessageBox.Show("You must have a RACFID in order to continue.", "RACFID Field Validation", MessageBoxButtons.OK, MessageBoxIcon.Stop);
ff.ControlSetFocus(tbRACFID);
return;
}
if (tbUserName.Text == string.Empty)
{
MessageBox.Show("You must have a User Name in order to continue.", "User Name Field Validation", MessageBoxButtons.OK, MessageBoxIcon.Stop);
ff.ControlSetFocus(tbUserName);
return;
}
PwdSecurity p = new PwdSecurity();
uipc = this.Parent as UIPCMainForm;
string strContainer = cbEndUserDepart.SelectedValue.ToString() + tbRACFID.Text.ToLower();
DAL d = new DAL(uipc.StrUserName, p.base64Decode(uipc.StrPassword));
this.backgroundWorker1.RunWorkerAsync();
btnReset.PerformClick();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
PwdSecurity p = new PwdSecurity();
uipc = this.Parent as UIPCMainForm;
string strContainer = cbEndUserDepart.SelectedValue.ToString() + tbRACFID.Text.ToLower();
DAL d = new DAL(uipc.StrUserName, p.base64Decode(uipc.StrPassword));
BackgroundWorker worker = (BackgroundWorker)sender;
string SubnetIPAddress = string.Empty;
DataSet ds = new DataSet();
ds = d.FillDropDowns(4);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (
dr["ROUTR_TYPE"].ToString() == cbRouter.Text &&
dr["LOC"].ToString() == cbLocation.SelectedValue.ToString())
{
SubnetIPAddress = dr["SUBNT_IPADR"].ToString();
}
}
d.importContainer(
strContainer,
tbUserName.Text,
cbRouter.SelectedValue.ToString(),
(int)arSubNetList[cbSubnet.SelectedIndex],
cbSubnet.SelectedValue.ToString(),
arDHCPOptionList[cbLocation.SelectedIndex].ToString(),
arDHCPPolicyList[cbLocation.SelectedIndex].ToString(),
SubnetIPAddress,
arAllocationTemplateList[cbSubnet.SelectedIndex].ToString()
);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.tsProgBar.Value = e.ProgressPercentage;
this.tslStatus.Text = e.UserState as string;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.tslStatus.Text = "Operation complete";
}
Any ideas? I saw something online about Invoke. Thank you, WHEELS
|
|
|
|
|
Hi,
Here[^] is the full description of your problem.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
but isnt the purpose of backgroundworker that u dont need to explicitly invoke the controls?
if i call worker.ReportProgress i dont need to invoke the controls and they are updated without cross threading exceptions!
i might have missed but idont see you calling worker.ReportProgress from your worker_DoWork method?
modified on Tuesday, August 4, 2009 11:37 AM
|
|
|
|
|
Two of the BGW events are handled on the main thread; and obviously DoWork is not, otherwise nothing would be going on in the background.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
|
Hello Forum
When you call Form.Close() the form actually closes but in fact it is still lives in memory, i need to make sure that when i close a form the from is dead and nothing is being executed any more on this form, is the way to achieve this by invoking the GC.Collect() or there is another way.
Please help
Thank You
|
|
|
|
|
there is no other way!
but you need not call GC.Collect(), the system does it regularly when it's idle and memory is low (or other propitious condition)
however, for testing / debugging purpose, it's good to call GC.Collect() and the use some kind of memory profiler to see if some unexpected object stays in memory.
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
Hi,
a Form doesn't do anything, neither do any other .NET object.
The only things that do something are Windows timers, Windows events and Windows threads.
So if your Form did launch a timer or a thread (incl. BackgroundWorkers and asynchronous I/O operations) you should make sure they come to a halt; having the Form collected is both unnecessary and insufficient.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
right, i had some timer's and upon closing i stoped them, but i was wonderig if thre is a way with one command stop them all, this is why i was going in this direction.
Thanks for your help
|
|
|
|
|
Write the timer stop code in Form's dispose method. Also consider using background threads to do the processing. Background threads will terminate when the parent thread terminates.
|
|
|
|
|
great idea
thanks
|
|
|
|
|
I can add text, data values from the database into a word file through the C# coding. I want to know how to insert an image in the word file through c# coding?Plz help urgently!!!!
csetopper_bhanu
|
|
|
|
|