|
In C#, how does one make a thread live indefinitely?
MyObject obj = new MyObject();
Thread t = new Thread(new ThreadStart(obj.MyMethod));
t.Start();
Because MyMethod has code in it that kicks off an asynchronous process in it, I want that thread to live forever (or at least until the application is terminated). Any ideas on how that might be accomplished?
Thank you,
Dan
C#, King of languages
|
|
|
|
|
Just don't return from the method.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Guffa had the right answer. But just to clarify, the thread won't end until the method obj.MyMethod() returns. You're allowed to return from the method that creates the Thread object and calls t.Start().
In fact, you don't even have to keep a reference to the Thread object that you created. You can simply write this as one line:
new Thread(new ThreadStart(obj.MyMethod)).Start();
|
|
|
|
|
This may seem like a dumb question, but I haven't been able to find the answer out there.
I have a 32 bit application with several 32 bit COM controls that can interact with it. I also have a .NET app that uses these COM controls. When I run the .NET app on a 32 bit machine, it runs just fine, no problems.
When I install the 32 bit application and its COM controls to an x64 system, the .NET application crashes. If I look at the exception thrown when I compile the .NET app on the x64 system, it says "COM Exception was unhandled". Then the rest of the message is: Retrieving the COM class factory for component with CLSID {B87F44E3-191A-4655-B582-D2E176A09BCF} failed due to the following error: 80040154.
When I look on the web at that error all that is mentioned is that the COM control is not registered properly.
On the x64 machine, if I run an old VB6 program that uses those same COM controls, it works just fine, so obviously my COM controls are registered so that the VB6 app can see them.
Is it possible that I need to recompile all the 32 bit code (32 bit app and COM controls) as 64 bit to get it to work on the x64 machine?
Are there any other ideas?
Thanks!
David
|
|
|
|
|
I have form1 and form2.
But form2 is not a child of form1.
How to fix this.
Because when i want to close one form, the two forms are closing.
Vasildb
|
|
|
|
|
It is not clear what you are asking for. I assume that form1 is your main form and form2 another form initialized in form2. So if you close (by closing I do not mean just changing the Visible property to false, I mean exit) form1 the application will exit. This will also happen if form2 was the child of form1...
Here is a snippet keeping the application running by changing the application thread, when the main form is disposed :
<br />
private void onExitEvent(object sender, EventArgs e)<br />
{<br />
Application.ExitThread();
<br />
Thread mainThread = new Thread(new ThreadStart(startRun));
mainThread.SetApartmentState(ApartmentState.STA);
mainThread.Start();
<br />
}<br />
public void startRun()<br />
{<br />
Application.Run(new NewForm());
}<br />
I am not sure if it has performance overheads or any side effects, but I am using this pattern and everything is working fine.
Best regards..
.:: Something is Wrong ::.
|
|
|
|
|
Is there something easy i can use? That doesn't involve the Windows Media Player? Or anything really. Thats the point of this app, that it doesn't need anything else installed.
Or do you think i'll have to yoink the stuff straight from the files by reading them byte by byte?
|
|
|
|
|
If you are talking about mp3 files, look up the id3 tag . Reading the id3 tag is done by yoinking bytes from the file.
I believe that there is an article here on the cp with an id3 class that works for id3 version 1 at least.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Yeah, after peering into an MP3 with a hex editor, each piece of info has its own little 4 letter tag, like TIT1, or TRCK.
So i'll probably just do it all myself.
|
|
|
|
|
The Undefeated wrote: Yeah, after peering into an MP3 with a hex editor, each piece of info has its own little 4 letter tag, like TIT1, or TRCK.
This is the ID3 Version 2 tag. It's very complicated and pretty complex to parse. You might want to check the much easier ID3 Version 1 tag. It's typically at the end of the MP3 file, starting 128bytes from the end to then end. If there is an ID3v1 tag, then the last 128bytes start with 'TAG'.
It's very easy to read and write.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hmm. I don't have much choice of what tags i get to use in the first instance.
I file could come in with any version of an ID3 tag so im gonna have to check 'em all out i guess.
I've also got to accomodate for the tags in WMA files, which are much clearly labled it seems. For example, a tag might contain this: O r i g i n a l A r t i s t Bob
Incase you couldn't tell, the original artist is Bob
|
|
|
|
|
I am trying to get and R, G, and B value and make the form that color, but the BackColor. R, G, and B values are all read-only. how should i go about doing this? is there a way to convert RGB values into hex-colors(or whatever they're called - 000000 is black and ffffff is white)and then assign the hex to the form?
thanks in advance
|
|
|
|
|
The R, G and B properties are read-only because you can't change a color value, you have to create a new color value.
You can use the FromArgb and ToArgb methods to convert from int to Color and back.
You can use the formatting string "X6" if you want to format an int to a six digit hexadecimal string, and if you want to parse a string to an int.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
hi
i have a button in my webPage and i define a Javascript Function in my Suorce Page and set OnClientClick="MyJavaScrpitFunction()", it is worked good but my problem is when i click button at runtime, page is Reload and refresh my page and my cpntrol's Changes will be lost. i want only OnClientClick event run and don't want button_click event run.
how to solve problem ?(i thinks that override button_click event but how to ?)
and does have SomeBody Suggestion for this operation ?
thanks
|
|
|
|
|
Did you try removing the event declaration?
|
|
|
|
|
i don't create event handler. it will run automatically
|
|
|
|
|
Are you trying to prevent the page being posted back when you click the button and just want to run some javascript on the client when it is clicked?
If so, maybe setting the button's on client click to the following will work:
"MyJavaScrpitFunction(); return false;"
|
|
|
|
|
Hi Plz I need help on this
I have a schedular that on specific time fires a function
this function fires another function that takes arguments from a database
well here is my problem: at runtime it seems that the function does not recognise the arguments
PLZ Can any one help me on this
|
|
|
|
|
I have a main form, and a form named preferences, and i have attempted to get the information from the form into the settings, where i have the background color information stored.
Preferences.cs:
private void btnPickBGColor_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
BackgroundPanel.BackColor = colorDialog1.Color;
txtBackGroundColorName.Text = colorDialog1.Color.Name;
}
private void btnSave_Click(object sender, EventArgs e)
{
Close();
}
Form1.cs (starting form):
private void btnPreferences_Click(object sender, EventArgs e)
{
Preferences PreferenceDialogBox = new Preferences();
PreferenceDialogBox.BackgroundPanel.BackColor = Properties.Settings.Default.BackGroundColor;
PreferenceDialogBox.txtBackGroundColorName.Text = Properties.Settings.Default.BackGroundColor.Name;
if (PreferenceDialogBox.ShowDialog() != DialogResult.Cancel)
{
Properties.Settings.Default.BackGroundColor = PreferenceDialogBox.BackgroundPanel.BackColor;
Properties.Settings.Default.Save();
this.BackColor = Properties.Settings.Default.BackGroundColor;
}
}
how else should i get the information into settings? the preferences form does detect the background color settings and adjusts the display and text accordingly.
Thanks for the help
(and sorry about the long code)
|
|
|
|
|
there isn't a problem in the code, but i found out that the save button was set as a cancel button.
|
|
|
|
|
Hi all,
I have a drop down control which let the user to select for creating number of textbox controls into the form. Once it generate the textboxes, I check the view source for each of the textbox control with _ctl0, _ctl1 ..... added in front of the textboxes'id. I need to get those value and insert into the DB. Here is my code behind.
private void btnMulitTextBox_Click(object sender, System.EventArgs e)
{
SqlConnection conn = ....;
conn.Open();
cmd = new SqlCommand("....", conn);
cmd.CommandType = CommandType.StoredProcedure;
int i = 0;
int totalnumofctrl = Convert.ToInt32(this.ddlNumtoGenerate.SelectedValue.ToString());
while (i < totalnumofctrl)
{
DoInsertToDB(i);
i++;
}
}
private void DoInsertToDB(int ctrlnum)
{
string textboxname ="_ctl" + ctrlnum + ":txtAName";
System.Web.UI.WebControls.TextBox tb = this.FindControl(textboxname) as System.Web.UI.WebControls.TextBox;
cmd.Parameters.Add("@AName", tb.Text);
cmd.ExecuteNonQuery();
}
I got "Object reference not set to an instance of an object." error everytime I run this. Any idea? Thanks in advance.
gsdg
|
|
|
|
|
One would suspect that your "FindControl" method is failing to return a match, causing a null reference error in the next line of code. It might be helpful if you included the code for that (as well as the code that creates the textboxes so that we could see how they get named). In any case, you should check tb for null before using it.
Also, it appears that cmd is instantiated in the first method, and used many times in the second method, without clearing the parameters list before adding a new one by the same name.
|
|
|
|
|
Stop spamming. You already have an active thread about this.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
I want to listen ip host with a specified port number. But not local ip address. I've tried to do it using three classes IPEndPoint, Socket, NetworkStream, but I couldn't see any data (in a MessageBox for example). I use:
private static string connectSocket(string address, int port)
{
//address = "10.0.105.4";
//port = 4000;
String data = null;
try
{
Byte[] bytes = new Byte[256];
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(address), port);
Socket socket = new Socket(endPoint.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
socket.Connect(endPoint);
NetworkStream ns = new NetworkStream(socket);
Int32 i;
while((i = ns.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
}
return data
}
Does anyone know how to solve this?
|
|
|
|
|
Err... The line that goes:
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Should you appending? i.e.
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
It is that simple, just adding the data += instead of data = ?
- Eitsop
What we do not understand we do not possess. - Goethe.
|
|
|
|